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..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,12 +1,23 @@ import type { Migration } from "@/db/umzug" -import { UserGroup } from "@/models" +import { QueryTypes } from "sequelize" -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 }) => { + 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 }, + } + ) + } } export const down: Migration = async () => {