cargagep-v2/db/migrations/20260823153300_create_users.sql
2026-08-23 17:47:31 +02:00

28 lines
947 B
SQL

-- migrate:up
-- Users come from the OIDC provider (Whiskey): `oidc_sub` is the stable identity,
-- and a user row is created/refreshed on every login (see `upsert_user`).
CREATE TABLE users (
id SERIAL PRIMARY KEY,
external_id TEXT UNIQUE,
firstname TEXT NOT NULL,
"name" TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
oidc_sub TEXT NOT NULL UNIQUE,
admin BOOLEAN NOT NULL DEFAULT FALSE
);
-- The units a user belongs to. The list is provided by Whiskey, so the unit
-- identifiers are plain text rather than a fixed enum or a reference table:
-- a unit that appears in the provider must not break a login.
CREATE TABLE units_users (
user_id INTEGER NOT NULL REFERENCES users (id) ON DELETE CASCADE,
unit_name TEXT NOT NULL,
PRIMARY KEY (user_id, unit_name)
);
CREATE INDEX units_users_unit_name_idx ON units_users (unit_name);
-- migrate:down
DROP TABLE IF EXISTS units_users;
DROP TABLE IF EXISTS users;