Falls back to SQLite if no environment variables are provided

This commit is contained in:
Julian Reinhardt 2021-11-02 12:22:39 +01:00
parent 3b47decbb0
commit ffe3db8c08

View File

@ -44,84 +44,85 @@ async function appStart () {
async function createDbConfigFromEnvironment() { async function createDbConfigFromEnvironment() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const envMysqlHost = process.env.DB_MYSQL_HOST || null; const envMysqlHost = process.env.DB_MYSQL_HOST || null;
const envMysqlPort = process.env.DB_MYSQL_PORT || null; const envMysqlPort = process.env.DB_MYSQL_PORT || null;
const envMysqlUser = process.env.DB_MYSQL_USER || null; const envMysqlUser = process.env.DB_MYSQL_USER || null;
const envMysqlName = process.env.DB_MYSQL_NAME || null; const envMysqlName = process.env.DB_MYSQL_NAME || null;
const envSqliteFile = process.env.DB_SQLITE_FILE || null; let envSqliteFile = process.env.DB_SQLITE_FILE || null;
if ((envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) || envSqliteFile) { const fs = require('fs');
const fs = require('fs'); const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json';
const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json'; let configData = {};
let configData = {};
try { try {
configData = require(filename); configData = require(filename);
} catch (err) { } catch (err) {
// do nothing // do nothing
} }
if (configData.database && configData.database.engine && !configData.database.fromEnv) { if (configData.database && configData.database.engine && !configData.database.fromEnv) {
logger.info('Manual db configuration already exists, skipping config creation from environment variables'); logger.info('Manual db configuration already exists, skipping config creation from environment variables');
resolve();
return;
}
if ((!envMysqlHost || !envMysqlPort || !envMysqlUser || !envMysqlName) && !envSqliteFile){
envSqliteFile = '/data/database.sqlite';
logger.info(`No valid environment variables for database provided, using default SQLite file '${envSqliteFile}'`);
}
if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) {
const newConfig = {
fromEnv: true,
engine: 'mysql',
host: envMysqlHost,
port: envMysqlPort,
user: envMysqlUser,
password: process.env.DB_MYSQL_PASSWORD,
name: envMysqlName,
};
if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
// Config is unchanged, skip overwrite
resolve(); resolve();
return; return;
} }
if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) { logger.info('Generating MySQL knex configuration from environment variables');
const newConfig = { configData.database = newConfig;
fromEnv: true,
engine: 'mysql',
host: envMysqlHost,
port: envMysqlPort,
user: envMysqlUser,
password: process.env.DB_MYSQL_PASSWORD,
name: envMysqlName,
};
if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) { } else {
// Config is unchanged, skip overwrite const newConfig = {
resolve(); fromEnv: true,
return; engine: 'knex-native',
knex: {
client: 'sqlite3',
connection: {
filename: envSqliteFile
},
useNullAsDefault: true
} }
};
logger.info('Generating MySQL db configuration from environment variables'); if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
configData.database = newConfig; // Config is unchanged, skip overwrite
resolve();
} else { return;
const newConfig = {
fromEnv: true,
engine: 'knex-native',
knex: {
client: 'sqlite3',
connection: {
filename: envSqliteFile
},
useNullAsDefault: true
}
};
if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) {
// Config is unchanged, skip overwrite
resolve();
return;
}
logger.info('Generating Sqlite db configuration from environment variables');
configData.database = newConfig;
} }
// Write config logger.info('Generating SQLite knex configuration');
fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => { configData.database = newConfig;
if (err) {
logger.error('Could not write db config to config file: ' + filename);
reject(err);
} else {
logger.info('Wrote db configuration to config file: ' + filename);
resolve();
}
});
} else {
resolve();
} }
// Write config
fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => {
if (err) {
logger.error('Could not write db config to config file: ' + filename);
reject(err);
} else {
logger.debug('Wrote db configuration to config file: ' + filename);
resolve();
}
});
}); });
} }