mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
24 lines
940 B
TypeScript
24 lines
940 B
TypeScript
import 'tsconfig-paths/register';
|
|
import { DataSource as Connection } from 'typeorm';
|
|
import config from '@/config';
|
|
import { getBootstrapDBOptions, testDbPrefix } from './integration/shared/testDb';
|
|
|
|
export default async () => {
|
|
const dbType = config.getEnv('database.type').replace(/db$/, '');
|
|
if (dbType !== 'postgres' && dbType !== 'mysql') return;
|
|
|
|
const connection = new Connection(getBootstrapDBOptions(dbType));
|
|
await connection.initialize();
|
|
|
|
const query =
|
|
dbType === 'postgres' ? 'SELECT datname as "Database" FROM pg_database' : 'SHOW DATABASES';
|
|
const results: Array<{ Database: string }> = await connection.query(query);
|
|
const databases = results
|
|
.filter(({ Database: dbName }) => dbName.startsWith(testDbPrefix))
|
|
.map(({ Database: dbName }) => dbName);
|
|
|
|
const promises = databases.map(async (dbName) => connection.query(`DROP DATABASE ${dbName};`));
|
|
await Promise.all(promises);
|
|
await connection.destroy();
|
|
};
|