From da1363021c117001c3bac8a0e4d791afd3a5f09f Mon Sep 17 00:00:00 2001 From: Saihajpreet Singh Date: Mon, 19 Feb 2024 10:19:06 -0500 Subject: [PATCH] agent: protocol network migration for cost model --- ...2-add-protocol-network-field-cost-model.ts | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 packages/indexer-agent/src/db/migrations/12-add-protocol-network-field-cost-model.ts diff --git a/packages/indexer-agent/src/db/migrations/12-add-protocol-network-field-cost-model.ts b/packages/indexer-agent/src/db/migrations/12-add-protocol-network-field-cost-model.ts new file mode 100644 index 000000000..ec0ff924d --- /dev/null +++ b/packages/indexer-agent/src/db/migrations/12-add-protocol-network-field-cost-model.ts @@ -0,0 +1,61 @@ +import { Logger } from '@graphprotocol/common-ts' +import { caip2IdRegex } from '@graphprotocol/indexer-common' +import { DataTypes, 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.debug(`Checking if 'CostModel' table exists`) + const tables = await queryInterface.showAllTables() + if (!tables.includes('CostModel')) { + logger.info(`Indexing rules table does not exist, migration not necessary`) + return + } + + logger.debug(`Checking if 'CostModel' table needs to be migrated`) + const table = await queryInterface.describeTable('CostModel') + const protocolNetwork = table.protocolNetwork + if (protocolNetwork) { + logger.info( + `'protocolNetwork' column already exist, migration not necessary`, + ) + return + } + + logger.info(`Add 'protocolNetwork' column to 'CostModel' table`) + await queryInterface.addColumn('CostModel', 'protocolNetwork', { + type: DataTypes.STRING, + allowNull: false, + validate: { + is: caip2IdRegex, + }, + }) +} + +export async function down({ context }: Context): Promise { + const { queryInterface, logger } = context + + return await queryInterface.sequelize.transaction({}, async transaction => { + const tables = await queryInterface.showAllTables() + + if (tables.includes('CostModel')) { + logger.info(`Remove 'protocolNetwork' column`) + await context.queryInterface.removeColumn( + 'CostModel', + 'protocolNetwork', + { + transaction, + }, + ) + } + }) +}