enforce config database

This commit is contained in:
Ulf Gebhardt 2022-03-18 03:13:11 +01:00
parent 677581b76e
commit c9177bbce3
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
3 changed files with 25 additions and 1 deletions

View File

@ -1,3 +1,5 @@
CONFIG_VERSION=v1.2022-03-18
DB_HOST=localhost
DB_PORT=3306
DB_USER=root

View File

@ -1,3 +1,5 @@
CONFIG_VERSION=$DATABASE_CONFIG_VERSION
DB_HOST=localhost
DB_PORT=3306
DB_USER=$DB_USER

View File

@ -3,6 +3,14 @@
import dotenv from 'dotenv'
dotenv.config()
const constants = {
CONFIG_VERSION: {
DEFAULT: 'DEFAULT',
EXPECTED: 'v1.2022-03-18',
CURRENT: '',
},
}
const database = {
DB_HOST: process.env.DB_HOST || 'localhost',
DB_PORT: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 3306,
@ -15,6 +23,18 @@ const migrations = {
MIGRATIONS_TABLE: process.env.MIGRATIONS_TABLE || 'migrations',
}
const CONFIG = { ...database, ...migrations }
// Check config version
constants.CONFIG_VERSION.CURRENT = process.env.CONFIG_VERSION || constants.CONFIG_VERSION.DEFAULT
if (
![constants.CONFIG_VERSION.EXPECTED, constants.CONFIG_VERSION.DEFAULT].includes(
constants.CONFIG_VERSION.CURRENT,
)
) {
throw new Error(
`Fatal: Config Version incorrect - expected "${constants.CONFIG_VERSION.EXPECTED}" or "${constants.CONFIG_VERSION.DEFAULT}", but found "${constants.CONFIG_VERSION.CURRENT}"`,
)
}
const CONFIG = { ...constants, ...database, ...migrations }
export default CONFIG