Skip to content

Commit

Permalink
fix: alteration
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyYe committed Nov 7, 2024
1 parent 1f88a29 commit 87d77d1
Showing 1 changed file with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,50 @@ import type { AlterationScript } from '../lib/types/alteration.js';
const alteration: AlterationScript = {
up: async (pool) => {
await pool.query(sql`
alter type application_type add value if not exists 'SAML';
`);
alter type application_type add value 'SAML';
await pool.query(sql`
alter table applications
add constraint check_saml_app_third_party_consistency
check (type != 'SAML' OR (type = 'SAML' AND is_third_party = true));
alter table applications
add constraint check_saml_app_third_party_consistency
check (type != 'SAML' OR (type = 'SAML' AND is_third_party = true));
`);
},
down: async (pool) => {
await pool.query(sql`
alter table applications
drop constraint if exists check_saml_app_third_party_consistency;
alter table organization_application_relations drop constraint application_type;
alter table application_secrets drop constraint application_type;
alter table sso_connector_idp_initiated_auth_configs drop constraint application_type;
delete from applications where type = 'SAML';
`);
drop function check_application_type;
await pool.query(sql`
create type application_type_new as enum ('Native', 'SPA', 'Traditional', 'MachineToMachine', 'Protected');
delete from applications where "type"='SAML';
alter table applications
alter column "type" type application_type_new
using ("type"::text::application_type_new);
drop type application_type;
alter type application_type_new rename to application_type;
alter table applications
alter column type type application_type_new
using type::text::application_type_new;
alter table applications drop constraint check_saml_app_third_party_consistency;
drop type application_type;
create function check_application_type(
application_id varchar(21),
variadic target_type application_type[]
) returns boolean as
$$ begin
return (select type from applications where id = application_id) = any(target_type);
end; $$ language plpgsql set search_path = public;
alter type application_type_new rename to application_type;
alter table organization_application_relations
add constraint application_type
check (check_application_type(application_id, 'MachineToMachine'));
alter table application_secrets
add constraint application_type
check (check_application_type(application_id, 'MachineToMachine', 'Traditional', 'Protected'));
alter table sso_connector_idp_initiated_auth_configs
add constraint application_type
check (check_application_type(default_application_id, 'Traditional', 'SPA'));
`);
},
};
Expand Down

0 comments on commit 87d77d1

Please sign in to comment.