2024-07-14 21:26:34 +00:00
|
|
|
-- +goose Up
|
2024-07-24 20:25:36 +00:00
|
|
|
-- +goose StatementBegin
|
|
|
|
CREATE TABLE IF NOT EXISTS users
|
|
|
|
(
|
|
|
|
id serial NOT NULL,
|
|
|
|
username VARCHAR(70) UNIQUE NOT NULL,
|
|
|
|
hashed_pwd VARCHAR(60) NOT NULL,
|
|
|
|
email VARCHAR(70) UNIQUE,
|
2024-08-14 10:36:43 +00:00
|
|
|
role INT NOT NULL DEFAULT 0,
|
|
|
|
expires_at TIMESTAMPTZ NOT NULL,
|
2024-07-24 20:25:36 +00:00
|
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
|
|
|
|
|
|
PRIMARY KEY (id),
|
2024-08-14 10:36:43 +00:00
|
|
|
CHECK (length(username) != 0 AND username = lower(username) AND username = trim(username)),
|
|
|
|
CHECK (length(email) != 0 AND email = lower(email) AND email = trim(email)),
|
2024-07-24 20:25:36 +00:00
|
|
|
CHECK (lower(username) != lower(email)),
|
|
|
|
CHECK (length(hashed_pwd) != 0),
|
|
|
|
CHECK (role BETWEEN 0 AND 3)
|
2024-07-14 21:26:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE INDEX ON users (id);
|
|
|
|
CREATE INDEX ON users (username);
|
|
|
|
CREATE INDEX ON users (email);
|
|
|
|
|
2024-07-24 20:25:36 +00:00
|
|
|
CREATE FUNCTION usr_upd_trg_fn() RETURNS TRIGGER
|
|
|
|
LANGUAGE plpgsql AS
|
|
|
|
$$
|
|
|
|
BEGIN
|
|
|
|
NEW.updated_at = NOW();
|
|
|
|
RETURN NEW;
|
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
CREATE TRIGGER usr_upd_trg
|
|
|
|
BEFORE UPDATE
|
|
|
|
ON users
|
|
|
|
FOR EACH ROW
|
|
|
|
EXECUTE PROCEDURE usr_upd_trg_fn();
|
|
|
|
-- +goose StatementEnd
|
|
|
|
|
2024-07-14 21:26:34 +00:00
|
|
|
-- +goose Down
|
|
|
|
DROP TABLE IF EXISTS users;
|