ci: Fix test database cleanup (no-changelog) (#6188)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-05-05 10:12:37 +00:00 committed by GitHub
parent 82fe6383ef
commit 85a6ace56b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 23 deletions

View file

@ -25,23 +25,36 @@ import type { ICredentialsDb } from '@/Interfaces';
import { DB_INITIALIZATION_TIMEOUT } from './constants'; import { DB_INITIALIZATION_TIMEOUT } from './constants';
import { randomApiKey, randomEmail, randomName, randomString, randomValidPassword } from './random'; import { randomApiKey, randomEmail, randomName, randomString, randomValidPassword } from './random';
import { getPostgresSchemaSection } from './utils';
import type { import type {
CollectionName, CollectionName,
CredentialPayload, CredentialPayload,
InstalledNodePayload, InstalledNodePayload,
InstalledPackagePayload, InstalledPackagePayload,
PostgresSchemaSection,
} from './types'; } from './types';
export type TestDBType = 'postgres' | 'mysql'; export type TestDBType = 'postgres' | 'mysql';
export const testDbPrefix = 'n8n_test_';
export function getPostgresSchemaSection(
schema = config.getSchema(),
): PostgresSchemaSection | null {
for (const [key, value] of Object.entries(schema)) {
if (key === 'postgresdb') {
return value._cvtProperties;
}
}
return null;
}
/** /**
* Initialize one test DB per suite run, with bootstrap connection if needed. * Initialize one test DB per suite run, with bootstrap connection if needed.
*/ */
export async function init() { export async function init() {
jest.setTimeout(DB_INITIALIZATION_TIMEOUT); jest.setTimeout(DB_INITIALIZATION_TIMEOUT);
const dbType = config.getEnv('database.type'); const dbType = config.getEnv('database.type');
const testDbName = `n8n_test_${randomString(6, 10)}_${Date.now()}`; const testDbName = `${testDbPrefix}${randomString(6, 10)}_${Date.now()}`;
if (dbType === 'sqlite') { if (dbType === 'sqlite') {
// no bootstrap connection required // no bootstrap connection required

View file

@ -51,7 +51,6 @@ import type {
EndpointGroup, EndpointGroup,
InstalledNodePayload, InstalledNodePayload,
InstalledPackagePayload, InstalledPackagePayload,
PostgresSchemaSection,
} from './types'; } from './types';
import { licenseController } from '@/license/license.controller'; import { licenseController } from '@/license/license.controller';
import { registerController } from '@/decorators'; import { registerController } from '@/decorators';
@ -760,22 +759,6 @@ export const setInstanceOwnerSetUp = async (value: boolean) => {
); );
}; };
// ----------------------------------
// misc
// ----------------------------------
export function getPostgresSchemaSection(
schema = config.getSchema(),
): PostgresSchemaSection | null {
for (const [key, value] of Object.entries(schema)) {
if (key === 'postgresdb') {
return value._cvtProperties;
}
}
return null;
}
// ---------------------------------- // ----------------------------------
// community nodes // community nodes
// ---------------------------------- // ----------------------------------

View file

@ -1,7 +1,7 @@
import 'tsconfig-paths/register'; import 'tsconfig-paths/register';
import { DataSource as Connection } from 'typeorm'; import { DataSource as Connection } from 'typeorm';
import config from '@/config'; import config from '@/config';
import { getBootstrapDBOptions } from './integration/shared/testDb'; import { getBootstrapDBOptions, testDbPrefix } from './integration/shared/testDb';
export default async () => { export default async () => {
const dbType = config.getEnv('database.type').replace(/db$/, ''); const dbType = config.getEnv('database.type').replace(/db$/, '');
@ -14,9 +14,7 @@ export default async () => {
dbType === 'postgres' ? 'SELECT datname as "Database" FROM pg_database' : 'SHOW DATABASES'; dbType === 'postgres' ? 'SELECT datname as "Database" FROM pg_database' : 'SHOW DATABASES';
const results: Array<{ Database: string }> = await connection.query(query); const results: Array<{ Database: string }> = await connection.query(query);
const databases = results const databases = results
.filter( .filter(({ Database: dbName }) => dbName.startsWith(testDbPrefix))
({ Database: dbName }) => dbName.startsWith(`${dbType}_`) && dbName.endsWith('_n8n_test'),
)
.map(({ Database: dbName }) => dbName); .map(({ Database: dbName }) => dbName);
const promises = databases.map(async (dbName) => connection.query(`DROP DATABASE ${dbName};`)); const promises = databases.map(async (dbName) => connection.query(`DROP DATABASE ${dbName};`));