-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent: protocol network migration for cost model
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/indexer-agent/src/db/migrations/12-add-protocol-network-field-cost-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> { | ||
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<void> { | ||
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, | ||
}, | ||
) | ||
} | ||
}) | ||
} |