From 226c0048f97229b80ec7b14a90e7953ea4f171f3 Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 2 Oct 2023 12:54:08 +0200 Subject: [PATCH] fix (generator): correctly capitalise table named model (#515) This PR fixes the bug for a table named "model", described by a user on [Discord](https://discord.com/channels/933657521581858818/933657523049885698/1156342498453639260). We were parsing the Prisma schema in order to capitalise model names but if the model name itself was "model" we would capitalise the "model" keyword instead of the model name "model". This is fixed by replacing the exact match in the string instead of replacing the first occurrence of the table name. Also extended the unit test to check this case. --- .changeset/chilled-planes-attack.md | 5 +++++ clients/typescript/src/cli/migrations/migrate.ts | 7 +++++-- clients/typescript/test/cli/migrations/migrate.test.ts | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/chilled-planes-attack.md 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) => {