40 lines
1.3 KiB
SQL
40 lines
1.3 KiB
SQL
CREATE TABLE IF NOT EXISTS accounts (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
|
username VARCHAR(32) NOT NULL,
|
|
displayName VARCHAR(64),
|
|
avatarURL TEXT,
|
|
status TEXT NOT NULL DEFAULT 'active' CHECK (status in ('active', 'suspended', 'disabled')),
|
|
createdAt TIMESTAMPTZ NOT NULL DEFAULT NOW (),
|
|
updatedAt TIMESTAMPTZ NOT NULL DEFAULT NOW (),
|
|
lastLoginAt TIMESTAMPTZ
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS localCredentials (
|
|
accountId UUID PRIMARY KEY REFERENCES accounts (id) ON DELETE CASCADE,
|
|
passwordHash TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE externalAccounts (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
|
accountId UUID NOT NULL REFERENCES accounts (id) ON DELETE CASCADE,
|
|
provider TEXT NOT NULL,
|
|
providerUserId TEXT NOT NULL,
|
|
linkedAt TIMESTAMPTZ NOT NULL DEFAULT NOW (),
|
|
UNIQUE (provider, providerUserId),
|
|
UNIQUE (accountId, provider)
|
|
);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS accountsUsernameUnique ON accounts (LOWER(username));
|
|
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
|
accountId UUID NOT NULL REFERENCES accounts (id) ON DELETE CASCADE,
|
|
tokenHash TEXT NOT NULL UNIQUE,
|
|
createdAt TIMESTAMPTZ NOT NULL DEFAULT NOW (),
|
|
expiresAt TIMESTAMPTZ NOT NULL,
|
|
lastUsedAt TIMESTAMPTZ,
|
|
revokedAt TIMESTAMPTZ
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS lookUp ON sessions (accountId);
|