n8n/packages/cli/test/integration/shared/testDb.ts
Csaba Tuncsik 596c472ecc
feat: RBAC (#8922)
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Val <68596159+valya@users.noreply.github.com>
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
Co-authored-by: Valya Bullions <valya@n8n.io>
Co-authored-by: Danny Martini <danny@n8n.io>
Co-authored-by: Danny Martini <despair.blue@gmail.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: oleg <me@olegivaniv.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: Elias Meire <elias@meire.dev>
Co-authored-by: Giulio Andreini <andreini@netseven.it>
Co-authored-by: Giulio Andreini <g.andreini@gmail.com>
Co-authored-by: Ayato Hayashi <go12limchangyong@gmail.com>
2024-05-17 10:53:15 +02:00

102 lines
2.8 KiB
TypeScript

import type { DataSourceOptions, Repository } from '@n8n/typeorm';
import { DataSource as Connection } from '@n8n/typeorm';
import { Container } from 'typedi';
import type { Class } from 'n8n-core';
import config from '@/config';
import * as Db from '@/Db';
import { getOptionOverrides } from '@db/config';
import { randomString } from './random';
export const testDbPrefix = 'n8n_test_';
/**
* Initialize one test DB per suite run, with bootstrap connection if needed.
*/
export async function init() {
const dbType = config.getEnv('database.type');
const testDbName = `${testDbPrefix}${randomString(6, 10)}_${Date.now()}`;
if (dbType === 'postgresdb') {
const bootstrapPostgres = await new Connection(
getBootstrapDBOptions('postgresdb'),
).initialize();
await bootstrapPostgres.query(`CREATE DATABASE ${testDbName}`);
await bootstrapPostgres.destroy();
config.set('database.postgresdb.database', testDbName);
} else if (dbType === 'mysqldb' || dbType === 'mariadb') {
const bootstrapMysql = await new Connection(getBootstrapDBOptions('mysqldb')).initialize();
await bootstrapMysql.query(`CREATE DATABASE ${testDbName} DEFAULT CHARACTER SET utf8mb4`);
await bootstrapMysql.destroy();
config.set('database.mysqldb.database', testDbName);
}
await Db.init();
await Db.migrate();
}
/**
* Drop test DB, closing bootstrap connection if existing.
*/
export async function terminate() {
await Db.close();
}
// Can't use `Object.keys(entities)` here because some entities have a `Entity` suffix, while the repositories don't
const repositories = [
'AuthIdentity',
'AuthProviderSyncHistory',
'Credentials',
'EventDestinations',
'Execution',
'ExecutionData',
'ExecutionMetadata',
'InstalledNodes',
'InstalledPackages',
'Project',
'ProjectRelation',
'Role',
'Project',
'ProjectRelation',
'Settings',
'SharedCredentials',
'SharedWorkflow',
'Tag',
'User',
'Variables',
'Webhook',
'Workflow',
'WorkflowHistory',
'WorkflowStatistics',
'WorkflowTagMapping',
] as const;
/**
* Truncate specific DB tables in a test DB.
*/
export async function truncate(names: Array<(typeof repositories)[number]>) {
for (const name of names) {
const RepositoryClass: Class<Repository<object>> = (
await import(`@db/repositories/${name.charAt(0).toLowerCase() + name.slice(1)}.repository`)
)[`${name}Repository`];
await Container.get(RepositoryClass).delete({});
}
}
/**
* Generate options for a bootstrap DB connection, to create and drop test databases.
*/
export const getBootstrapDBOptions = (dbType: 'postgresdb' | 'mysqldb'): DataSourceOptions => {
const type = dbType === 'postgresdb' ? 'postgres' : 'mysql';
return {
type,
...getOptionOverrides(dbType),
database: type,
entityPrefix: config.getEnv('database.tablePrefix'),
schema: dbType === 'postgresdb' ? config.getEnv('database.postgresdb.schema') : undefined,
};
};