diff --git a/.changeset/tiny-guests-wait.md b/.changeset/tiny-guests-wait.md new file mode 100644 index 0000000000..049578f018 --- /dev/null +++ b/.changeset/tiny-guests-wait.md @@ -0,0 +1,6 @@ +--- +"@medusajs/framework": patch +"@medusajs/utils": patch +--- + +chore(): remove ssl_mode from url and also use sslmode diff --git a/packages/core/framework/src/database/pg-connection-loader.ts b/packages/core/framework/src/database/pg-connection-loader.ts index 33d2279903..e977990045 100644 --- a/packages/core/framework/src/database/pg-connection-loader.ts +++ b/packages/core/framework/src/database/pg-connection-loader.ts @@ -38,8 +38,18 @@ export async function pgConnectionLoader(): Promise< delete driverOptions.pool + const clientUrl = connectionString?.replace( + /(\?|&)ssl_mode=[^&]*(&|$)/gi, + (match, prefix, suffix) => { + if (prefix === "?" && suffix === "&") return "?" + if (prefix === "?" && suffix === "") return "" + if (prefix === "&") return suffix + return "" + } + ) + const pgConnection = ModulesSdkUtils.createPgConnection({ - clientUrl: connectionString, + clientUrl, schema, driverOptions, pool: { diff --git a/packages/core/utils/src/modules-sdk/__tests__/load-database-config.spec.ts b/packages/core/utils/src/modules-sdk/__tests__/load-database-config.spec.ts index 83ed0923af..7e02f1307f 100644 --- a/packages/core/utils/src/modules-sdk/__tests__/load-database-config.spec.ts +++ b/packages/core/utils/src/modules-sdk/__tests__/load-database-config.spec.ts @@ -169,7 +169,30 @@ describe("loadDatabaseConfig", function () { let config = loadDatabaseConfig("product", options) expect(config).toEqual({ - clientUrl: options.database.clientUrl, + clientUrl: "postgres://https://test.com:5432/medusa-test", + driverOptions: { + connection: { + ssl: false, + }, + }, + debug: false, + schema: "", + }) + }) + + it("should return the local configuration using the client url sslmode=disable", function () { + process.env.DATABASE_URL = "postgres://localhost:5432/medusa" + const options = { + database: { + clientUrl: + "postgres://https://test.com:5432/medusa-test?sslmode=disable", + }, + } + + let config = loadDatabaseConfig("product", options) + + expect(config).toEqual({ + clientUrl: "postgres://https://test.com:5432/medusa-test?sslmode=disable", driverOptions: { connection: { ssl: false, @@ -185,14 +208,14 @@ describe("loadDatabaseConfig", function () { const options = { database: { clientUrl: - "postgres://https://test.com:5432/medusa-test?ssl_mode=disable", + "postgres://https://test.com:5432/medusa-test?ssl_mode=false", }, } let config = loadDatabaseConfig("product", options) expect(config).toEqual({ - clientUrl: options.database.clientUrl, + clientUrl: "postgres://https://test.com:5432/medusa-test", driverOptions: { connection: { ssl: false, diff --git a/packages/core/utils/src/modules-sdk/load-module-database-config.ts b/packages/core/utils/src/modules-sdk/load-module-database-config.ts index baaca4c558..e844c70381 100644 --- a/packages/core/utils/src/modules-sdk/load-module-database-config.ts +++ b/packages/core/utils/src/modules-sdk/load-module-database-config.ts @@ -31,7 +31,9 @@ function getDefaultDriverOptions(clientUrl: string) { } if (clientUrl) { - return clientUrl.match(/localhost|127\.0\.0\.1|ssl_mode=(disable|false)/i) + return clientUrl.match( + /localhost|127\.0\.0\.1|ssl_mode=(disable|false)|sslmode=(disable)/i + ) ? localOptions : remoteOptions } @@ -105,5 +107,19 @@ export function loadDatabaseConfig( ) } + /** + * Remove the ssl_mode parameter since it is not supported by + * the database + * driver but rather an internal parameter used by us. + */ + database.clientUrl = database.clientUrl?.replace( + /(\?|&)ssl_mode=[^&]*(&|$)/gi, + (match, prefix, suffix) => { + if (prefix === "?" && suffix === "&") return "?" + if (prefix === "?" && suffix === "") return "" + if (prefix === "&") return suffix + return "" + } + ) return database }