Files
threshold/internal/db/migrations/postgres/06_iam.up.sql
T

233 lines
5.9 KiB
PL/PgSQL
Raw Normal View History

2020-05-27 17:35:04 -04:00
begin;
2020-05-27 17:35:04 -04:00
create table iam_scope_type_enm (
string text not null primary key check(string in ('unknown', 'organization', 'project'))
);
2020-05-27 17:35:04 -04:00
insert into iam_scope_type_enm (string)
values
('unknown'),
('organization'),
('project');
2020-05-27 17:35:04 -04:00
create table iam_scope (
public_id wt_public_id primary key,
2020-05-13 10:40:57 -04:00
create_time wt_timestamp,
update_time wt_timestamp,
name text,
2020-05-27 17:35:04 -04:00
type text not null references iam_scope_type_enm(string) check(
(
type = 'organization'
2020-05-27 17:35:04 -04:00
and parent_id = null
)
or (
type = 'project'
2020-05-27 17:35:04 -04:00
and parent_id is not null
)
),
description text,
2020-05-27 17:35:04 -04:00
parent_id text references iam_scope(public_id) on delete cascade on update cascade
);
2020-05-27 17:35:04 -04:00
create table iam_scope_organization (
2020-05-27 17:35:04 -04:00
scope_id wt_public_id not null unique references iam_scope(public_id) on delete cascade on update cascade,
name text unique,
primary key(scope_id)
);
2020-05-27 17:35:04 -04:00
create table iam_scope_project (
2020-05-27 17:35:04 -04:00
scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade,
parent_id wt_public_id not null references iam_scope_organization(scope_id) on delete cascade on update cascade,
name text,
unique(parent_id, name),
primary key(scope_id, parent_id)
);
2020-05-27 17:35:04 -04:00
create or replace function
iam_sub_scopes_func()
returns trigger
as $$
declare parent_type int;
begin
if new.type = 'organization' then
insert into iam_scope_organization (scope_id, name)
values
(new.public_id, new.name);
return new;
end if;
if new.type = 'project' then
insert into iam_scope_project (scope_id, parent_id, name)
values
(new.public_id, new.parent_id, new.name);
return new;
end if;
raise exception 'unknown scope type';
end;
$$ language plpgsql;
2020-05-27 17:35:04 -04:00
create trigger
iam_scope_insert
after
insert on iam_scope
for each row execute procedure iam_sub_scopes_func();
2020-05-27 17:35:04 -04:00
create or replace function
iam_immutable_scope_type_func()
returns trigger
as $$
declare parent_type int;
begin
if new.type != old.type then
raise exception 'scope type cannot be updated';
end if;
return new;
end;
$$ language plpgsql;
2020-05-27 17:35:04 -04:00
create trigger
iam_scope_update
before
update on iam_scope
for each row execute procedure iam_immutable_scope_type_func();
2020-05-27 17:35:04 -04:00
create trigger
update_time_column
before update on iam_scope
for each row execute procedure update_time_column();
2020-05-27 17:35:04 -04:00
create trigger
immutable_create_time
before
update on iam_scope
for each row execute procedure immutable_create_time_func();
create trigger
default_create_time_column
before
insert on iam_scope
for each row execute procedure default_create_time();
2020-05-31 19:55:56 -04:00
-- iam_sub_names will allow us to enforce the different name constraints for
-- organizations and projects via a before update trigger on the iam_scope
-- table.
create or replace function
iam_sub_names()
returns trigger
as $$
begin
if new.name != old.name then
if new.type = 'organization' then
update iam_scope_organization set name = new.name where scope_id = old.public_id;
return new;
end if;
if new.type = 'project' then
update iam_scope_project set name = new.name where scope_id = old.public_id;
return new;
end if;
raise exception 'unknown scope type';
end if;
return new;
end;
$$ language plpgsql;
create trigger
iam_sub_names
before
update on iam_scope
for each row execute procedure iam_sub_names();
2020-06-04 20:05:08 -04:00
create table iam_user (
public_id wt_public_id not null primary key,
create_time wt_timestamp,
update_time wt_timestamp,
name text,
description text,
scope_id wt_public_id not null references iam_scope_organization(scope_id) on delete cascade on update cascade,
unique(name, scope_id),
disabled boolean not null default false,
-- The order of columns is important for performance. See:
-- https://dba.stackexchange.com/questions/58970/enforcing-constraints-two-tables-away/58972#58972
-- https://dba.stackexchange.com/questions/27481/is-a-composite-index-also-good-for-queries-on-the-first-field
unique(scope_id, public_id)
2020-06-04 20:05:08 -04:00
);
create trigger
update_time_column
before update on iam_user
for each row execute procedure update_time_column();
create trigger
immutable_create_time
before
update on iam_user
for each row execute procedure immutable_create_time_func();
create trigger
default_create_time_column
before
insert on iam_user
for each row execute procedure default_create_time();
2020-06-15 12:42:08 -04:00
create table iam_role (
public_id wt_public_id not null primary key,
create_time wt_timestamp,
update_time wt_timestamp,
name text,
description text,
scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade,
unique(name, scope_id),
disabled boolean not null default false
);
create trigger
update_time_column
before update on iam_role
for each row execute procedure update_time_column();
create trigger
immutable_create_time
before
update on iam_role
for each row execute procedure immutable_create_time_func();
create trigger
default_create_time_column
before
insert on iam_role
for each row execute procedure default_create_time();
2020-06-04 20:05:08 -04:00
2020-06-11 16:36:47 -04:00
create table iam_group (
public_id wt_public_id not null primary key,
create_time wt_timestamp,
update_time wt_timestamp,
name text,
description text,
scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade,
unique(name, scope_id),
disabled boolean not null default false
);
create trigger
update_time_column
before update on iam_group
for each row execute procedure update_time_column();
create trigger
immutable_create_time
before
update on iam_group
for each row execute procedure immutable_create_time_func();
create trigger
default_create_time_column
before
insert on iam_group
for each row execute procedure default_create_time();
2020-06-15 12:42:08 -04:00
2020-05-27 17:35:04 -04:00
commit;