From ad8108c11f1e63dcdc1bd88a0538459b6f0cb779 Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Sat, 23 Nov 2024 11:03:50 -0300 Subject: [PATCH] Ticket CV2-5653 [WIP]: Adding migration related to last commit --- ...5242_add_trigger_to_relationships_table.rb | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 db/migrate/20241123135242_add_trigger_to_relationships_table.rb diff --git a/db/migrate/20241123135242_add_trigger_to_relationships_table.rb b/db/migrate/20241123135242_add_trigger_to_relationships_table.rb new file mode 100644 index 000000000..af52d9ea7 --- /dev/null +++ b/db/migrate/20241123135242_add_trigger_to_relationships_table.rb @@ -0,0 +1,37 @@ +class AddTriggerToRelationshipsTable < ActiveRecord::Migration[6.1] + def up + # Create the trigger function + execute <<~SQL + CREATE OR REPLACE FUNCTION validate_relationships() + RETURNS TRIGGER AS $$ + BEGIN + -- Check if source_id exists as a target_id + IF EXISTS (SELECT 1 FROM relationships WHERE target_id = NEW.source_id) THEN + RAISE EXCEPTION 'source_id % already exists as a target_id', NEW.source_id; + END IF; + + -- Check if target_id exists as a source_id + IF EXISTS (SELECT 1 FROM relationships WHERE source_id = NEW.target_id) THEN + RAISE EXCEPTION 'target_id % already exists as a source_id', NEW.target_id; + END IF; + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + SQL + + # Attach the trigger to the table + execute <<~SQL + CREATE TRIGGER enforce_relationships + BEFORE INSERT OR UPDATE ON relationships + FOR EACH ROW + EXECUTE FUNCTION validate_relationships(); + SQL + end + + def down + # Remove the trigger and function if rolling back + execute "DROP TRIGGER IF EXISTS enforce_relationships ON relationships;" + execute "DROP FUNCTION IF EXISTS validate_relationships();" + end +end