-
Notifications
You must be signed in to change notification settings - Fork 72
/
testUtilIntegration.ts
55 lines (41 loc) · 1.59 KB
/
testUtilIntegration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import chalk from "chalk";
import cliPrisma from "./packages/cli/prisma";
import webPrisma from "./packages/web/prisma";
// logging
const { DEBUG } = process.env;
const PREFIX = "mailing tests";
export function error(message?: any, ...optionalParams: any[]) {
console.error(chalk.red(PREFIX), message, ...optionalParams);
}
export function debug(message?: any, ...optionalParams: any[]) {
if (DEBUG)
console.info(chalk.yellowBright(PREFIX), message, ...optionalParams);
}
export async function truncateCliDatabase() {
debug("Running TRUNCATE on ", process.env.MAILING_DATABASE_URL_TEST);
await truncateTables(cliPrisma);
}
export async function truncateWebDatabase() {
debug("Running TRUNCATE on ", process.env.WEB_DATABASE_URL_TEST);
await truncateTables(webPrisma);
}
export async function truncateDatabases() {
return Promise.all([truncateCliDatabase(), truncateWebDatabase()]);
}
export async function disconnectDatabases() {
await Promise.all([cliPrisma.$disconnect(), webPrisma.$disconnect()]);
delete global.prismaMailingCli;
delete global.prismaMailingWeb;
}
export interface PrismaTableName {
table_name: string;
}
async function truncateTables(client: typeof cliPrisma | typeof webPrisma) {
const tables =
(await client.$queryRaw`SELECT table_name FROM information_schema.tables where table_schema = 'public' AND table_name NOT like '_prisma%';`) as PrismaTableName[];
const joinedTableNames = tables
.map((t: PrismaTableName) => `"${t.table_name}"`)
.join(", ");
const query = `TRUNCATE ${joinedTableNames} CASCADE;`;
await client.$executeRawUnsafe(query);
}