From e6c9d3778a617467ca02fa516913df39fc8b896c Mon Sep 17 00:00:00 2001 From: Carlos V Date: Fri, 20 Sep 2024 19:34:49 +0000 Subject: [PATCH 1/2] indexer-agent: edit costmodel migration trigger --- .../migrations/17-edit-cost-models-trigger.ts | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts diff --git a/packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts b/packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts new file mode 100644 index 000000000..62696ee82 --- /dev/null +++ b/packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts @@ -0,0 +1,56 @@ +import { Logger } from '@graphprotocol/common-ts' +import { QueryInterface } from 'sequelize' + +interface MigrationContext { + queryInterface: QueryInterface + logger: Logger +} + +interface Context { + context: MigrationContext +} + +export async function up({ context }: Context): Promise { + const { queryInterface, logger } = context + + logger.info( + 'Deleting old function/trigger for cost models to add the model field', + ) + + const dropFunctionSQL = ` + DROP FUNCTION IF EXISTS cost_models_update_notify() CASCADE; + ` + await queryInterface.sequelize.query(dropFunctionSQL) + + const functionSQL = ` + CREATE FUNCTION cost_models_update_notify() + RETURNS trigger AS + $$ + BEGIN + IF TG_OP = 'DELETE' THEN + PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "DELETE", "deployment": "%s"}', OLD.deployment)); + RETURN OLD; + ELSIF TG_OP = 'INSERT' THEN + PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "INSERT", "deployment": "%s", "model": "%s"}', NEW.deployment, NEW.model)); + RETURN NEW; + ELSE + PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "%s", "deployment": "%s", "model": "%s"}', NEW.deployment, NEW.model)); + RETURN NEW; + END IF; + END; + $$ LANGUAGE 'plpgsql'; + ` + const triggerSQL = ` + CREATE TRIGGER cost_models_update AFTER INSERT OR UPDATE OR DELETE + ON "CostModelsHistory" + FOR EACH ROW EXECUTE PROCEDURE cost_models_update_notify(); + ` + await queryInterface.sequelize.query(functionSQL) + await queryInterface.sequelize.query(triggerSQL) +} + +export async function down({ context }: Context): Promise { + const { queryInterface, logger } = context + logger.info(`Drop function, trigger, indices, and table`) + queryInterface.removeColumn('scalar_tap_receipts_invalid', 'error_log') +} From 5f3d1e88804883820be669ed672a756394d1534a Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 24 Oct 2024 23:19:29 +0200 Subject: [PATCH 2/2] indexer-agent: update trigger and add needed variables Signed-off-by: Gustavo Inacio --- .../src/db/migrations/17-edit-cost-models-trigger.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts b/packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts index 62696ee82..849fbfe7e 100644 --- a/packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts +++ b/packages/indexer-agent/src/db/migrations/17-edit-cost-models-trigger.ts @@ -31,10 +31,10 @@ export async function up({ context }: Context): Promise { PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "DELETE", "deployment": "%s"}', OLD.deployment)); RETURN OLD; ELSIF TG_OP = 'INSERT' THEN - PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "INSERT", "deployment": "%s", "model": "%s"}', NEW.deployment, NEW.model)); + PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "INSERT", "deployment": "%s", "model": "%s", "variables": "%s"}', NEW.deployment, NEW.model, NEW.variables)); RETURN NEW; ELSE - PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "%s", "deployment": "%s", "model": "%s"}', NEW.deployment, NEW.model)); + PERFORM pg_notify('cost_models_update_notification', format('{"tg_op": "%s", "deployment": "%s", "model": "%s", "variables": "%s" }', NEW.deployment, NEW.model, NEW.variables)); RETURN NEW; END IF; END; @@ -52,5 +52,11 @@ export async function up({ context }: Context): Promise { export async function down({ context }: Context): Promise { const { queryInterface, logger } = context logger.info(`Drop function, trigger, indices, and table`) - queryInterface.removeColumn('scalar_tap_receipts_invalid', 'error_log') + await queryInterface.sequelize.query( + 'DROP TRIGGER IF EXISTS cost_models_update ON "CostModelsHistory" CASCADE;', + ) + + await queryInterface.sequelize.query( + 'DROP FUNCTION IF EXISTS cost_models_update_notify() CASCADE;', + ) }