diff --git a/migrations/00001_init.sql b/migrations/00001_init.sql deleted file mode 100644 index 3b1e6a5..0000000 --- a/migrations/00001_init.sql +++ /dev/null @@ -1,3 +0,0 @@ --- +goose Up - --- +goose Down diff --git a/migrations/00001_users.sql b/migrations/00001_users.sql new file mode 100644 index 0000000..09917e0 --- /dev/null +++ b/migrations/00001_users.sql @@ -0,0 +1,15 @@ +-- +goose Up +CREATE TABLE users ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + email text NOT NULL, + given_name text NOT NULL, + family_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +CREATE UNIQUE INDEX users_email_key ON users (lower(email)); + +-- +goose Down +DROP TABLE users; diff --git a/migrations/00002_roles_permissions.sql b/migrations/00002_roles_permissions.sql new file mode 100644 index 0000000..3e256cc --- /dev/null +++ b/migrations/00002_roles_permissions.sql @@ -0,0 +1,39 @@ +-- +goose Up +CREATE TABLE roles ( + id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + name text NOT NULL UNIQUE, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE permissions ( + id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + name text NOT NULL UNIQUE, + description text NOT NULL DEFAULT '', + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE role_permissions ( + role_id integer NOT NULL REFERENCES roles (id) ON DELETE CASCADE, + permission_id integer NOT NULL REFERENCES permissions (id) ON DELETE CASCADE, + created_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (role_id, permission_id) +); + +CREATE INDEX role_permissions_permission_id_idx ON role_permissions (permission_id); + +CREATE TABLE user_roles ( + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + role_id integer NOT NULL REFERENCES roles (id) ON DELETE CASCADE, + created_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (user_id, role_id) +); + +CREATE INDEX user_roles_role_id_idx ON user_roles (role_id); + +-- +goose Down +DROP TABLE user_roles; +DROP TABLE role_permissions; +DROP TABLE permissions; +DROP TABLE roles;