2021-07-19 15:51:50 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
|
import * as dotenv from 'dotenv';
|
|
|
|
|
import * as fs from 'fs';
|
2023-10-11 06:59:07 +00:00
|
|
|
import { ExecFileSyncOptions, exec, execFileSync } 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 dropDatabaseFromFile(envPath: string): void {
|
2021-07-19 15:51:50 +00:00
|
|
|
const result = dotenv.config({ path: envPath });
|
|
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
|
throw result.error;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 14:58:36 +00:00
|
|
|
dropDatabase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dropDatabase(): 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-12-27 10:48:36 +00:00
|
|
|
// Allow dropping db based on cmd line arg
|
|
|
|
|
const dbNameFromArg = process.argv[2];
|
|
|
|
|
if (dbNameFromArg) return dropDb(envVars, dbNameFromArg);
|
|
|
|
|
|
|
|
|
|
dropDb(envVars, envVars.PG_DB);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 06:59:07 +00:00
|
|
|
function checkCommandAvailable(command: string) {
|
|
|
|
|
try {
|
|
|
|
|
const options = { env: Object.assign({}, process.env) } as ExecFileSyncOptions;
|
|
|
|
|
execFileSync('which', [command], options);
|
2025-07-09 17:06:41 +00:00
|
|
|
} catch {
|
2023-10-11 06:59:07 +00:00
|
|
|
throw `Error: ${command} not found. Make sure it's installed and available in the system's PATH.`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2023-10-11 06:59:07 +00:00
|
|
|
function dropDb(envVars, dbName) {
|
|
|
|
|
const env = Object.assign({}, process.env, { PGPASSWORD: envVars.PG_PASS });
|
|
|
|
|
const dropDbArgs = ['-h', envVars.PG_HOST, '-p', envVars.PG_PORT, '-U', envVars.PG_USER, dbName];
|
|
|
|
|
const options = { env, stdio: 'pipe' } as ExecFileSyncOptions;
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2023-10-11 06:59:07 +00:00
|
|
|
try {
|
|
|
|
|
execFileSync('dropdb', dropDbArgs, options);
|
|
|
|
|
console.log(`Dropped database ${dbName}`);
|
|
|
|
|
} catch (error) {
|
2022-12-27 10:48:36 +00:00
|
|
|
const errorMessage = `database "${dbName}" does not exist`;
|
2021-08-13 15:25:43 +00:00
|
|
|
|
2023-10-11 06:59:07 +00:00
|
|
|
if (error.message.includes(errorMessage)) {
|
2022-05-31 13:12:36 +00:00
|
|
|
console.log(errorMessage);
|
|
|
|
|
} else {
|
2023-10-11 06:59:07 +00:00
|
|
|
console.error(errorMessage);
|
2022-05-31 13:12:36 +00:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2023-10-11 06:59:07 +00:00
|
|
|
}
|
2021-07-30 14:58:36 +00:00
|
|
|
}
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2023-10-11 06:59:07 +00:00
|
|
|
try {
|
|
|
|
|
checkCommandAvailable('dropdb');
|
|
|
|
|
const nodeEnvPath = path.resolve(process.cwd(), process.env.NODE_ENV === 'test' ? '../.env.test' : '../.env');
|
2021-07-23 14:16:01 +00:00
|
|
|
|
2023-10-11 06:59:07 +00:00
|
|
|
const fallbackPath = path.resolve(process.cwd(), '../.env');
|
2021-07-19 15:51:50 +00:00
|
|
|
|
2023-10-11 06:59:07 +00:00
|
|
|
if (fs.existsSync(nodeEnvPath)) {
|
|
|
|
|
dropDatabaseFromFile(nodeEnvPath);
|
|
|
|
|
} else if (fs.existsSync(fallbackPath)) {
|
|
|
|
|
dropDatabaseFromFile(fallbackPath);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`${nodeEnvPath} file not found to drop database\n` + 'Picking up config from the environment');
|
|
|
|
|
dropDatabase();
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
process.exit(1);
|
2021-07-19 15:51:50 +00:00
|
|
|
}
|