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, + }, + ) + } + }) +}