Skip to content

Instantly share code, notes, and snippets.

@dfrankland
Forked from rsaryev/prisma-utils.ts
Created August 16, 2022 14:55
Show Gist options
  • Save dfrankland/833c31d090ae8160d11d83fbce055600 to your computer and use it in GitHub Desktop.
Save dfrankland/833c31d090ae8160d11d83fbce055600 to your computer and use it in GitHub Desktop.
Prisma analogue migrate rest command only programmatically, for testing purposes.
import { Prisma, PrismaClient } from '@prisma/client';
import { exec } from 'child_process';
import * as util from 'util';
const execPromisify = util.promisify(exec);
const prisma = new PrismaClient();
const tables = Prisma.dmmf.datamodel.models
.map((model) => model.dbName)
.filter((table) => table);
const clearMysql = async () => {
await prisma.$transaction([
prisma.$executeRaw`SET FOREIGN_KEY_CHECKS = 0;`,
...tables.map((table) =>
prisma.$executeRawUnsafe(`TRUNCATE ${table};`),
),
prisma.$executeRaw`SET FOREIGN_KEY_CHECKS = 1;`,
]);
};
const clearPostgres = async () => {
await prisma.$transaction([
...tables.map((table) =>
prisma.$executeRawUnsafe(`TRUNCATE ${table} CASCADE;`),
),
]);
};
const clearDefault = async () =>
execPromisify('npx prisma migrate reset --force --skip-seed');
export const clear = async (provider: string) => {
const executeClear = {
mysql: clearMysql,
postgres: clearPostgres,
};
const execute = executeClear[provider] || clearDefault;
return execute();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment