From 1b1ae987143672dd24ddc4afb55ed73f514bbce9 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Fri, 3 May 2019 22:40:05 +0300 Subject: [PATCH] Third time's the charm --- ...12501-MigrateUsernamesToNewHistoryTable.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/migrations/1556909512501-MigrateUsernamesToNewHistoryTable.ts b/src/migrations/1556909512501-MigrateUsernamesToNewHistoryTable.ts index 3079a37a..d9e003c3 100644 --- a/src/migrations/1556909512501-MigrateUsernamesToNewHistoryTable.ts +++ b/src/migrations/1556909512501-MigrateUsernamesToNewHistoryTable.ts @@ -38,15 +38,22 @@ export class MigrateUsernamesToNewHistoryTable1556909512501 implements Migration stream.on("end", async () => { if (toInsert.length || toDelete.length) { await queryRunner.query("START TRANSACTION"); - await queryRunner.query( - "INSERT INTO username_history (user_id, username, timestamp) VALUES " + - Array.from({ length: toInsert.length }, () => "(?, ?, ?)").join(","), - toInsert.flat(), - ); - await queryRunner.query( - "DELETE FROM name_history WHERE id IN (" + Array.from("?".repeat(toDelete.length)).join(", ") + ")", - toDelete, - ); + + if (toInsert.length) { + await queryRunner.query( + "INSERT INTO username_history (user_id, username, timestamp) VALUES " + + Array.from({ length: toInsert.length }, () => "(?, ?, ?)").join(","), + toInsert.flat(), + ); + } + + if (toDelete.length) { + await queryRunner.query( + "DELETE FROM name_history WHERE id IN (" + Array.from("?".repeat(toDelete.length)).join(", ") + ")", + toDelete, + ); + } + await queryRunner.query("COMMIT"); resolve({ finished: false, migrated: toInsert.length });