2016-11-20 20:04:52 +01:00
|
|
|
CREATE TABLE users (
|
|
|
|
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
|
|
|
|
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
2020-01-14 13:13:10 +01:00
|
|
|
deleted_at timestamptz NULL,
|
2016-11-20 20:04:52 +01:00
|
|
|
|
|
|
|
fullname text NOT NULL DEFAULT '',
|
|
|
|
email text NOT NULL,
|
|
|
|
photo text NOT NULL,
|
|
|
|
password text NOT NULL,
|
2019-12-09 16:27:01 +01:00
|
|
|
|
2020-01-14 13:13:10 +01:00
|
|
|
lang text NULL,
|
|
|
|
is_demo boolean NOT NULL DEFAULT false
|
2016-11-20 20:04:52 +01:00
|
|
|
);
|
|
|
|
|
2020-01-23 17:50:00 +01:00
|
|
|
CREATE UNIQUE INDEX users__email__idx
|
|
|
|
ON users (email)
|
|
|
|
WHERE deleted_at IS null;
|
|
|
|
|
|
|
|
CREATE INDEX users__is_demo
|
|
|
|
ON users (is_demo)
|
|
|
|
WHERE deleted_at IS null
|
|
|
|
AND is_demo IS true;
|
|
|
|
|
|
|
|
--- Table used for register all used emails by the user
|
2020-01-25 17:23:21 +01:00
|
|
|
CREATE TABLE user_emails (
|
2020-01-23 17:50:00 +01:00
|
|
|
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
|
|
|
|
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
verified_at timestamptz NULL DEFAULT NULL,
|
|
|
|
|
|
|
|
email text NOT NULL,
|
|
|
|
|
|
|
|
is_main boolean NOT NULL DEFAULT false,
|
|
|
|
is_verified boolean NOT NULL DEFAULT false
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE INDEX user_emails__user_id__idx
|
|
|
|
ON user_emails (user_id);
|
|
|
|
|
|
|
|
--- Table for user key value attributes
|
|
|
|
|
2020-01-25 17:23:21 +01:00
|
|
|
CREATE TABLE user_attrs (
|
2019-12-01 16:48:41 +01:00
|
|
|
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
|
|
|
|
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
|
|
|
|
key text NOT NULL,
|
|
|
|
val bytea NOT NULL,
|
|
|
|
|
|
|
|
PRIMARY KEY (key, user_id)
|
|
|
|
);
|
|
|
|
|
2020-01-23 17:50:00 +01:00
|
|
|
--- Table for store verification tokens
|
|
|
|
|
2020-01-25 17:23:21 +01:00
|
|
|
CREATE TABLE tokens (
|
2019-12-01 16:48:41 +01:00
|
|
|
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
|
|
token text NOT NULL,
|
|
|
|
|
|
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
2020-01-14 13:13:10 +01:00
|
|
|
used_at timestamptz NULL,
|
2019-12-01 16:48:41 +01:00
|
|
|
|
|
|
|
PRIMARY KEY (token, user_id)
|
|
|
|
);
|
|
|
|
|
2020-01-23 17:50:00 +01:00
|
|
|
--- Table for store user sessions.
|
|
|
|
|
2020-01-25 17:23:21 +01:00
|
|
|
CREATE TABLE sessions (
|
2019-11-18 11:52:57 +01:00
|
|
|
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
|
|
|
|
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
modified_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
|
|
|
|
|
|
user_id uuid REFERENCES users(id) ON DELETE CASCADE,
|
2020-01-14 13:13:10 +01:00
|
|
|
user_agent text NULL
|
2019-11-18 11:52:57 +01:00
|
|
|
);
|
|
|
|
|
2020-01-23 17:50:00 +01:00
|
|
|
CREATE INDEX sessions__user_id__idx
|
|
|
|
ON sessions (user_id);
|
|
|
|
|
2016-11-20 20:04:52 +01:00
|
|
|
-- Insert a placeholder system user.
|
2019-12-01 16:48:41 +01:00
|
|
|
|
2020-02-04 16:05:51 +01:00
|
|
|
INSERT INTO users (id, fullname, email, photo, password)
|
2016-11-20 20:04:52 +01:00
|
|
|
VALUES ('00000000-0000-0000-0000-000000000000'::uuid,
|
|
|
|
'System User',
|
|
|
|
'system@uxbox.io',
|
|
|
|
'',
|
2020-01-14 13:13:10 +01:00
|
|
|
'!');
|
2016-11-20 20:04:52 +01:00
|
|
|
|
2020-01-23 17:50:00 +01:00
|
|
|
--- Triggers
|
2016-11-20 20:04:52 +01:00
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
CREATE TRIGGER users__modified_at__tgr
|
|
|
|
BEFORE UPDATE ON users
|
2019-11-18 11:52:57 +01:00
|
|
|
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
|
2016-11-20 20:04:52 +01:00
|
|
|
|
2019-12-09 16:27:01 +01:00
|
|
|
CREATE TRIGGER user_attrs__modified_at__tgr
|
|
|
|
BEFORE UPDATE ON user_attrs
|
2019-12-01 16:48:41 +01:00
|
|
|
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
|
2016-11-20 20:04:52 +01:00
|
|
|
|