2021-07-19 15:51:50 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as dotenv from 'dotenv';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import { exec } from 'child_process';
|
2021-08-02 08:21:48 +00:00
|
|
|
import { buildAndValidateDatabaseConfig } from './database-config-utils';
|
2021-07-30 14:58:36 +00:00
|
|
|
|
|
|
|
|
function createDatabaseFromFile(envPath: string): void {
|
2021-07-19 15:51:50 +00:00
|
|
|
const result = dotenv.config({ path: envPath });
|
|
|
|
|
|
2022-09-16 15:38:45 +00:00
|
|
|
if (process.env.PG_DB_OWNER === 'false') {
|
|
|
|
|
console.log('Skipping database creation');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 15:51:50 +00:00
|
|
|
if (result.error) {
|
|
|
|
|
throw result.error;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 14:58:36 +00:00
|
|
|
createDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createDatabase(): void {
|
2021-08-02 08:21:48 +00:00
|
|
|
const { value: envVars, error } = buildAndValidateDatabaseConfig();
|
2021-07-30 14:58:36 +00:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
throw new Error(`Config validation error: ${error.message}`);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 13:12:36 +00:00
|
|
|
const connectivityCheck = exec('command -v createdb');
|
|
|
|
|
|
|
|
|
|
connectivityCheck.on('exit', function (signal) {
|
|
|
|
|
if (signal === 1) {
|
|
|
|
|
console.error('Unable to connect to database');
|
|
|
|
|
process.exit(1);
|
2021-07-19 15:51:50 +00:00
|
|
|
}
|
2022-05-31 13:12:36 +00:00
|
|
|
});
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2022-09-16 16:27:33 +00:00
|
|
|
if (envVars.PG_DB_OWNER === 'false') {
|
|
|
|
|
console.log('Skipping database creation');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 13:12:36 +00:00
|
|
|
const createdb =
|
2022-12-22 20:39:57 +00:00
|
|
|
`PGPASSWORD="${envVars.PG_PASS}" createdb ` +
|
2022-05-31 13:12:36 +00:00
|
|
|
`-h ${envVars.PG_HOST} ` +
|
|
|
|
|
`-p ${envVars.PG_PORT} ` +
|
|
|
|
|
`-U ${envVars.PG_USER} ` +
|
2022-12-22 20:39:57 +00:00
|
|
|
envVars.PG_DB;
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2022-05-31 13:12:36 +00:00
|
|
|
exec(createdb, (err, _stdout, _stderr) => {
|
|
|
|
|
if (!err) {
|
|
|
|
|
console.log(`Created database ${envVars.PG_DB}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2022-05-31 13:12:36 +00:00
|
|
|
const errorMessage = `database "${envVars.PG_DB}" already exists`;
|
2021-08-13 15:25:43 +00:00
|
|
|
|
2022-05-31 13:12:36 +00:00
|
|
|
if (err.message.includes(errorMessage)) {
|
|
|
|
|
console.log(`Using database: ${envVars.PG_DB}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.error(err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2021-07-19 15:51:50 +00:00
|
|
|
});
|
2021-07-30 14:58:36 +00:00
|
|
|
}
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const nodeEnvPath = path.resolve(process.cwd(), process.env.NODE_ENV === 'test' ? '../.env.test' : '../.env');
|
2021-07-23 14:16:01 +00:00
|
|
|
|
2021-07-19 17:08:59 +00:00
|
|
|
const fallbackPath = path.resolve(process.cwd(), '../.env');
|
2021-07-19 15:51:50 +00:00
|
|
|
|
|
|
|
|
if (fs.existsSync(nodeEnvPath)) {
|
2021-07-30 14:58:36 +00:00
|
|
|
createDatabaseFromFile(nodeEnvPath);
|
2021-07-19 15:51:50 +00:00
|
|
|
} else if (fs.existsSync(fallbackPath)) {
|
2021-07-30 14:58:36 +00:00
|
|
|
createDatabaseFromFile(fallbackPath);
|
2021-07-19 15:51:50 +00:00
|
|
|
} else {
|
2022-05-31 13:12:36 +00:00
|
|
|
console.log('Picking up config from the environment');
|
2021-07-30 14:58:36 +00:00
|
|
|
|
|
|
|
|
createDatabase();
|
2021-07-19 15:51:50 +00:00
|
|
|
}
|