From 9a742911feaa2274743b0edca5d649fcbbf8b6e3 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 16 Jan 2025 15:29:09 +0100 Subject: [PATCH] fix(framework): Exclude .d.ts files from the glob search and fix insert query (#10990) **What** - Exclude definition file from the glob search string - Properly generate the insert query string --- .changeset/tall-gorillas-smash.md | 5 +++++ .../__tests__/run-migration-scripts.spec.ts | 16 +++++++++------- .../core/framework/src/migrations/migrator.ts | 11 ++++++++--- .../src/migrations/run-migration-scripts.ts | 4 ++-- 4 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 .changeset/tall-gorillas-smash.md diff --git a/.changeset/tall-gorillas-smash.md b/.changeset/tall-gorillas-smash.md new file mode 100644 index 0000000000..4453972692 --- /dev/null +++ b/.changeset/tall-gorillas-smash.md @@ -0,0 +1,5 @@ +--- +"@medusajs/framework": patch +--- + +fix(framework): Exclude .d.ts files from the glob search and Properly generate the insert query string diff --git a/packages/core/framework/src/migrations/__tests__/run-migration-scripts.spec.ts b/packages/core/framework/src/migrations/__tests__/run-migration-scripts.spec.ts index b3ba38b530..9417da351f 100644 --- a/packages/core/framework/src/migrations/__tests__/run-migration-scripts.spec.ts +++ b/packages/core/framework/src/migrations/__tests__/run-migration-scripts.spec.ts @@ -44,9 +44,6 @@ describe("MigrationScriptsMigrator", () => { jest .spyOn(migrator as any, "getPendingMigrations") .mockResolvedValue([scriptPath]) - jest - .spyOn(migrator as any, "insertMigration") - .mockResolvedValue(undefined) jest .spyOn(migrator as any, "trackDuration") .mockReturnValue({ getSeconds: () => 1 }) @@ -63,13 +60,18 @@ describe("MigrationScriptsMigrator", () => { expect(mockScript).toHaveBeenCalled() - expect(mockPgConnection.raw).toHaveBeenCalledWith( + expect(mockPgConnection.raw).toHaveBeenNthCalledWith( + 1, + expect.stringContaining( + "INSERT INTO script_migrations (script_name) VALUES (?)" + ), + [path.basename(scriptPath)] + ) + expect(mockPgConnection.raw).toHaveBeenNthCalledWith( + 2, expect.stringContaining("UPDATE script_migrations"), [path.basename(scriptPath)] ) - expect(migrator["insertMigration"]).toHaveBeenCalledWith([ - { script_name: `'${path.basename(scriptPath)}'` }, - ]) }) it("should handle failed migrations by cleaning up", async () => { diff --git a/packages/core/framework/src/migrations/migrator.ts b/packages/core/framework/src/migrations/migrator.ts index 76d3a04806..df1ab51434 100644 --- a/packages/core/framework/src/migrations/migrator.ts +++ b/packages/core/framework/src/migrations/migrator.ts @@ -97,8 +97,13 @@ export abstract class Migrator { await this.pgConnection.raw( `INSERT INTO ${this.migration_table_name} (${columns.join( ", " - )}) VALUES (${new Array(values.length).fill("?").join(",")})`, - values + )}) VALUES ${values + .map( + (itemValues) => + `(${new Array(itemValues.length).fill("?").join(",")})` + ) + .join(",")}`, + values.flat() ) } catch (error) { logger.error( @@ -130,7 +135,7 @@ export abstract class Migrator { } try { - const scriptFiles = glob.sync("*.{js,ts}", { + const scriptFiles = glob.sync("*.{js,(!d.)ts}", { cwd: basePath, ignore: ["**/index.{js,ts}"], }) diff --git a/packages/core/framework/src/migrations/run-migration-scripts.ts b/packages/core/framework/src/migrations/run-migration-scripts.ts index ff88a07038..12bac7bc21 100644 --- a/packages/core/framework/src/migrations/run-migration-scripts.ts +++ b/packages/core/framework/src/migrations/run-migration-scripts.ts @@ -37,7 +37,7 @@ export class MigrationScriptsMigrator extends Migrator { const scriptName = basename(script) const err = await this.insertMigration([ - { script_name: `'${scriptName}'` }, + { script_name: scriptName }, ]).catch((e) => e) /** @@ -99,7 +99,7 @@ export class MigrationScriptsMigrator extends Migrator { #updateMigrationFinishedAt(scriptName: string) { return this.pgConnection.raw( - `UPDATE ${this.migration_table_name} SET finished_at = CURRENT_TIMESTAMP WHERE script_name = ?`, + `UPDATE ${this.migration_table_name} SET finished_at = NOW() WHERE script_name = ?`, [scriptName] ) }