diff --git a/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/down.sql b/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/down.sql index 86b0a91888..7d7f7900da 100644 --- a/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/down.sql +++ b/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/down.sql @@ -6,6 +6,7 @@ RESTORE ORIGINAL */ create table scheduling_goal ( id integer generated always as identity, + old_id integer, revision integer not null default 0, name text not null, definition text not null, @@ -59,12 +60,85 @@ create trigger update_logging_on_update_scheduling_goal_trigger when (pg_trigger_depth() < 1) execute function update_logging_on_update_scheduling_goal(); +/* +ANALYSIS TABLES +*/ +/* Dropped FKs before data migration first */ +alter table scheduling_goal_analysis_satisfying_activities + drop constraint satisfying_activities_references_scheduling_goal; + +alter table scheduling_goal_analysis_created_activities + drop constraint created_activities_references_scheduling_goal; + +alter table scheduling_goal_analysis + drop constraint scheduling_goal_analysis_references_scheduling_goal; + +alter table scheduling_specification_goals + drop constraint scheduling_spec_goal_definition_exists, + drop constraint scheduling_spec_goal_exists; + +alter table metadata.scheduling_goal_tags +drop constraint scheduling_goal_tags_goal_id_fkey; + +/* +DATA MIGRATION +*/ +-- Goals not on a model spec will not be kept, as the scheduler DB can't get the model id from the plan id +-- Because multiple spec may be using the same goal/goal definition, we have to regenerate the id +with specified_definition(goal_id, goal_revision, model_id, definition, definition_creation) as ( + select gd.goal_id, gd.revision, s.model_id, gd.definition, gd.created_at + from scheduling_model_specification_goals s + left join scheduling_goal_definition gd using (goal_id) + where ((s.goal_revision is not null and s.goal_revision = gd.revision) + or (s.goal_revision is null and gd.revision = (select def.revision + from scheduling_goal_definition def + where def.goal_id = s.goal_id + order by def.revision desc limit 1)))), + new_goal_ids(old_goal_id, new_goal_id) as ( + insert into scheduling_goal(old_id, revision, name, definition, model_id, description, + author, last_modified_by, created_date, modified_date) + select m.id, sd.goal_revision, m.name, sd.definition, sd.model_id, m.description, + m.owner, m.updated_by, m.created_at, greatest(m.updated_at::timestamptz, sd.definition_creation::timestamptz) + from scheduling_goal_metadata m + inner join specified_definition sd on m.id = sd.goal_id + returning old_id, id), + satisfying_acts as ( + update scheduling_goal_analysis_satisfying_activities + set goal_id = ngi.new_goal_id + from new_goal_ids ngi + where goal_id = ngi.old_goal_id), + create_acts as ( + update scheduling_goal_analysis_created_activities + set goal_id = ngi.new_goal_id + from new_goal_ids ngi + where goal_id = ngi.old_goal_id), + analysis as ( + update scheduling_goal_analysis + set goal_id = ngi.new_goal_id + from new_goal_ids ngi + where goal_id = ngi.old_goal_id), + tags as ( + update metadata.scheduling_goal_tags + set goal_id = ngi.new_goal_id + from new_goal_ids ngi + where goal_id = ngi.old_goal_id) + update scheduling_specification_goals + set goal_id = ngi.new_goal_id + from new_goal_ids ngi + where goal_id = ngi.old_goal_id; + +/* +POST DATA MIGRATION TABLE CHANGES +*/ +alter table scheduling_goal drop column old_id; +drop trigger set_timestamp on scheduling_goal_metadata; +drop function scheduling_goal_metadata_set_updated_at(); + /* ANALYSIS TABLES */ /* Dropped FKs are restored first */ alter table scheduling_goal_analysis_satisfying_activities - drop constraint satisfying_activities_references_scheduling_goal, add constraint satisfying_activities_references_scheduling_goal foreign key (goal_id) references scheduling_goal @@ -76,7 +150,6 @@ alter table scheduling_goal_analysis_satisfying_activities drop column goal_revision; alter table scheduling_goal_analysis_created_activities - drop constraint created_activities_references_scheduling_goal, add constraint created_activities_references_scheduling_goal foreign key (goal_id) references scheduling_goal @@ -88,7 +161,6 @@ alter table scheduling_goal_analysis_created_activities drop column goal_revision; alter table scheduling_goal_analysis - drop constraint scheduling_goal_analysis_references_scheduling_goal, add constraint scheduling_goal_analysis_references_scheduling_goal foreign key (goal_id) references scheduling_goal @@ -170,33 +242,6 @@ alter table scheduling_goal_analysis on update cascade on delete cascade; -/* -DATA MIGRATION -*/ --- Goals not on a model spec will not be kept, as the scheduler DB can't get the model id from the plan id --- Because multiple spec may be using the same goal/goal definition, we have to regenerate the id -with specified_definition(goal_id, goal_revision, model_id, definition, definition_creation) as ( - select gd.goal_id, gd.revision, s.model_id, gd.definition, gd.created_at - from scheduling_model_specification_goals s - left join scheduling_goal_definition gd using (goal_id) - where ((s.goal_revision is not null and s.goal_revision = gd.revision) - or (s.goal_revision is null and gd.revision = (select def.revision - from scheduling_goal_definition def - where def.goal_id = s.goal_id - order by def.revision desc limit 1))) -) -insert into scheduling_goal(revision, name, definition, model_id, description, - author, last_modified_by, created_date, modified_date) -select sd.goal_revision, m.name, sd.definition, sd.model_id, m.description, - m.owner, m.updated_by, m.created_at, greatest(m.updated_at::timestamptz, sd.definition_creation::timestamptz) - from scheduling_goal_metadata m - inner join specified_definition sd on m.id = sd.goal_id; -/* -POST DATA MIGRATION TABLE CHANGES -*/ -drop trigger set_timestamp on scheduling_goal_metadata; -drop function scheduling_goal_metadata_set_updated_at(); - /* SCHEDULING SPECIFICATION */ @@ -287,8 +332,6 @@ language plpgsql; alter table scheduling_specification_goals add constraint scheduling_specification_unique_goal_id unique (goal_id), - drop constraint scheduling_spec_goal_definition_exists, - drop constraint scheduling_spec_goal_exists, add constraint scheduling_specification_goals_references_scheduling_goals foreign key (goal_id) references scheduling_goal @@ -330,7 +373,6 @@ TAGS */ drop table metadata.scheduling_goal_definition_tags; alter table metadata.scheduling_goal_tags -drop constraint scheduling_goal_tags_goal_id_fkey, add foreign key (goal_id) references public.scheduling_goal on update cascade on delete cascade; diff --git a/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/up.sql b/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/up.sql index e2e8af38f9..6c9044b940 100644 --- a/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/up.sql +++ b/deployment/hasura/migrations/AerieScheduler/13_versioning_scheduling_goals_conditions/up.sql @@ -160,18 +160,7 @@ alter table scheduling_specification_conditions foreign key (specification_id) references scheduling_specification on update cascade - on delete cascade, - drop constraint scheduling_specification_conditions_references_scheduling_conditions, - add constraint scheduling_specification_condition_exists - foreign key (condition_id) - references scheduling_condition_metadata - on update cascade - on delete restrict, - add constraint scheduling_specification_condition_definition_exists - foreign key (condition_id, condition_revision) - references scheduling_condition_definition - on update cascade - on delete restrict; + on delete cascade; comment on table scheduling_specification_conditions is e'' 'The set of scheduling conditions to be used on a given plan.'; @@ -252,6 +241,19 @@ before update on scheduling_condition_metadata for each row execute function scheduling_condition_metadata_set_updated_at(); +alter table scheduling_specification_conditions + drop constraint scheduling_specification_conditions_references_scheduling_conditions, + add constraint scheduling_specification_condition_exists + foreign key (condition_id) + references scheduling_condition_metadata + on update cascade + on delete restrict, + add constraint scheduling_specification_condition_definition_exists + foreign key (condition_id, condition_revision) + references scheduling_condition_definition + on update cascade + on delete restrict; + /* DROP ORIGINAL */ @@ -360,29 +362,6 @@ create trigger scheduling_goal_definition_set_revision for each row execute function scheduling_goal_definition_set_revision(); - -/* -TAGS -*/ -alter table metadata.scheduling_goal_tags -drop constraint scheduling_goal_tags_goal_id_fkey, -add foreign key (goal_id) references public.scheduling_goal_metadata - on update cascade - on delete cascade; - -create table metadata.scheduling_goal_definition_tags ( - goal_id integer not null, - goal_revision integer not null, - tag_id integer not null, - primary key (goal_id, goal_revision, tag_id), - foreign key (goal_id, goal_revision) references scheduling_goal_definition - on update cascade - on delete cascade -); - -comment on table metadata.scheduling_goal_definition_tags is e'' - 'The tags associated with a specific scheduling condition definition.'; - /* SPECIFICATIONS */ @@ -538,17 +517,6 @@ alter table scheduling_specification_goals references scheduling_specification on update cascade on delete cascade, - drop constraint scheduling_specification_goals_references_scheduling_goals, - add constraint scheduling_spec_goal_exists - foreign key (goal_id) - references scheduling_goal_metadata - on update cascade - on delete restrict, - add constraint scheduling_spec_goal_definition_exists - foreign key (goal_id, goal_revision) - references scheduling_goal_definition - on update cascade - on delete restrict, drop constraint scheduling_specification_unique_goal_id; comment on table scheduling_specification_goals is e'' @@ -705,6 +673,19 @@ POST DATA MIGRATION TABLE CHANGES alter table scheduling_goal_metadata alter column id set generated always; +alter table scheduling_specification_goals + drop constraint scheduling_specification_goals_references_scheduling_goals, + add constraint scheduling_spec_goal_exists + foreign key (goal_id) + references scheduling_goal_metadata + on update cascade + on delete restrict, + add constraint scheduling_spec_goal_definition_exists + foreign key (goal_id, goal_revision) + references scheduling_goal_definition + on update cascade + on delete restrict; + create function scheduling_goal_metadata_set_updated_at() returns trigger security definer @@ -718,6 +699,28 @@ before update on scheduling_goal_metadata for each row execute function scheduling_goal_metadata_set_updated_at(); +/* +TAGS +*/ +alter table metadata.scheduling_goal_tags +drop constraint scheduling_goal_tags_goal_id_fkey, +add foreign key (goal_id) references public.scheduling_goal_metadata + on update cascade + on delete cascade; + +create table metadata.scheduling_goal_definition_tags ( + goal_id integer not null, + goal_revision integer not null, + tag_id integer not null, + primary key (goal_id, goal_revision, tag_id), + foreign key (goal_id, goal_revision) references scheduling_goal_definition + on update cascade + on delete cascade +); + +comment on table metadata.scheduling_goal_definition_tags is e'' + 'The tags associated with a specific scheduling condition definition.'; + /* SCHEDULING REQUEST */