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

This commit is contained in:
Harminder Virk
2024-08-20 17:05:19 +05:30
committed by GitHub
parent cfdd056d70
commit 5f310c0a00
7 changed files with 38 additions and 46 deletions
@@ -9,7 +9,8 @@ import type { Client } from "pg"
const ADMIN_EMAIL = "admin@medusa-test.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 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`
if (!skipDb) {
env += `${EOL}DATABASE_URL=${dbConnectionString}${EOL}POSTGRES_URL=${dbConnectionString}`
env += `${EOL}DATABASE_URL=${dbConnectionString}`
}
if (nextjsDirectory) {
env += `${EOL}MEDUSA_ADMIN_ONBOARDING_NEXTJS_DIRECTORY=${nextjsDirectory}`
}
fs.appendFileSync(path.join(directory, `.env`), env)
factBoxOptions.interval = displayFactBox({
@@ -183,10 +184,7 @@ export default async ({
await processManager.runProcess({
process: async () => {
const proc = await execute(
[
`npx medusa user -e ${ADMIN_EMAIL} --invite`,
npxOptions,
],
[`npx medusa user -e ${ADMIN_EMAIL} --invite`, npxOptions],
{ verbose, needOutput: true }
)
@@ -2,20 +2,20 @@ import { loadDatabaseConfig } from "../load-module-database-config"
describe("loadDatabaseConfig", function () {
afterEach(() => {
delete process.env.POSTGRES_URL
delete process.env.MEDUSA_POSTGRES_URL
delete process.env.PRODUCT_POSTGRES_URL
delete process.env.DATABASE_URL
delete process.env.MEDUSA_DATABASE_URL
delete process.env.PRODUCT_DATABASE_URL
})
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.PRODUCT_POSTGRES_URL = "postgres://localhost:5432/product"
process.env.POSTGRES_URL = "postgres://localhost:5432/share_db"
process.env.MEDUSA_DATABASE_URL = "postgres://localhost:5432/medusa"
process.env.PRODUCT_DATABASE_URL = "postgres://localhost:5432/product"
process.env.DATABASE_URL = "postgres://localhost:5432/share_db"
let config = loadDatabaseConfig("product")
expect(config).toEqual({
clientUrl: process.env.PRODUCT_POSTGRES_URL,
clientUrl: process.env.PRODUCT_DATABASE_URL,
driverOptions: {
connection: {
ssl: false,
@@ -25,11 +25,11 @@ describe("loadDatabaseConfig", function () {
schema: "",
})
delete process.env.PRODUCT_POSTGRES_URL
delete process.env.PRODUCT_DATABASE_URL
config = loadDatabaseConfig("product")
expect(config).toEqual({
clientUrl: process.env.MEDUSA_POSTGRES_URL,
clientUrl: process.env.MEDUSA_DATABASE_URL,
driverOptions: {
connection: {
ssl: false,
@@ -39,11 +39,11 @@ describe("loadDatabaseConfig", function () {
schema: "",
})
delete process.env.MEDUSA_POSTGRES_URL
delete process.env.MEDUSA_DATABASE_URL
config = loadDatabaseConfig("product")
expect(config).toEqual({
clientUrl: process.env.POSTGRES_URL,
clientUrl: process.env.DATABASE_URL,
driverOptions: {
connection: {
ssl: false,
@@ -55,11 +55,11 @@ describe("loadDatabaseConfig", 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")
expect(config).toEqual({
clientUrl: process.env.POSTGRES_URL,
clientUrl: process.env.DATABASE_URL,
driverOptions: {
connection: {
ssl: {
@@ -71,12 +71,12 @@ describe("loadDatabaseConfig", function () {
schema: "",
})
delete process.env.POSTGRES_URL
process.env.PRODUCT_POSTGRES_URL = "postgres://https://test.com:5432/medusa"
delete process.env.DATABASE_URL
process.env.PRODUCT_DATABASE_URL = "postgres://https://test.com:5432/medusa"
config = loadDatabaseConfig("product")
expect(config).toEqual({
clientUrl: process.env.PRODUCT_POSTGRES_URL,
clientUrl: process.env.PRODUCT_DATABASE_URL,
driverOptions: {
connection: {
ssl: {
@@ -90,7 +90,7 @@ describe("loadDatabaseConfig", 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 = {
database: {
clientUrl: "postgres://localhost:5432/medusa-test",
@@ -112,7 +112,7 @@ describe("loadDatabaseConfig", 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 = {
database: {
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 () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa"
process.env.DATABASE_URL = "postgres://localhost:5432/medusa"
const options = {
database: {
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 () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa"
process.env.DATABASE_URL = "postgres://localhost:5432/medusa"
const options = {
database: {
clientUrl:
@@ -181,7 +181,7 @@ describe("loadDatabaseConfig", 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 = {
database: {
clientUrl:
@@ -212,7 +212,7 @@ describe("loadDatabaseConfig", function () {
}
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
* 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 moduleName
*/
@@ -70,13 +70,13 @@ export function loadDatabaseConfig(
"clientUrl" | "schema" | "driverOptions" | "debug"
> {
const clientUrl =
options?.database?.clientUrl ?? getEnv("POSTGRES_URL", moduleName)
options?.database?.clientUrl ?? getEnv("DATABASE_URL", moduleName)
const database = {
clientUrl,
schema: getEnv("POSTGRES_SCHEMA", moduleName) ?? "public",
schema: getEnv("DATABASE_SCHEMA", moduleName) ?? "public",
driverOptions: JSON.parse(
getEnv("POSTGRES_DRIVER_OPTIONS", moduleName) ||
getEnv("DATABASE_DRIVER_OPTIONS", moduleName) ||
JSON.stringify(getDefaultDriverOptions(clientUrl))
),
debug: false,
@@ -98,7 +98,7 @@ export function loadDatabaseConfig(
if (!database.clientUrl && !silent && !database.connection) {
throw new MedusaError(
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
```bash
POSTGRES_URL=<DATABASE_URL>
DATABASE_URL=<URL_GOES_HERE>
```
3\. Apply database migrations