Skip to content

Commit

Permalink
patch before generate
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Jan 24, 2025
1 parent eae054c commit fc6390b
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { normalizeMigrationSQL } from "../utils"

type FilterDef = Parameters<typeof MikroORMFilter>[0]

let sqlPatches: string[] = []
export class CustomTsMigrationGenerator extends TSMigrationGenerator {
// TODO: temporary fix to drop unique constraint before creating unique index
private dropUniqueConstraintBeforeUniqueIndex(sql: string) {
private dropUniqueConstraintBeforeUniqueIndex(
sqlPatches: string[],
sql: string
) {
// DML unique index
const uniqueIndexName = sql.match(/"IDX_(.+?)_unique"/)?.[1]
if (!uniqueIndexName) {
Expand All @@ -29,18 +31,21 @@ export class CustomTsMigrationGenerator extends TSMigrationGenerator {
className: string,
diff: { up: string[]; down: string[] }
): string {
const sqlPatches: string[] = []
for (const sql of diff.up) {
this.dropUniqueConstraintBeforeUniqueIndex(sqlPatches, sql)
}

for (const sql of sqlPatches) {
diff.up.unshift(super.createStatement(sql, 0))
diff.up.unshift(sql)
}
sqlPatches = []

return super.generateMigrationFile(className, diff)
}

createStatement(sql: string, padLeft: number): string {
if (isString(sql)) {
sql = normalizeMigrationSQL(sql)
this.dropUniqueConstraintBeforeUniqueIndex(sql)
}

return super.createStatement(sql, padLeft)
Expand Down

0 comments on commit fc6390b

Please sign in to comment.