|
| 1 | +import { Logger } from '@graphprotocol/common-ts' |
| 2 | +import { DataTypes, QueryInterface } from 'sequelize' |
| 3 | + |
| 4 | +interface MigrationContext { |
| 5 | + queryInterface: QueryInterface |
| 6 | + logger: Logger |
| 7 | +} |
| 8 | + |
| 9 | +interface Context { |
| 10 | + context: MigrationContext |
| 11 | +} |
| 12 | + |
| 13 | +export async function up({ context }: Context): Promise<void> { |
| 14 | + const { queryInterface, logger } = context |
| 15 | + |
| 16 | + logger.debug(`Checking if indexing rules table exists`) |
| 17 | + const tables = await queryInterface.showAllTables() |
| 18 | + if (!tables.includes('IndexingRules')) { |
| 19 | + logger.info(`Indexing rules table does not exist, migration not necessary`) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + logger.debug(`Checking if 'IndexingRules' table needs to be migrated`) |
| 24 | + const table = await queryInterface.describeTable('IndexingRules') |
| 25 | + const requireSupportedColumn = table.requireSupported |
| 26 | + if (requireSupportedColumn) { |
| 27 | + logger.info( |
| 28 | + `'requireSupported' columns already exist, migration not necessary`, |
| 29 | + ) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + logger.info(`Add 'requireSupported' column to 'IndexingRules' table`) |
| 34 | + await queryInterface.addColumn('IndexingRules', 'requireSupported', { |
| 35 | + type: DataTypes.BOOLEAN, |
| 36 | + defaultValue: true, |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +export async function down({ context }: Context): Promise<void> { |
| 41 | + const { queryInterface, logger } = context |
| 42 | + |
| 43 | + return await queryInterface.sequelize.transaction({}, async transaction => { |
| 44 | + const tables = await queryInterface.showAllTables() |
| 45 | + |
| 46 | + if (tables.includes('IndexingRules')) { |
| 47 | + logger.info(`Remove 'requireSupported' column`) |
| 48 | + await context.queryInterface.removeColumn( |
| 49 | + 'IndexingRules', |
| 50 | + 'requireSupported', |
| 51 | + { transaction }, |
| 52 | + ) |
| 53 | + |
| 54 | + logger.info(`Remove 'enum_IndexingRules_identifierType' custom type`) |
| 55 | + await queryInterface.sequelize.query( |
| 56 | + `delete from pg_type where typname = 'bool_IndexingRules_requireSupported'`, |
| 57 | + ) |
| 58 | + } |
| 59 | + }) |
| 60 | +} |
0 commit comments