Skip to content

feat: Standardize environment variables for database connection. #4678

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: Standardize environment variables for database connection.
Add `DB_ENGINE` environment variable, and unify alls `DB_*` redundant environment variables.
fixes #4677
  • Loading branch information
EdwinBetanc0urt committed Jul 25, 2025
commit 03d0b906dbc508bc4e3b0bf16ee819f610d3ae77
4 changes: 2 additions & 2 deletions backend/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"database": {
"engine": "mysql2",
"host": "db",
"port": 3306,
"name": "npm",
"user": "npm",
"password": "npm",
"port": 3306
"password": "npm"
}
}
68 changes: 65 additions & 3 deletions backend/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,30 @@ const logger = require('../logger').global;
const keysFile = '/data/keys.json';
const mysqlEngine = 'mysql2';
const postgresEngine = 'pg';
const sqliteEngine = 'knex-native';
const sqliteClientName = 'sqlite3';

const dataBaseEngines = {
mysql: {
engine: mysqlEngine,
host: '127.0.0.1',
port: 3306,
},
mariadb: {
engine: mysqlEngine,
host: '127.0.0.1',
port: 3306,
},
postgres: {
engine: postgresEngine,
host: '127.0.0.1',
port: 5432,
},
sqlite: {
engine: sqliteEngine,
},
}

let instance = null;

// 1. Load from config file first (not recommended anymore)
Expand All @@ -29,6 +51,45 @@ const configure = () => {
}
}

const envDataBaseEngne = process.env.DB_ENGINE || null;
if (envDataBaseEngne) {
if (envDataBaseEngne === 'sqlite') {
logger.info(`Using Sqlite: ${envSqliteFile}`);
const defaultConection = dataBaseEngines[envDataBaseEngne]
const envSqliteFile = process.env.DB_SQLITE_FILE || '/data/database.sqlite';
instance = {
database: {
engine: defaultConection.engine,
knex: {
client: sqliteClientName,
connection: {
filename: envSqliteFile
},
useNullAsDefault: true
}
},
keys: getKeys(),
};
} else {
// we have enough mysql/mariadb/postgres creds to go with mysql/mariadb/postgres
logger.info(`Using ${envDataBaseEngne} configuration`);
const defaultConection = dataBaseEngines[envDataBaseEngne]
instance = {
database: {
engine: defaultConection.engine,
host: process.env.DB_HOST || defaultConection.host,
port: process.env.DB_PORT || defaultConection.port,
name: process.env.DB_NAME || 'npm',
user: process.env.DB_USER || 'npm',
password: process.env.DB_PASSWORD || 'npmpass',
},
keys: getKeys(),
};
}
return;
}

// TODO: Remove this section in future versions; it is retained for backward compatibility.
const envMysqlHost = process.env.DB_MYSQL_HOST || null;
const envMysqlUser = process.env.DB_MYSQL_USER || null;
const envMysqlName = process.env.DB_MYSQL_NAME || null;
Expand All @@ -49,6 +110,7 @@ const configure = () => {
return;
}

// TODO: Remove this section in future versions; it is retained for backward compatibility.
const envPostgresHost = process.env.DB_POSTGRES_HOST || null;
const envPostgresUser = process.env.DB_POSTGRES_USER || null;
const envPostgresName = process.env.DB_POSTGRES_NAME || null;
Expand All @@ -73,7 +135,7 @@ const configure = () => {
logger.info(`Using Sqlite: ${envSqliteFile}`);
instance = {
database: {
engine: 'knex-native',
engine: sqliteEngine,
knex: {
client: sqliteClientName,
connection: {
Expand Down Expand Up @@ -170,15 +232,15 @@ module.exports = {
},

/**
* Is this a mysql configuration?
* Is this a MySQL/MariaDB configuration?
*
* @returns {boolean}
*/
isMysql: function () {
instance === null && configure();
return instance.database.engine === mysqlEngine;
},

/**
* Is this a postgres configuration?
*
Expand Down
11 changes: 6 additions & 5 deletions docker/docker-compose.ci.mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ services:

fullstack:
environment:
DB_MYSQL_HOST: 'db-mysql'
DB_MYSQL_PORT: '3306'
DB_MYSQL_USER: 'npm'
DB_MYSQL_PASSWORD: 'npmpass'
DB_MYSQL_NAME: 'npm'
DB_ENGINE: 'mysql'
DB_HOST: 'db-mysql'
DB_PORT: '3306'
DB_NAME: 'npm'
DB_USER: 'npm'
DB_PASSWORD: 'npmpass'
depends_on:
- db-mysql

Expand Down
11 changes: 6 additions & 5 deletions docker/docker-compose.ci.postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ services:

fullstack:
environment:
DB_POSTGRES_HOST: 'db-postgres'
DB_POSTGRES_PORT: '5432'
DB_POSTGRES_USER: 'npm'
DB_POSTGRES_PASSWORD: 'npmpass'
DB_POSTGRES_NAME: 'npm'
DB_ENGINE: 'postgres'
DB_HOST: 'db-postgres'
DB_PORT: '5432'
DB_NAME: 'npm'
DB_USER: 'npm'
DB_PASSWORD: 'npmpass'
depends_on:
- db-postgres
- authentik
Expand Down
23 changes: 13 additions & 10 deletions docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ services:
DEVELOPMENT: 'true'
LE_STAGING: 'true'
# db:
# DB_MYSQL_HOST: 'db'
# DB_MYSQL_PORT: '3306'
# DB_MYSQL_USER: 'npm'
# DB_MYSQL_PASSWORD: 'npm'
# DB_MYSQL_NAME: 'npm'
# DB_ENGINE: 'mysql'
# DB_HOST: 'db'
# DB_PORT: '3306'
# DB_NAME: 'npm'
# DB_USER: 'npm'
# DB_PASSWORD: 'npmpass'
# db-postgres:
DB_POSTGRES_HOST: 'db-postgres'
DB_POSTGRES_PORT: '5432'
DB_POSTGRES_USER: 'npm'
DB_POSTGRES_PASSWORD: 'npmpass'
DB_POSTGRES_NAME: 'npm'
DB_ENGINE: 'postgres'
DB_HOST: 'db-postgres'
DB_PORT: '5432'
DB_NAME: 'npm'
DB_USER: 'npm'
DB_PASSWORD: 'npmpass'
# DB_ENGINE: 'sqlite'
# DB_SQLITE_FILE: "/data/database.sqlite"
# DISABLE_IPV6: "true"
# Required for DNS Certificate provisioning testing:
Expand Down
13 changes: 7 additions & 6 deletions docs/src/advanced-config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ services:
- '81:81'
environment:
# These are the settings to access your db
DB_MYSQL_HOST: "db"
DB_MYSQL_PORT: 3306
DB_ENGINE: "mysql"
DB_HOST: "db"
DB_PORT: 3306
DB_NAME: "npm"
DB_MYSQL_USER: "npm"
# DB_MYSQL_PASSWORD: "npm" # use secret instead
DB_MYSQL_PASSWORD__FILE: /run/secrets/MYSQL_PWD
DB_MYSQL_NAME: "npm"
# DB_PASSWORD: "npmpass" # use secret instead
DB_PASSWORD__FILE: /run/secrets/MYSQL_PWD
# If you would rather use Sqlite, remove all DB_MYSQL_* lines above
# Uncomment this if IPv6 is not enabled on your host
# DISABLE_IPV6: 'true'
Expand All @@ -137,7 +138,7 @@ services:
MYSQL_ROOT_PASSWORD__FILE: /run/secrets/DB_ROOT_PWD
MYSQL_DATABASE: "npm"
MYSQL_USER: "npm"
# MYSQL_PASSWORD: "npm" # use secret instead
# MYSQL_PASSWORD: "npmpass" # use secret instead
MYSQL_PASSWORD__FILE: /run/secrets/MYSQL_PWD
MARIADB_AUTO_UPGRADE: '1'
volumes:
Expand Down
30 changes: 16 additions & 14 deletions docs/src/setup/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ If you opt for the MySQL configuration you will have to provide the database ser
It's easy to use another docker container for your database also and link it as part of the docker stack, so that's what the following examples
are going to use.

Here is an example of what your `docker-compose.yml` will look like when using a MariaDB container:
Use `DB_ENGINE` environment variable with `mysql`/`mariadb` value, here is an example of what your `docker-compose.yml` will look like when using a MariaDB container:

```yml
services:
Expand All @@ -65,12 +65,13 @@ services:
# Add any other Stream port you want to expose
# - '21:21' # FTP
environment:
# Mysql/Maria connection parameters:
DB_MYSQL_HOST: "db"
DB_MYSQL_PORT: 3306
DB_MYSQL_USER: "npm"
DB_MYSQL_PASSWORD: "npm"
DB_MYSQL_NAME: "npm"
# MySQL/MariaDB connection parameters:
DB_ENGINE: 'mysql' # It also works with `mariadb`
DB_HOST: "db"
DB_PORT: 3306
DB_NAME: "npm"
DB_USER: "npm"
DB_PASSWORD: "npm"
# Uncomment this if IPv6 is not enabled on your host
# DISABLE_IPV6: 'true'
volumes:
Expand Down Expand Up @@ -100,7 +101,7 @@ Please note, that `DB_MYSQL_*` environment variables will take precedent over `D

## Using Postgres database

Similar to the MySQL server setup:
Use `DB_ENGINE` environment variable with `postgres` value, similar to the MySQL server setup:

```yml
services:
Expand All @@ -115,12 +116,13 @@ services:
# Add any other Stream port you want to expose
# - '21:21' # FTP
environment:
# Postgres parameters:
DB_POSTGRES_HOST: 'db'
DB_POSTGRES_PORT: '5432'
DB_POSTGRES_USER: 'npm'
DB_POSTGRES_PASSWORD: 'npmpass'
DB_POSTGRES_NAME: 'npm'
# PostgreSQL parameters:
DB_ENGINE: 'postgres'
DB_HOST: 'db'
DB_PORT: '5432'
DB_NAME: 'npm'
DB_USER: 'npm'
DB_PASSWORD: 'npmpass'
# Uncomment this if IPv6 is not enabled on your host
# DISABLE_IPV6: 'true'
volumes:
Expand Down