-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move test database URL to environment variable (#493)
DEFRA/water-abstraction-team#64 We currently hardcode the url of the test database in our `package.json` which isn't good practice, and also causes us problems when running within a Docker environment (as per the linked issue). We want to use `DATABASE_URL` instead as `db-migrate` will use this url if it exists. Unfortunately this causes an issue when we want to run migrations against different dbs in the same environment -- eg. normally we want migrations to run against our regular db but when running unit tests, we want migrations to run against the test db. `db-migrate` does allow for setting different environments in `database.json` (as in test, prod etc.) but this then moves configuration away from our `.env` file -- not what we want when our environments are more like "local", "local test", "CI test", etc. We therefore create a script `scripts/migrate.js`, which checks `NODE_ENV` to see if the environment is `test` or not and sets `DATABASE_URL` accordingly before running `db-migrate up`: - If `NODE_ENV` is `test` then it sets `DATABASE_URL` to the value of `TEST_DATABASE_URL` in the `.env` file; - Otherwise, it simply uses `DATABASE_URL` as-is. We update our `package.json` so that this is run whenever we would normally run `db-migrate up`. Since `lab` sets `NODE_ENV` to `test`, we don't need to make any changes to our `test` script. We also update `config.js` to also check `NODE_ENV` when setting the db connection string -- again, if `NODE_ENV` is `test` then we use `TEST_DATABASE_URL`, otherwise we use `DATABASE_URL`. This therefore means that things like the "create schema" script don't need to be changed as these use the db connection string in `config.js`.
- Loading branch information
Showing
6 changed files
with
35 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ RETURNS_URI= | |
WATER_URI= | ||
CRM_URI= | ||
|
||
DATABASE_URL= | ||
TEST_DATABASE_URL= | ||
|
||
S3_KEY= | ||
S3_SECRET= | ||
S3_BUCKET= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict' | ||
|
||
require('dotenv').config() | ||
|
||
// We use promisify to wrap exec in a promise. This allows us to await it without resorting to using callbacks. | ||
const util = require('util') | ||
const exec = util.promisify(require('child_process').exec) | ||
|
||
async function run () { | ||
const databaseUrl = process.env.NODE_ENV !== 'test' ? process.env.DATABASE_URL : process.env.TEST_DATABASE_URL | ||
|
||
try { | ||
const { stdout, stderr } = await exec(`export DATABASE_URL=${databaseUrl} && db-migrate up --verbose`) | ||
|
||
const output = stderr ? `ERROR: ${stderr}` : stdout.replace('\n', '') | ||
console.log(output) | ||
|
||
process.exit(stderr ? 1 : 0) | ||
} catch (error) { | ||
console.log(`ERROR: ${error.message}`) | ||
|
||
process.exit(1) | ||
} | ||
} | ||
|
||
run() |