Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change to use database environment variables in dev mode #8951

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"test:coverage:jest": "NODE_ENV=test PORT=4243 jest --silent --ci --json --coverage --testLocationInResults --outputFile=\"report.json\" --forceExit --testTimeout=10000",
"test:updateSnapshot": "NODE_ENV=test PORT=4243 jest --updateSnapshot --testTimeout=10000",
"seed:setup": "ts-node --compilerOptions '{\"strictNullChecks\": false}' src/test/e2e/seed/segment.seed.ts",
"seed:serve": "UNLEASH_DATABASE_NAME=unleash_test UNLEASH_DATABASE_SCHEMA=seed yarn run start:dev",
"seed:serve": "DATABASE_NAME=unleash_test DATABASE_SCHEMA=seed yarn run start:dev",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yarn seed:serve also use start:dev and it would be better to use the same environment variables instead of using different name such as UNLEASH_DATABASE_NAME.

"clean": "del-cli --force dist",
"heroku-postbuild": "cd frontend && yarn && yarn build",
"prepack": "./scripts/prepack.sh",
Expand Down
25 changes: 17 additions & 8 deletions src/server-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { start } from './lib/server-impl';
import { createConfig } from './lib/create-config';
import { LogLevel } from './lib/logger';
import { ApiTokenType } from './lib/types/models/api-token';
import { parseEnvVarNumber, parseEnvVarBoolean } from './lib/util';

process.nextTick(async () => {
try {
Expand All @@ -10,15 +11,23 @@ process.nextTick(async () => {
db: process.env.DATABASE_URL
? undefined
: {
user: 'unleash_user',
password: 'password',
host: 'localhost',
port: 5432,
database:
process.env.UNLEASH_DATABASE_NAME || 'unleash',
schema: process.env.UNLEASH_DATABASE_SCHEMA,
user: process.env.DATABASE_USERNAME || 'unleash_user',
password: process.env.DATABASE_PASSWORD || 'password',
host: process.env.DATABASE_HOST || 'localhost',
port: parseEnvVarNumber(
process.env.DATABASE_PORT,
5432,
),
database: process.env.DATBASE_NAME || 'unleash',
schema: process.env.DATABASE_SCHEMA,
ssl: false,
applicationName: 'unleash',
applicationName:
process.env.DATABASE_APPLICATION_NAME ||
'unleash',
disableMigration: parseEnvVarBoolean(
process.env.DATABASE_DISABLE_MIGRATION,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to handle migration flag through environment variable in dev mode because sometimes users don't want to run migration and they have to turn it off by fixing code if they can't handle it via environment variable.

false,
),
},
server: {
enableRequestLogger: true,
Expand Down
15 changes: 15 additions & 0 deletions website/docs/contributing/backend/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ export DATABASE_URL=postgres://unleash_user:password@localhost:5432/unleash
export TEST_DATABASE_URL=postgres://unleash_user:password@localhost:5432/unleash_test
```

However, you cal also use your pre-defined database with env vars:
0417taehyun marked this conversation as resolved.
Show resolved Hide resolved

```
export DATABASE_USERNAME=YOUR-DATBASE-USERNAME
0417taehyun marked this conversation as resolved.
Show resolved Hide resolved
export DATABASE_PASSWORD=YOUR-DATABASE-PASSWORD
export DATABASE_HOST=YOUR-DATABASE-HOST
export DATABASE_PORT=YOUR-DATABASE-PORT
export DATABASE_NAME=YOUR-DATABASE-NAME
export DATABASE_SCHEMA=YOUR-DATABASE-SCHEMA
export DATABASE_APPLICATION_NAME=YOUR-DATABASE-APPLICATION_NAME
export DATABASE_DISABLE_MIGRATION=YOUR-DATABASE-DISABLE_MIGRATION
```

You can check what each option means on [Database configuration](../../using-unleash/deploy/configuring-unleash.mdx#database-configuration)
0417taehyun marked this conversation as resolved.
Show resolved Hide resolved

## PostgreSQL with docker {#postgresql-with-docker}

If you don't want to install PostgreSQL locally, you can spin up an Docker instance. We have created a script to ease this process: `scripts/docker-postgres.sh`
Expand Down
Loading