0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-25 07:58:49 -05:00
penpot/backend/resources/migrations/0002.users.sql

102 lines
2.4 KiB
MySQL
Raw Normal View History

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(),
deleted_at timestamptz NULL,
fullname text NOT NULL DEFAULT '',
email text NOT NULL,
photo text NOT NULL,
password text NOT NULL,
lang text NULL,
is_demo boolean NOT NULL DEFAULT false
);
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
CREATE TABLE user_emails (
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
CREATE TABLE user_attrs (
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)
);
--- Table for store verification tokens
CREATE TABLE tokens (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
used_at timestamptz NULL,
PRIMARY KEY (token, user_id)
);
--- Table for store user sessions.
CREATE TABLE sessions (
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,
user_agent text NULL
);
CREATE INDEX sessions__user_id__idx
ON sessions (user_id);
-- Insert a placeholder system user.
2020-02-04 16:05:51 +01:00
INSERT INTO users (id, fullname, email, photo, password)
VALUES ('00000000-0000-0000-0000-000000000000'::uuid,
'System User',
'system@uxbox.io',
'',
'!');
--- Triggers
CREATE TRIGGER users__modified_at__tgr
BEFORE UPDATE ON users
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();
CREATE TRIGGER user_attrs__modified_at__tgr
BEFORE UPDATE ON user_attrs
FOR EACH ROW EXECUTE PROCEDURE update_modified_at();