diff --git a/.changeset/chilled-planes-attack.md b/.changeset/chilled-planes-attack.md new file mode 100644 index 0000000000..8375630ea5 --- /dev/null +++ b/.changeset/chilled-planes-attack.md @@ -0,0 +1,5 @@ +--- +"electric-sql": patch +--- + +Fix bug for table named "model" diff --git a/clients/typescript/src/cli/migrations/migrate.ts b/clients/typescript/src/cli/migrations/migrate.ts index 6ee03a37f1..1b2c83b770 100644 --- a/clients/typescript/src/cli/migrations/migrate.ts +++ b/clients/typescript/src/cli/migrations/migrate.ts @@ -309,7 +309,8 @@ export function doPascalCaseTableNames(lines: string[]): string[] { const replacements: Map = new Map() // maps table names to their PascalCased model name const modelNameToDbName: Map = new Map() // maps the PascalCased model names to their original table name - const getModelName = (ln: string) => ln.match(/^\s*model\s+(\w+)/)?.[1] + const modelRegex = /^\s*model\s+(\w+)\s*{/ + const getModelName = (ln: string) => ln.match(modelRegex)?.[1] lines.forEach((ln, idx) => { const tableName = getModelName(ln) @@ -320,7 +321,9 @@ export function doPascalCaseTableNames(lines: string[]): string[] { : capitaliseFirstLetter(tableName) // always capitalise first letter // Replace the model name on this line - const newLn = ln.replace(tableName, modelName) + const newLn = ln.replace(modelRegex, (_, _tableName) => { + return `model ${modelName} {` + }) lines[idx] = newLn replacements.set(tableName, modelName) diff --git a/clients/typescript/test/cli/migrations/migrate.test.ts b/clients/typescript/test/cli/migrations/migrate.test.ts index 9933b92b5e..0b88f60435 100644 --- a/clients/typescript/test/cli/migrations/migrate.test.ts +++ b/clients/typescript/test/cli/migrations/migrate.test.ts @@ -46,6 +46,10 @@ model user_profile { userId Int @unique user user? @relation(fields: [userId], references: [id]) } + +model model { + id Int @id +} ` const expectedPrismaSchema = ` @@ -97,6 +101,11 @@ model UserProfile { userId Int @unique user User? @relation(fields: [userId], references: [id]) } + +model Model { + @@map("model") + id Int @id +} ` test('migrator correctly PascalCases model names', (t) => {