From c94399123c8751317b3e08b517aac90ebfc8a781 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Wed, 12 Jun 2024 17:56:08 -0400 Subject: [PATCH] fixed seed migration when duplicates are present --- ...40612062819-seed-wasm-codes-from-config.ts | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/db/migrations/20240612062819-seed-wasm-codes-from-config.ts b/src/db/migrations/20240612062819-seed-wasm-codes-from-config.ts index 024fc235..2d6424fa 100644 --- a/src/db/migrations/20240612062819-seed-wasm-codes-from-config.ts +++ b/src/db/migrations/20240612062819-seed-wasm-codes-from-config.ts @@ -1,24 +1,38 @@ -import { QueryInterface } from 'sequelize' +import { QueryInterface, Sequelize } from 'sequelize' import { WasmCodeService } from '@/services/wasm-codes' module.exports = { - async up(queryInterface: QueryInterface) { + async up(queryInterface: QueryInterface, sequelize: Sequelize) { // loads from config automatically const codes = (await WasmCodeService.setUpInstance()).getWasmCodes() - await queryInterface.bulkInsert( - 'WasmCodeKeys', - codes.map(({ codeKey }) => ({ codeKey })) - ) - await queryInterface.bulkInsert( - 'WasmCodeKeyIds', - codes.flatMap(({ codeKey, codeIds }) => - codeIds.map((codeKeyId) => ({ - codeKey, - codeKeyId, - })) - ) + await Promise.all( + codes.map(async ({ codeKey, codeIds }) => { + await queryInterface.upsert( + 'WasmCodeKeys', + { codeKey }, + { codeKey }, + { codeKey }, + { + model: sequelize.models.WasmCodeKey, + } + ) + + await Promise.all( + codeIds.map((codeKeyId) => + queryInterface.upsert( + 'WasmCodeKeyIds', + { codeKey, codeKeyId }, + { codeKey, codeKeyId }, + { codeKey, codeKeyId }, + { + model: sequelize.models.WasmCodeKeyId, + } + ) + ) + ) + }) ) }, async down(queryInterface: QueryInterface) {