Skip to content

Commit

Permalink
Merge pull request #115 from icefoganalytics/issue-114/fix-broke-migr…
Browse files Browse the repository at this point in the history
…ation-with-user-groups
  • Loading branch information
burkkyy authored Jul 8, 2024
2 parents 4e2c016 + 7c89f68 commit 9468e67
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down

0 comments on commit 9468e67

Please sign in to comment.