|
| 1 | +create table merlin.model_derivation_group ( |
| 2 | + model_id integer not null, |
| 3 | + derivation_group_name text not null, |
| 4 | + |
| 5 | + constraint model_derivation_group_pkey |
| 6 | + primary key (model_id, derivation_group_name), |
| 7 | + constraint mdg_model_exists |
| 8 | + foreign key (model_id) |
| 9 | + references merlin.mission_model(id) |
| 10 | + on delete cascade, |
| 11 | + constraint mdg_derivation_group_exists |
| 12 | + foreign key (derivation_group_name) |
| 13 | + references merlin.derivation_group(name) |
| 14 | + on update cascade |
| 15 | + on delete restrict |
| 16 | +); |
| 17 | + |
| 18 | +comment on table merlin.model_derivation_group is e'' |
| 19 | + 'The default set of derivation groups that any new plans created using this model would have linked to them.'; |
| 20 | + |
| 21 | +comment on column merlin.model_derivation_group.model_id is e'' |
| 22 | + 'The model with which the derivation group is associated.'; |
| 23 | +comment on column merlin.model_derivation_group.derivation_group_name is e'' |
| 24 | + 'The derivation group being associated with the model.'; |
| 25 | + |
| 26 | + |
| 27 | +create function merlin.populate_derivation_groups_new_plan() |
| 28 | +returns trigger |
| 29 | +language plpgsql as $$ |
| 30 | +begin |
| 31 | + insert into merlin.plan_derivation_group (plan_id, derivation_group_name) |
| 32 | + select new.id, mdg.derivation_group_name |
| 33 | + from merlin.model_derivation_group mdg |
| 34 | + where mdg.model_id = new.model_id; |
| 35 | + return new; |
| 36 | +end; |
| 37 | +$$; |
| 38 | + |
| 39 | +comment on function merlin.populate_derivation_groups_new_plan() is e'' |
| 40 | +'Populates the plan''s derivation group associations with the contents of its model''s derivation group associations.'; |
| 41 | + |
| 42 | +create trigger populate_derivation_groups_new_plan_trigger |
| 43 | +after insert on merlin.plan |
| 44 | +for each row |
| 45 | +execute function merlin.populate_derivation_groups_new_plan(); |
| 46 | + |
| 47 | +call migrations.mark_migration_applied('35'); |
0 commit comments