ms-auth/migrations/20240608163806_initial.sql

35 lines
900 B
MySQL
Raw Normal View History

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
(
2024-12-30 15:04:26 +00:00
id serial NOT NULL,
username VARCHAR(70) UNIQUE NOT NULL,
hashed_pwd VARCHAR(60) NOT NULL,
role INT NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
modified_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
2024-07-24 20:25:36 +00:00
PRIMARY KEY (id),
2024-08-14 10:36:43 +00:00
CHECK (length(username) != 0 AND username = lower(username) AND username = trim(username)),
2024-07-24 20:25:36 +00:00
CHECK (length(hashed_pwd) != 0),
2024-12-30 15:04:26 +00:00
CHECK (role BETWEEN 0 AND 2)
2024-07-14 21:26:34 +00:00
);
2024-12-30 15:04:26 +00:00
CREATE FUNCTION modified_at_update() RETURNS TRIGGER
2024-07-24 20:25:36 +00:00
LANGUAGE plpgsql AS
$$
BEGIN
2024-12-30 15:04:26 +00:00
NEW.modified_at = NOW();
2024-07-24 20:25:36 +00:00
RETURN NEW;
END;
$$;
2024-12-30 15:04:26 +00:00
CREATE TRIGGER on_users_update
2024-07-24 20:25:36 +00:00
BEFORE UPDATE
ON users
FOR EACH ROW
2024-12-30 15:04:26 +00:00
EXECUTE PROCEDURE modified_at_update();
2024-07-24 20:25:36 +00:00
-- +goose StatementEnd
2024-07-14 21:26:34 +00:00
-- +goose Down
DROP TABLE IF EXISTS users;