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),
|
|
displayName VARCHAR(64),
|
|
avatarURL TEXT,
|
|
passwordHash TEXT,
|
|
email TEXT,
|
|
emailVerified BOOLEAN NOT NULL DEFAULT FALSE,
|
|
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 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 UNIQUE INDEX IF NOT EXISTS accountsEmailUnique ON accounts (LOWER(email));
|
|
|
|
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);
|