Files
Assistant/database/migrations/001_bootstrap_creator_authority.sql
2026-08-16 15:22:49 +03:00

17 lines
2.1 KiB
PL/PgSQL

BEGIN;
SET ROLE identity_role;
CREATE TABLE IF NOT EXISTS identity.actors (actor_id TEXT PRIMARY KEY, display_name TEXT NOT NULL, recorded_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE IF NOT EXISTS identity.principals (principal_id TEXT PRIMARY KEY, display_name TEXT NOT NULL, recorded_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE IF NOT EXISTS identity.principal_bindings (actor_id TEXT NOT NULL, principal_id TEXT NOT NULL, active BOOLEAN NOT NULL, recorded_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (actor_id, principal_id));
INSERT INTO identity.actors (actor_id, display_name) VALUES ('domovoy-creator-actor', 'Domovoy Creator') ON CONFLICT (actor_id) DO NOTHING;
INSERT INTO identity.principals (principal_id, display_name) VALUES ('domovoy-creator-principal', 'Domovoy Creator Principal') ON CONFLICT (principal_id) DO NOTHING;
INSERT INTO identity.principal_bindings (actor_id, principal_id, active) VALUES ('domovoy-creator-actor', 'domovoy-creator-principal', TRUE) ON CONFLICT (actor_id, principal_id) DO NOTHING;
RESET ROLE;
SET ROLE authority_role;
CREATE TABLE IF NOT EXISTS authority.scopes (scope_id TEXT PRIMARY KEY, label TEXT NOT NULL, global_scope BOOLEAN NOT NULL, recorded_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE IF NOT EXISTS authority.authority_grants (grant_id TEXT PRIMARY KEY, actor_id TEXT NOT NULL, principal_id TEXT NOT NULL, scope_id TEXT NOT NULL, grant_class TEXT NOT NULL, revision BIGINT NOT NULL, active BOOLEAN NOT NULL, valid_from TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, valid_until TIMESTAMPTZ, CHECK (revision > 0));
INSERT INTO authority.scopes (scope_id, label, global_scope) VALUES ('domovoy-global-scope', 'All Domovoy scopes', TRUE) ON CONFLICT (scope_id) DO NOTHING;
INSERT INTO authority.authority_grants (grant_id, actor_id, principal_id, scope_id, grant_class, revision, active) VALUES ('domovoy-creator-grant', 'domovoy-creator-actor', 'domovoy-creator-principal', 'domovoy-global-scope', 'CREATOR', 1, TRUE) ON CONFLICT (grant_id) DO NOTHING;
RESET ROLE;
COMMIT;