From 489dabf5374667e4a165ac83ca94810482074364 Mon Sep 17 00:00:00 2001 From: tilacog Date: Tue, 18 Jul 2023 17:16:22 -0300 Subject: [PATCH] agent: fix migration: check for previous data when migrating UP --- .../11-add-protocol-network-field.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/indexer-agent/src/db/migrations/11-add-protocol-network-field.ts b/packages/indexer-agent/src/db/migrations/11-add-protocol-network-field.ts index e42f9b84b..724c28ad2 100644 --- a/packages/indexer-agent/src/db/migrations/11-add-protocol-network-field.ts +++ b/packages/indexer-agent/src/db/migrations/11-add-protocol-network-field.ts @@ -92,17 +92,19 @@ const migrationInputs: MigrationInput[] = [ export async function up({ context }: Context): Promise { const { queryInterface, networkSpecifications, logger } = context + const m = new Migration(queryInterface, logger) - // This migration requires that just one network is used - if (networkSpecifications.length !== 1) { + // This migration requires that just one network is used if the database holds previous data. + const hasExistingRows = await m.hasExistingRows(migrationInputs) + if (hasExistingRows && networkSpecifications.length !== 1) { throw new Error( `Migration expects only one network specification, but found ${networkSpecifications.length}. ` + - `Please avoid using other network specifications until this migration completes.`, + `Please avoid using other network specifications until this migration completes. ` + + `Make sure to use the same network specification that matches the data in the database.`, ) } const networkChainId = networkSpecifications[0].networkIdentifier - const m = new Migration(queryInterface, logger) for (const input of migrationInputs) { await m.addPrimaryKeyMigration(input, networkChainId) } @@ -112,14 +114,12 @@ export async function down({ context }: Context): Promise { const { queryInterface, networkSpecifications, logger } = context const m = new Migration(queryInterface, logger) - // This migration requires that just one network is used if the database holds previous - // data. - const hasExistingRows = await m.hasExistingRows(migrationInputs) - if (hasExistingRows && networkSpecifications.length !== 1) { + // This migration requires that just one network is used + if (networkSpecifications.length !== 1) { throw new Error( `Migration expects only one network specification, but found ${networkSpecifications.length}. ` + `Please avoid using other network specifications until this migration completes. ` + - `Make sure to use the same network specification that matches the data in the database.`, + `Make sure to use the same network specification for the network data that will be left in the database.`, ) } const networkChainId = networkSpecifications[0].networkIdentifier