Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexer-agent: edit costmodel migration trigger #1008

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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<void> {
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", "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", "variables": "%s" }', NEW.deployment, NEW.model, NEW.variables));
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<void> {
const { queryInterface, logger } = context
logger.info(`Drop function, trigger, indices, and table`)
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;',
)
}
Loading