diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/migrations/0002_populate.py b/SAS/TMSS/backend/src/tmss/tmssapp/migrations/0002_populate.py index 92baffd4c15a8c025d234eeffed61ae9f443fabf..cc01b09d6659e81e82e1001073ba00d044e1aa95 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/migrations/0002_populate.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/migrations/0002_populate.py @@ -14,9 +14,24 @@ class Migration(migrations.Migration): ('tmssapp', '0001_initial'), ] - # Start SubTask id with 2 000 000 to avoid overlap with 'old' (test/production) OTDB - operations = [ migrations.RunSQL('ALTER SEQUENCE tmssapp_SubTask_id_seq RESTART WITH 2000000;'), - migrations.RunPython(populate_choices), + + operations = [ migrations.RunSQL('ALTER SEQUENCE tmssapp_SubTask_id_seq RESTART WITH 2000000;'), # Start SubTask id with 2 000 000 to avoid overlap with 'old' (test/production) OTDB + migrations.RunPython(populate_choices), # poppulates the "fixed" enums, with amongst others the subtask_states which are used next in the trigger + migrations.RunSQL("CREATE OR REPLACE FUNCTION tmssapp_check_subtask_state_transition() \ + RETURNS trigger AS \ + $BODY$ \ + BEGIN \ + IF OLD.state_id='defining' AND NEW.state_id != 'defined' THEN \ + RAISE EXCEPTION 'ILLEGAL SUBTASK STATE TRANSITION FROM % TO %', OLD.state_id, NEW.state_id; \ + END IF; \ + RETURN NEW; \ + END; \ + $BODY$ \ + LANGUAGE plpgsql VOLATILE; \ + DROP TRIGGER IF EXISTS tmssapp_trigger_on_check_subtask_state_transition ON tmssapp_SubTask ; \ + CREATE TRIGGER tmssapp_trigger_on_check_subtask_state_transition \ + AFTER UPDATE ON tmssapp_SubTask \ + FOR EACH ROW EXECUTE PROCEDURE tmssapp_check_subtask_state_transition();"), migrations.RunPython(populate_settings), migrations.RunPython(populate_misc), migrations.RunPython(populate_resources),