From f39d47595e1cb6d380b9271226faf11a9fbe0030 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Wed, 26 Jun 2024 14:55:18 -0700 Subject: [PATCH 1/7] Raw sql query fix --- .../2024.06.07T00.36.14.clean-user-group-names.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts index 82d9fa9b..f0b7b524 100644 --- a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts +++ b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts @@ -1,12 +1,9 @@ import type { Migration } from "@/db/umzug" import { UserGroup } from "@/models" -export const up: Migration = async () => { - await UserGroup.findEach(async (userGroup) => { - await userGroup.update({ - name: userGroup.name.trim().replace(/\s+/g, " "), - }) - }) +export const up: Migration = async ({ context: queryInterface }) => { + // https://stackoverflow.com/questions/6940646/mysql-how-to-remove-double-or-more-spaces-from-a-string + await queryInterface.sequelize.query(`UPDATE user_groups SET name = REPLACE(REPLACE(REPLACE(TRIM(name), ' ', '<>'), '><', ''), '<>', ' ');`); } export const down: Migration = async () => { From 69c4b254f6f7a5b755372b574533be47d3d51126 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Thu, 27 Jun 2024 01:22:11 -0700 Subject: [PATCH 2/7] Fixed clean user group names update migration --- .../2024.06.07T00.36.14.clean-user-group-names.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts index f0b7b524..8aed36d7 100644 --- a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts +++ b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts @@ -1,9 +1,16 @@ import type { Migration } from "@/db/umzug" -import { UserGroup } from "@/models" export const up: Migration = async ({ context: queryInterface }) => { - // https://stackoverflow.com/questions/6940646/mysql-how-to-remove-double-or-more-spaces-from-a-string - await queryInterface.sequelize.query(`UPDATE user_groups SET name = REPLACE(REPLACE(REPLACE(TRIM(name), ' ', '<>'), '><', ''), '<>', ' ');`); + await queryInterface.sequelize.transaction(async (transaction) => { + const [rows]: any[] = await queryInterface.sequelize.query('SELECT name from user_groups', { transaction }); + for (const row of rows) { + const trimmedName = row.name.trim().replace(/\s+/g, " ") + await queryInterface.sequelize.query('UPDATE user_groups SET name = ? WHERE id = ?', { + replacements: [trimmedName, row.id], + transaction + }); + } + }); } export const down: Migration = async () => { From 057f377560b9d700339422eb3670b09e0ba95bf4 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Fri, 28 Jun 2024 13:27:59 -0700 Subject: [PATCH 3/7] Fix for PR review --- ...024.06.07T00.36.14.clean-user-group-names.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts index 8aed36d7..717e1c35 100644 --- a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts +++ b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts @@ -1,16 +1,13 @@ import type { Migration } from "@/db/umzug" export const up: Migration = async ({ context: queryInterface }) => { - await queryInterface.sequelize.transaction(async (transaction) => { - const [rows]: any[] = await queryInterface.sequelize.query('SELECT name from user_groups', { transaction }); - for (const row of rows) { - const trimmedName = row.name.trim().replace(/\s+/g, " ") - await queryInterface.sequelize.query('UPDATE user_groups SET name = ? WHERE id = ?', { - replacements: [trimmedName, row.id], - transaction - }); - } - }); + const [rows]: any[] = await queryInterface.sequelize.query('SELECT name from user_groups'); + for (const row of rows) { + const trimmedName = row.name.trim().replace(/\s+/g, " ") + await queryInterface.sequelize.query('UPDATE user_groups SET name = ? WHERE id = ?', { + replacements: [trimmedName, row.id] + }); + } } export const down: Migration = async () => { From c6818dd76decde6443badcaba2ff3faf818ba0e7 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Fri, 28 Jun 2024 14:03:52 -0700 Subject: [PATCH 4/7] Fix for the fix --- .../2024.06.07T00.36.14.clean-user-group-names.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts index 717e1c35..e47a205a 100644 --- a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts +++ b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts @@ -1,7 +1,11 @@ import type { Migration } from "@/db/umzug" +import { QueryTypes } from "sequelize"; export const up: Migration = async ({ context: queryInterface }) => { - const [rows]: any[] = await queryInterface.sequelize.query('SELECT name from user_groups'); + const [rows] = await queryInterface.sequelize.query<{ id: number, name: string }[]>('SELECT name from user_groups', { + type: QueryTypes.SELECT, + }); + for (const row of rows) { const trimmedName = row.name.trim().replace(/\s+/g, " ") await queryInterface.sequelize.query('UPDATE user_groups SET name = ? WHERE id = ?', { From ce72982b6b6bc13abb0f0d8b24c6a92beee9ad08 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Fri, 28 Jun 2024 15:10:36 -0700 Subject: [PATCH 5/7] PR comment resolve --- .../migrations/2024.06.07T00.36.14.clean-user-group-names.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts index e47a205a..dad45526 100644 --- a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts +++ b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts @@ -8,8 +8,8 @@ export const up: Migration = async ({ context: queryInterface }) => { for (const row of rows) { const trimmedName = row.name.trim().replace(/\s+/g, " ") - await queryInterface.sequelize.query('UPDATE user_groups SET name = ? WHERE id = ?', { - replacements: [trimmedName, row.id] + await queryInterface.sequelize.query('UPDATE user_groups SET name = :trimmedName WHERE id = :userId', { + replacements: { trimmedName, userId: row.id } }); } } From cdbd1bf3f3355ae03dbeea9f65f7504f146bf5e3 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Mon, 8 Jul 2024 12:08:53 -0700 Subject: [PATCH 6/7] Fixed type issue --- ...4.06.07T00.36.14.clean-user-group-names.ts | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts index dad45526..df9fbecc 100644 --- a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts +++ b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts @@ -1,19 +1,25 @@ -import type { Migration } from "@/db/umzug" +import type { Migration } from "@/db/umzug"; import { QueryTypes } from "sequelize"; export const up: Migration = async ({ context: queryInterface }) => { - const [rows] = await queryInterface.sequelize.query<{ id: number, name: string }[]>('SELECT name from user_groups', { - type: QueryTypes.SELECT, - }); - + const rows: { id: number, name: string }[] = await queryInterface.sequelize.query( + 'SELECT id, name FROM user_groups', + { + type: QueryTypes.SELECT, + } + ); + for (const row of rows) { - const trimmedName = row.name.trim().replace(/\s+/g, " ") - await queryInterface.sequelize.query('UPDATE user_groups SET name = :trimmedName WHERE id = :userId', { - replacements: { trimmedName, userId: row.id } - }); + const trimmedName = row.name.trim().replace(/\s+/g, " "); + await queryInterface.sequelize.query( + 'UPDATE user_groups SET name = :trimmedName WHERE id = :userId', + { + replacements: { trimmedName, userId: row.id }, + } + ); } -} +}; export const down: Migration = async () => { // no-op -} +}; \ No newline at end of file From 7c89f68a40c5295c3631e5a78b547de448a63a96 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Mon, 8 Jul 2024 13:45:16 -0700 Subject: [PATCH 7/7] Prettier --- ...4.06.07T00.36.14.clean-user-group-names.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts index df9fbecc..fde0f40c 100644 --- a/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts +++ b/api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts @@ -1,25 +1,25 @@ -import type { Migration } from "@/db/umzug"; -import { QueryTypes } from "sequelize"; +import type { Migration } from "@/db/umzug" +import { QueryTypes } from "sequelize" export const up: Migration = async ({ context: queryInterface }) => { - const rows: { id: number, name: string }[] = await queryInterface.sequelize.query( - 'SELECT id, name FROM user_groups', + const rows: { id: number; name: string }[] = await queryInterface.sequelize.query( + "SELECT id, name FROM user_groups", { type: QueryTypes.SELECT, } - ); - + ) + for (const row of rows) { - const trimmedName = row.name.trim().replace(/\s+/g, " "); + const trimmedName = row.name.trim().replace(/\s+/g, " ") await queryInterface.sequelize.query( - 'UPDATE user_groups SET name = :trimmedName WHERE id = :userId', + "UPDATE user_groups SET name = :trimmedName WHERE id = :userId", { replacements: { trimmedName, userId: row.id }, } - ); + ) } -}; +} export const down: Migration = async () => { // no-op -}; \ No newline at end of file +}