n8n/packages/cli/test/teardown.ts
कारतोफ्फेलस्क्रिप्ट™ c7d4b471c4
refactor(core): Move backend config to a separate package (no-changelog) (#9325)
2024-07-05 11:43:27 +02:00

27 lines
1,009 B
TypeScript

import 'tsconfig-paths/register';
import { Container } from 'typedi';
import { GlobalConfig } from '@n8n/config';
import { DataSource as Connection } from '@n8n/typeorm';
import { getBootstrapDBOptions, testDbPrefix } from './integration/shared/testDb';
export default async () => {
const { type: dbType } = Container.get(GlobalConfig).database;
if (dbType !== 'postgresdb' && dbType !== 'mysqldb') return;
const connection = new Connection(getBootstrapDBOptions(dbType));
await connection.initialize();
const query =
dbType === 'postgresdb' ? '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) => await connection.query(`DROP DATABASE ${dbName};`),
);
await Promise.all(promises);
await connection.destroy();
};