Skip to content

Commit

Permalink
ci: Fix test database cleanup (no-changelog) (#6188)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy authored May 5, 2023
1 parent 82fe638 commit 85a6ace
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
17 changes: 15 additions & 2 deletions packages/cli/test/integration/shared/testDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,36 @@ import type { ICredentialsDb } from '@/Interfaces';

import { DB_INITIALIZATION_TIMEOUT } from './constants';
import { randomApiKey, randomEmail, randomName, randomString, randomValidPassword } from './random';
import { getPostgresSchemaSection } from './utils';
import type {
CollectionName,
CredentialPayload,
InstalledNodePayload,
InstalledPackagePayload,
PostgresSchemaSection,
} from './types';

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.
*/
export async function init() {
jest.setTimeout(DB_INITIALIZATION_TIMEOUT);
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') {
// no bootstrap connection required
Expand Down
17 changes: 0 additions & 17 deletions packages/cli/test/integration/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import type {
EndpointGroup,
InstalledNodePayload,
InstalledPackagePayload,
PostgresSchemaSection,
} from './types';
import { licenseController } from '@/license/license.controller';
import { registerController } from '@/decorators';
Expand Down Expand Up @@ -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
// ----------------------------------
Expand Down
6 changes: 2 additions & 4 deletions packages/cli/test/teardown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'tsconfig-paths/register';
import { DataSource as Connection } from 'typeorm';
import config from '@/config';
import { getBootstrapDBOptions } from './integration/shared/testDb';
import { getBootstrapDBOptions, testDbPrefix } from './integration/shared/testDb';

export default async () => {
const dbType = config.getEnv('database.type').replace(/db$/, '');
Expand All @@ -14,9 +14,7 @@ export default async () => {
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(`${dbType}_`) && dbName.endsWith('_n8n_test'),
)
.filter(({ Database: dbName }) => dbName.startsWith(testDbPrefix))
.map(({ Database: dbName }) => dbName);

const promises = databases.map(async (dbName) => connection.query(`DROP DATABASE ${dbName};`));
Expand Down

0 comments on commit 85a6ace

Please sign in to comment.