graphql-engine/server/src-rsr/migrations/39_to_40.sql
Vishnu Bharathi P 58c44f55dd Merge oss/master onto mono/main
GitOrigin-RevId: 1c8c4d60e033c8a0bc8b2beed24c5bceb7d4bcc8
2020-11-12 22:37:19 +05:30

27 lines
1.0 KiB
PL/PgSQL

ALTER TABLE hdb_catalog.event_triggers
DROP CONSTRAINT IF EXISTS event_triggers_schema_name_fkey;
ALTER TABLE hdb_catalog.event_triggers
DROP CONSTRAINT IF EXISTS event_triggers_schema_name_table_name_fkey;
-- since we removed the foreign key constraint with hdb_catalog.hdb_table which had 'ON UPDATE CASCADE'
-- (see Note [Diff-and-patch event triggers on replace] in Hasura.RQL.DDL.EventTrigger), we perform the update using trigger
CREATE OR REPLACE FUNCTION hdb_catalog.event_trigger_table_name_update()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
BEGIN
IF (NEW.table_schema, NEW.table_name) <> (OLD.table_schema, OLD.table_name) THEN
UPDATE hdb_catalog.event_triggers
SET schema_name = NEW.table_schema, table_name = NEW.table_name
WHERE (schema_name, table_name) = (OLD.table_schema, OLD.table_name);
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER event_trigger_table_name_update_trigger
AFTER UPDATE ON hdb_catalog.hdb_table
FOR EACH ROW EXECUTE PROCEDURE hdb_catalog.event_trigger_table_name_update();