|
| 1 | +CREATE SCHEMA IF NOT EXISTS admin; |
| 2 | + |
| 3 | +CREATE TABLE admin.organizations ( |
| 4 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 5 | + name varchar NOT NULL, |
| 6 | + external_id varchar NOT NULL, |
| 7 | + created_at timestamp DEFAULT now() NOT NULL, |
| 8 | + modified_at timestamp, |
| 9 | + deleted_at timestamp, |
| 10 | + CONSTRAINT organizations_pkey PRIMARY KEY (id) |
| 11 | +); |
| 12 | +CREATE UNIQUE INDEX uq_organizations_external_id_active ON admin.organizations (external_id) WHERE deleted_at IS NULL; |
| 13 | +CREATE INDEX ix_organizations_external_id ON admin.organizations (external_id); |
| 14 | + |
| 15 | +CREATE TABLE admin.clients ( |
| 16 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 17 | + organization_id uuid NOT NULL, |
| 18 | + name varchar(100) NOT NULL, |
| 19 | + description varchar(255), |
| 20 | + created_at timestamp DEFAULT now() NOT NULL, |
| 21 | + modified_at timestamp, |
| 22 | + deleted_at timestamp, |
| 23 | + CONSTRAINT clients_pkey PRIMARY KEY (id), |
| 24 | + CONSTRAINT clients_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES admin.organizations (id) |
| 25 | +); |
| 26 | + |
| 27 | +CREATE TABLE admin.certificates ( |
| 28 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 29 | + organization_id uuid NOT NULL, |
| 30 | + organization_identifier varchar(100), |
| 31 | + domain varchar(100), |
| 32 | + created_at timestamp DEFAULT now() NOT NULL, |
| 33 | + modified_at timestamp, |
| 34 | + deleted_at timestamp, |
| 35 | + CONSTRAINT certificates_pkey PRIMARY KEY (id), |
| 36 | + CONSTRAINT fk_certificates_organizations FOREIGN KEY (organization_id) REFERENCES admin.organizations (id) |
| 37 | +); |
| 38 | +CREATE UNIQUE INDEX uq_organization_id_organization_identifier_domain ON admin.certificates (organization_id, organization_identifier, domain) WHERE deleted_at IS NULL; |
| 39 | + |
| 40 | +CREATE TABLE admin.sources ( |
| 41 | + id uuid DEFAULT gen_random_uuid() NOT NULL, |
| 42 | + organization_id uuid NOT NULL, |
| 43 | + source_id varchar(100) NOT NULL, |
| 44 | + name varchar(150) NOT NULL, |
| 45 | + created_at timestamp DEFAULT now() NOT NULL, |
| 46 | + modified_at timestamp, |
| 47 | + deleted_at timestamp, |
| 48 | + CONSTRAINT sources_pkey PRIMARY KEY (id), |
| 49 | + CONSTRAINT fk_sources_organizations FOREIGN KEY (organization_id) REFERENCES admin.organizations (id) |
| 50 | +); |
| 51 | +CREATE UNIQUE INDEX uq_sources_source_id_active ON admin.sources (organization_id, source_id) WHERE deleted_at IS NULL; |
| 52 | + |
| 53 | +CREATE TABLE admin.scopes ( |
| 54 | + id integer GENERATED ALWAYS AS IDENTITY, |
| 55 | + name varchar(50), |
| 56 | + created_at timestamp DEFAULT now() NOT NULL, |
| 57 | + CONSTRAINT scopes_pkey PRIMARY KEY (id), |
| 58 | + CONSTRAINT scopes_name_key UNIQUE (name) |
| 59 | +); |
| 60 | + |
| 61 | +INSERT INTO admin.scopes (name) VALUES |
| 62 | + ('CREATE'), |
| 63 | + ('DELETE'), |
| 64 | + ('READ'), |
| 65 | + ('LOCALIZE'); |
| 66 | + |
| 67 | +CREATE TABLE admin.organizations_scopes ( |
| 68 | + organization_id uuid NOT NULL, |
| 69 | + scope_id integer NOT NULL, |
| 70 | + created_at timestamp DEFAULT now() NOT NULL, |
| 71 | + CONSTRAINT pk_organizations_scopes PRIMARY KEY (organization_id, scope_id), |
| 72 | + CONSTRAINT fk_organization_scopes_organization FOREIGN KEY (organization_id) REFERENCES admin.organizations (id) ON DELETE CASCADE, |
| 73 | + CONSTRAINT fk_organization_scopes_scopes FOREIGN KEY (scope_id) REFERENCES admin.scopes (id) |
| 74 | +); |
| 75 | + |
| 76 | +CREATE TABLE admin.clients_scopes ( |
| 77 | + client_id uuid NOT NULL, |
| 78 | + organization_id uuid NOT NULL, |
| 79 | + scope_id integer NOT NULL, |
| 80 | + created_at timestamp DEFAULT now() NOT NULL, |
| 81 | + CONSTRAINT pk_clients_scopes PRIMARY KEY (client_id, organization_id, scope_id), |
| 82 | + CONSTRAINT fk_clients_scopes_clients FOREIGN KEY (client_id) REFERENCES admin.clients (id) ON DELETE CASCADE, |
| 83 | + CONSTRAINT fk_clients_scopes_scopes FOREIGN KEY (organization_id, scope_id) REFERENCES admin.organizations_scopes (organization_id, scope_id) |
| 84 | +); |
| 85 | + |
| 86 | +CREATE TABLE admin.clients_certificates ( |
| 87 | + client_id uuid NOT NULL, |
| 88 | + certificate_id uuid NOT NULL, |
| 89 | + created_at timestamp DEFAULT now() NOT NULL, |
| 90 | + CONSTRAINT pk_clients_certificates PRIMARY KEY (client_id, certificate_id), |
| 91 | + CONSTRAINT fk_clients_certificates_clients FOREIGN KEY (client_id) REFERENCES admin.clients (id) ON DELETE CASCADE, |
| 92 | + CONSTRAINT fk_clients_certificates_certificates FOREIGN KEY (certificate_id) REFERENCES admin.certificates (id) ON DELETE CASCADE |
| 93 | +); |
| 94 | + |
| 95 | +CREATE TABLE admin.clients_sources ( |
| 96 | + client_id uuid NOT NULL, |
| 97 | + source_id uuid NOT NULL, |
| 98 | + CONSTRAINT pk_sources_clients PRIMARY KEY (source_id, client_id), |
| 99 | + CONSTRAINT fk_clients_sources_clients FOREIGN KEY (client_id) REFERENCES admin.clients (id) ON DELETE CASCADE, |
| 100 | + CONSTRAINT fk_clients_sources_sources FOREIGN KEY (source_id) REFERENCES admin.sources (id) ON DELETE CASCADE |
| 101 | +); |
0 commit comments