Skip to content

Commit 9468e67

Browse files
authored
Merge pull request #115 from icefoganalytics/issue-114/fix-broke-migration-with-user-groups
2 parents 4e2c016 + 7c89f68 commit 9468e67

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

api/src/db/migrations/2024.06.07T00.36.14.clean-user-group-names.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import type { Migration } from "@/db/umzug"
2-
import { UserGroup } from "@/models"
2+
import { QueryTypes } from "sequelize"
33

4-
export const up: Migration = async () => {
5-
await UserGroup.findEach(async (userGroup) => {
6-
await userGroup.update({
7-
name: userGroup.name.trim().replace(/\s+/g, " "),
8-
})
9-
})
4+
export const up: Migration = async ({ context: queryInterface }) => {
5+
const rows: { id: number; name: string }[] = await queryInterface.sequelize.query(
6+
"SELECT id, name FROM user_groups",
7+
{
8+
type: QueryTypes.SELECT,
9+
}
10+
)
11+
12+
for (const row of rows) {
13+
const trimmedName = row.name.trim().replace(/\s+/g, " ")
14+
await queryInterface.sequelize.query(
15+
"UPDATE user_groups SET name = :trimmedName WHERE id = :userId",
16+
{
17+
replacements: { trimmedName, userId: row.id },
18+
}
19+
)
20+
}
1021
}
1122

1223
export const down: Migration = async () => {

0 commit comments

Comments
 (0)