breaking: remove POSTGRES prefix env variables in favor of DATABASE prefix (#8672)

This commit is contained in:
Harminder Virk authored and GitHub committed 2024-08-20 17:05:19 +05:30
1 parent cfdd056d70
commit 5f310c0a00
7 files changed
+38 -46

No files matched your search

@@ -8,7 +8,6 @@ jobs:
NODE_ENV: CI NODE_ENV: CI
REDIS_URL: redis://localhost:6379 REDIS_URL: redis://localhost:6379
DATABASE_URL: "postgres://postgres:postgres@localhost/cli-test" DATABASE_URL: "postgres://postgres:postgres@localhost/cli-test"
POSTGRES_URL: "postgres://postgres:postgres@localhost/cli-test"
services: services:
redis: redis:
image: redis image: redis
@@ -9,11 +9,11 @@ medusaIntegrationTestRunner({
testSuite: ({ dbConfig: { clientUrl } }) => { testSuite: ({ dbConfig: { clientUrl } }) => {
describe("Standalone Modules", () => { describe("Standalone Modules", () => {
beforeAll(async () => { beforeAll(async () => {
process.env.POSTGRES_URL = clientUrl process.env.DATABASE_URL = clientUrl
}) })
afterAll(async () => { afterAll(async () => {
process.env.POSTGRES_URL = undefined process.env.DATABASE_URL = undefined
}) })
it("Should migrate database and initialize Product module using connection string from environment variable ", async function () { it("Should migrate database and initialize Product module using connection string from environment variable ", async function () {
@@ -9,7 +9,8 @@ import type { Client } from "pg"
const ADMIN_EMAIL = "admin@medusa-test.com" const ADMIN_EMAIL = "admin@medusa-test.com"
const STORE_CORS = "http://localhost:8000,https://docs.medusajs.com" const STORE_CORS = "http://localhost:8000,https://docs.medusajs.com"
const ADMIN_CORS = "http://localhost:7000,http://localhost:7001,https://docs.medusajs.com" const ADMIN_CORS =
"http://localhost:7000,http://localhost:7001,https://docs.medusajs.com"
const AUTH_CORS = ADMIN_CORS const AUTH_CORS = ADMIN_CORS
const DEFAULT_REDIS_URL = "redis://localhost:6379" const DEFAULT_REDIS_URL = "redis://localhost:6379"
@@ -72,13 +73,13 @@ export default async ({
let env = `MEDUSA_ADMIN_ONBOARDING_TYPE=${onboardingType}${EOL}STORE_CORS=${STORE_CORS}${EOL}ADMIN_CORS=${ADMIN_CORS}${EOL}AUTH_CORS=${AUTH_CORS}${EOL}REDIS_URL=${DEFAULT_REDIS_URL}${EOL}JWT_SECRET=supersecret${EOL}COOKIE_SECRET=supersecret` let env = `MEDUSA_ADMIN_ONBOARDING_TYPE=${onboardingType}${EOL}STORE_CORS=${STORE_CORS}${EOL}ADMIN_CORS=${ADMIN_CORS}${EOL}AUTH_CORS=${AUTH_CORS}${EOL}REDIS_URL=${DEFAULT_REDIS_URL}${EOL}JWT_SECRET=supersecret${EOL}COOKIE_SECRET=supersecret`
if (!skipDb) { if (!skipDb) {
env += `${EOL}DATABASE_URL=${dbConnectionString}${EOL}POSTGRES_URL=${dbConnectionString}` env += `${EOL}DATABASE_URL=${dbConnectionString}`
} }
if (nextjsDirectory) { if (nextjsDirectory) {
env += `${EOL}MEDUSA_ADMIN_ONBOARDING_NEXTJS_DIRECTORY=${nextjsDirectory}` env += `${EOL}MEDUSA_ADMIN_ONBOARDING_NEXTJS_DIRECTORY=${nextjsDirectory}`
} }
fs.appendFileSync(path.join(directory, `.env`), env) fs.appendFileSync(path.join(directory, `.env`), env)
factBoxOptions.interval = displayFactBox({ factBoxOptions.interval = displayFactBox({
@@ -183,10 +184,7 @@ export default async ({
await processManager.runProcess({ await processManager.runProcess({
process: async () => { process: async () => {
const proc = await execute( const proc = await execute(
[ [`npx medusa user -e ${ADMIN_EMAIL} --invite`, npxOptions],
`npx medusa user -e ${ADMIN_EMAIL} --invite`,
npxOptions,
],
{ verbose, needOutput: true } { verbose, needOutput: true }
) )
@@ -2,20 +2,20 @@ import { loadDatabaseConfig } from "../load-module-database-config"
describe("loadDatabaseConfig", function () { describe("loadDatabaseConfig", function () {
afterEach(() => { afterEach(() => {
delete process.env.POSTGRES_URL delete process.env.DATABASE_URL
delete process.env.MEDUSA_POSTGRES_URL delete process.env.MEDUSA_DATABASE_URL
delete process.env.PRODUCT_POSTGRES_URL delete process.env.PRODUCT_DATABASE_URL
}) })
it("should return the local configuration using the environment variable respecting their precedence", function () { it("should return the local configuration using the environment variable respecting their precedence", function () {
process.env.MEDUSA_POSTGRES_URL = "postgres://localhost:5432/medusa" process.env.MEDUSA_DATABASE_URL = "postgres://localhost:5432/medusa"
process.env.PRODUCT_POSTGRES_URL = "postgres://localhost:5432/product" process.env.PRODUCT_DATABASE_URL = "postgres://localhost:5432/product"
process.env.POSTGRES_URL = "postgres://localhost:5432/share_db" process.env.DATABASE_URL = "postgres://localhost:5432/share_db"
let config = loadDatabaseConfig("product") let config = loadDatabaseConfig("product")
expect(config).toEqual({ expect(config).toEqual({
clientUrl: process.env.PRODUCT_POSTGRES_URL, clientUrl: process.env.PRODUCT_DATABASE_URL,
driverOptions: { driverOptions: {
connection: { connection: {
ssl: false, ssl: false,
@@ -25,11 +25,11 @@ describe("loadDatabaseConfig", function () {
schema: "", schema: "",
}) })
delete process.env.PRODUCT_POSTGRES_URL delete process.env.PRODUCT_DATABASE_URL
config = loadDatabaseConfig("product") config = loadDatabaseConfig("product")
expect(config).toEqual({ expect(config).toEqual({
clientUrl: process.env.MEDUSA_POSTGRES_URL, clientUrl: process.env.MEDUSA_DATABASE_URL,
driverOptions: { driverOptions: {
connection: { connection: {
ssl: false, ssl: false,
@@ -39,11 +39,11 @@ describe("loadDatabaseConfig", function () {
schema: "", schema: "",
}) })
delete process.env.MEDUSA_POSTGRES_URL delete process.env.MEDUSA_DATABASE_URL
config = loadDatabaseConfig("product") config = loadDatabaseConfig("product")
expect(config).toEqual({ expect(config).toEqual({
clientUrl: process.env.POSTGRES_URL, clientUrl: process.env.DATABASE_URL,
driverOptions: { driverOptions: {
connection: { connection: {
ssl: false, ssl: false,
@@ -55,11 +55,11 @@ describe("loadDatabaseConfig", function () {
}) })
it("should return the remote configuration using the environment variable", function () { it("should return the remote configuration using the environment variable", function () {
process.env.POSTGRES_URL = "postgres://https://test.com:5432/medusa" process.env.DATABASE_URL = "postgres://https://test.com:5432/medusa"
let config = loadDatabaseConfig("product") let config = loadDatabaseConfig("product")
expect(config).toEqual({ expect(config).toEqual({
clientUrl: process.env.POSTGRES_URL, clientUrl: process.env.DATABASE_URL,
driverOptions: { driverOptions: {
connection: { connection: {
ssl: { ssl: {
@@ -71,12 +71,12 @@ describe("loadDatabaseConfig", function () {
schema: "", schema: "",
}) })
delete process.env.POSTGRES_URL delete process.env.DATABASE_URL
process.env.PRODUCT_POSTGRES_URL = "postgres://https://test.com:5432/medusa" process.env.PRODUCT_DATABASE_URL = "postgres://https://test.com:5432/medusa"
config = loadDatabaseConfig("product") config = loadDatabaseConfig("product")
expect(config).toEqual({ expect(config).toEqual({
clientUrl: process.env.PRODUCT_POSTGRES_URL, clientUrl: process.env.PRODUCT_DATABASE_URL,
driverOptions: { driverOptions: {
connection: { connection: {
ssl: { ssl: {
@@ -90,7 +90,7 @@ describe("loadDatabaseConfig", function () {
}) })
it("should return the local configuration using the options", function () { it("should return the local configuration using the options", function () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa" process.env.DATABASE_URL = "postgres://localhost:5432/medusa"
const options = { const options = {
database: { database: {
clientUrl: "postgres://localhost:5432/medusa-test", clientUrl: "postgres://localhost:5432/medusa-test",
@@ -112,7 +112,7 @@ describe("loadDatabaseConfig", function () {
}) })
it("should return the local configuration using the options", function () { it("should return the local configuration using the options", function () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa" process.env.DATABASE_URL = "postgres://localhost:5432/medusa"
const options = { const options = {
database: { database: {
clientUrl: "postgres://127.0.0.1:5432/medusa-test", clientUrl: "postgres://127.0.0.1:5432/medusa-test",
@@ -134,7 +134,7 @@ describe("loadDatabaseConfig", function () {
}) })
it("should return the remote configuration using the options", function () { it("should return the remote configuration using the options", function () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa" process.env.DATABASE_URL = "postgres://localhost:5432/medusa"
const options = { const options = {
database: { database: {
clientUrl: "postgres://https://test.com:5432/medusa-test", clientUrl: "postgres://https://test.com:5432/medusa-test",
@@ -158,7 +158,7 @@ describe("loadDatabaseConfig", function () {
}) })
it("should return the local configuration using the client url ssl_mode=disable", function () { it("should return the local configuration using the client url ssl_mode=disable", function () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa" process.env.DATABASE_URL = "postgres://localhost:5432/medusa"
const options = { const options = {
database: { database: {
clientUrl: clientUrl:
@@ -181,7 +181,7 @@ describe("loadDatabaseConfig", function () {
}) })
it("should return the remote configuration using the client url ssl_mode=false", function () { it("should return the remote configuration using the client url ssl_mode=false", function () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa" process.env.DATABASE_URL = "postgres://localhost:5432/medusa"
const options = { const options = {
database: { database: {
clientUrl: clientUrl:
@@ -212,7 +212,7 @@ describe("loadDatabaseConfig", function () {
} }
expect(error.message).toEqual( expect(error.message).toEqual(
"No database clientUrl provided. Please provide the clientUrl through the [MODULE]_POSTGRES_URL, MEDUSA_POSTGRES_URL or POSTGRES_URL environment variable or the options object in the initialize function." "No database clientUrl provided. Please provide the clientUrl through the [MODULE]_DATABASE_URL, MEDUSA_DATABASE_URL or DATABASE_URL environment variable or the options object in the initialize function."
) )
}) })
}) })
@@ -57,7 +57,7 @@ function getDatabaseUrl(
/** /**
* Load the config for the database connection. The options can be retrieved * Load the config for the database connection. The options can be retrieved
* e.g through PRODUCT_* (e.g PRODUCT_POSTGRES_URL) or * (e.g POSTGRES_URL) environment variables or the options object. * e.g through PRODUCT_* (e.g PRODUCT_DATABASE_URL) or * (e.g DATABASE_URL) environment variables or the options object.
* @param options * @param options
* @param moduleName * @param moduleName
*/ */
@@ -70,13 +70,13 @@ export function loadDatabaseConfig(
"clientUrl" | "schema" | "driverOptions" | "debug" "clientUrl" | "schema" | "driverOptions" | "debug"
> { > {
const clientUrl = const clientUrl =
options?.database?.clientUrl ?? getEnv("POSTGRES_URL", moduleName) options?.database?.clientUrl ?? getEnv("DATABASE_URL", moduleName)
const database = { const database = {
clientUrl, clientUrl,
schema: getEnv("POSTGRES_SCHEMA", moduleName) ?? "public", schema: getEnv("DATABASE_SCHEMA", moduleName) ?? "public",
driverOptions: JSON.parse( driverOptions: JSON.parse(
getEnv("POSTGRES_DRIVER_OPTIONS", moduleName) || getEnv("DATABASE_DRIVER_OPTIONS", moduleName) ||
JSON.stringify(getDefaultDriverOptions(clientUrl)) JSON.stringify(getDefaultDriverOptions(clientUrl))
), ),
debug: false, debug: false,
@@ -98,7 +98,7 @@ export function loadDatabaseConfig(
if (!database.clientUrl && !silent && !database.connection) { if (!database.clientUrl && !silent && !database.connection) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT, MedusaError.Types.INVALID_ARGUMENT,
"No database clientUrl provided. Please provide the clientUrl through the [MODULE]_POSTGRES_URL, MEDUSA_POSTGRES_URL or POSTGRES_URL environment variable or the options object in the initialize function." "No database clientUrl provided. Please provide the clientUrl through the [MODULE]_DATABASE_URL, MEDUSA_DATABASE_URL or DATABASE_URL environment variable or the options object in the initialize function."
) )
} }
+1 -1
View File
@@ -23,7 +23,7 @@ npm install @medusajs/product
2\. Add Database URL to your environment variables 2\. Add Database URL to your environment variables
```bash ```bash
POSTGRES_URL=<DATABASE_URL> DATABASE_URL=<URL_GOES_HERE>
``` ```
3\. Apply database migrations 3\. Apply database migrations
@@ -53,7 +53,6 @@ module.exports = defineConfig({
workerMode: process.env.MEDUSA_WORKER_MODE, workerMode: process.env.MEDUSA_WORKER_MODE,
}, },
}) })
``` ```
Later, you’ll set different values of the `MEDUSA_WORKER_MODE` environment variable for each Medusa application deployment. Later, you’ll set different values of the `MEDUSA_WORKER_MODE` environment variable for each Medusa application deployment.
@@ -80,7 +79,6 @@ module.exports = defineConfig({
disable: process.env.DISABLE_MEDUSA_ADMIN === "true", disable: process.env.DISABLE_MEDUSA_ADMIN === "true",
}, },
}) })
``` ```
Later, you’ll set different values of the `DISABLE_MEDUSA_ADMIN` environment variable. Later, you’ll set different values of the `DISABLE_MEDUSA_ADMIN` environment variable.
@@ -96,7 +94,6 @@ module.exports = defineConfig({
redisUrl: process.env.REDIS_URL, redisUrl: process.env.REDIS_URL,
}, },
}) })
``` ```
--- ---
@@ -150,13 +147,13 @@ module.exports = defineConfig({
// ... // ...
[Modules.CACHE]: { [Modules.CACHE]: {
resolve: "@medusajs/cache-redis", resolve: "@medusajs/cache-redis",
options: { options: {
redisUrl: process.env.REDIS_URL, redisUrl: process.env.REDIS_URL,
}, },
}, },
[Modules.EVENT_BUS]: { [Modules.EVENT_BUS]: {
resolve: "@medusajs/event-bus-redis", resolve: "@medusajs/event-bus-redis",
options: { options: {
redisUrl: process.env.REDIS_URL, redisUrl: process.env.REDIS_URL,
}, },
}, },
@@ -240,7 +237,6 @@ DISABLE_MEDUSA_ADMIN=true
MEDUSA_WORKER_MODE=worker MEDUSA_WORKER_MODE=worker
PORT=9000 PORT=9000
DATABASE_URL=${{Postgres.DATABASE_PUBLIC_URL}} DATABASE_URL=${{Postgres.DATABASE_PUBLIC_URL}}
POSTGRES_URL=${{Postgres.DATABASE_PUBLIC_URL}}
REDIS_URL=${{Redis.REDIS_PUBLIC_URL}} REDIS_URL=${{Redis.REDIS_PUBLIC_URL}}
``` ```
@@ -279,7 +275,7 @@ You can either generate a random domain name or set a custom one. To do that:
2. Choose the Settings tab. 2. Choose the Settings tab.
3. Scroll down to the Networking section. 3. Scroll down to the Networking section.
4. Under Public Networking, click on Generate domain to generate a domain name or Custom domain to add your custom domain. 4. Under Public Networking, click on Generate domain to generate a domain name or Custom domain to add your custom domain.
1. Choose the `9000` port. 1. Choose the `9000` port.
5. Save the changes. 5. Save the changes.
### Additional Configuration if Deploying with Admin ### Additional Configuration if Deploying with Admin
@@ -349,7 +345,6 @@ DISABLE_MEDUSA_ADMIN=false
MEDUSA_WORKER_MODE=worker MEDUSA_WORKER_MODE=worker
PORT=9000 PORT=9000
DATABASE_URL=${{Postgres.DATABASE_PUBLIC_URL}} DATABASE_URL=${{Postgres.DATABASE_PUBLIC_URL}}
POSTGRES_URL=${{Postgres.DATABASE_PUBLIC_URL}}
REDIS_URL=${{Redis.REDIS_PUBLIC_URL}} REDIS_URL=${{Redis.REDIS_PUBLIC_URL}}
``` ```