medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.27k stars 2.67k forks source link

[Bug]: cli migration doesn't work for ssl connections with postgresql #10243

Closed IanWang0403 closed 1 week ago

IanWang0403 commented 1 week ago

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "predeploy": "medusa db:migrate",
    "start": "echo $DATABASE_CA && echo $NODE_ENV && medusa db:migrate && medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/medusa": "latest",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v20.11.0

Database and its version

PostgreSQL (ver. 16.4)

Operating system name and version

Ubuntu 22

Browser name

Chrome

What happended?

npx medusa db:migrate
{"level":"info","message":"Running migrations...","timestamp":"2024-11-24 21:42:51"}
---------------------------------------------------------------------------------------------------------------------------------------------------------------

{"level":"info","message":"MODULE: cache","timestamp":"2024-11-24 21:42:53"}
{"level":"error","message":"Failed with error no pg_hba.conf entry for host

Using ssl to connect to db doesn't work. the connection has established but seems migration's connection does not use the connection details in projectConfig

`import { loadEnv, defineConfig, Modules } from "@medusajs/framework/utils";
import fs from "fs";

loadEnv(process.env.NODE_ENV || "development", process.cwd());

module.exports = defineConfig({
  admin: {
    disable: process.env.DISABLE_MEDUSA_ADMIN === "true",
  },
  projectConfig: {
    databaseUrl: process.env.DATABASE_URL,
    workerMode: process.env.MEDUSA_WORKER_MODE as
      | "shared"
      | "worker"
      | "server",
    http: {
      storeCors: process.env.STORE_CORS!,
      adminCors: process.env.ADMIN_CORS!,
      authCors: process.env.AUTH_CORS!,
      jwtSecret: process.env.JWT_SECRET || "supersecret",
      cookieSecret: process.env.COOKIE_SECRET || "supersecret",
    },
    redisUrl: process.env.REDIS_URL,
    databaseDriverOptions: {
      // sslMode: "require",
      ssl: {
        rejectUnauthorized: true,
        ca: fs.readFileSync("ca.crt").toString(), // this is my local
      },
    },
  },
});
`

Expected behavior

use the projectConfig's connection info

Actual behavior

Failed with error no pg_hba.conf entry for host since I'm using ssl

Link to reproduction repo

https://github.com/medusajs/medusa

olivermrbl commented 1 week ago

Your ssl settings should be nested within a connection option:

   ...
    connection: {
      ssl: {
        rejectUnauthorized: true,
        ca: fs.existsSync(
          path.join(__dirname, process.env.DATABASE_SSL_CERT)
        ),
      },
    },

Did you follow a guide for this setup? If so, we need to get that updated.

IanWang0403 commented 1 week ago

@olivermrbl Thanks it worked.

The problem here is the type here. (also document doesn't have the connection) image

olivermrbl commented 1 week ago

Thanks, this was fixed in 2.0.5, so if you update to latest the error type should be resolved.