puerta/schema.sql
2023-04-16 15:17:36 -06:00

63 lines
1.5 KiB
SQL

CREATE TABLE user(
id INTEGER PRIMARY KEY AUTOINCREMENT,
handle VARCHAR(255) NOT NULL UNIQUE,
name TEXT NOT NULL,
password TEXT,
expires TEXT, -- datetime
greeting TEXT,
max_ttl TEXT DEFAULT "30d", -- golang auth.TTL
schedule TEXT, -- golang auth.UserSchedule
second_factor BOOLEAN DEFAULT 1,
is_admin BOOLEAN DEFAULT 0 NOT NULL,
receives_notifications BOOLEAN DEFAULT 0 NOT NULL
);
CREATE INDEX user_id ON user(id);
CREATE INDEX user_handle ON user(handle);
CREATE TABLE credential(
user INTEGER NOT NULL,
data TEXT NOT NULL,
FOREIGN KEY(user) REFERENCES user(id) ON DELETE CASCADE
);
CREATE INDEX credential_user ON credential(user);
CREATE TABLE session(
token TEXT PRIMARY KEY,
user INTEGER NOT NULL,
expires TEXT NOT NULL, -- datetime
FOREIGN KEY(user) REFERENCES user(id) ON DELETE CASCADE
);
CREATE INDEX session_token ON session(token);
CREATE TABLE log(
timestamp TEXT PRIMARY KEY,
user TEXT NOT NULL,
second_factor BOOLEAN NOT NULL,
failure VARCHAR(255),
error TEXT,
ip_address varchar(255) NOT NULL,
user_agent varchar(255) NOT NULL
);
CREATE TABLE subscription(
user INTEGER NOT NULL,
data TEXT NOT NULL,
FOREIGN KEY(user) REFERENCES user(id) ON DELETE CASCADE
);
CREATE INDEX subscription_user ON subscription(user);
CREATE INDEX log_timestamp_idx ON log(timestamp);
CREATE INDEX log_timestamp_error_idx ON log(timestamp,error);
CREATE INDEX log_timestamp_user_idx ON log(timestamp,user);
CREATE TABLE migrations (
name TEXT NOT NULL,
applied TEXT NOT NULL
);