mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Update: create raw seed query to truncate all tables using plp/pgsql
This commit is contained in:
@@ -2,15 +2,26 @@ import DBClient from '../../DBClient';
|
||||
|
||||
const cleanDatabase = async () => {
|
||||
const prisma = DBClient.instance;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "User" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BeerPost" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BeerType" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BreweryPost" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BeerComment" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BreweryComment" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BeerPostLike" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BeerImage" CASCADE`;
|
||||
await prisma.$executeRaw`TRUNCATE TABLE "BreweryImage" CASCADE`;
|
||||
|
||||
/**
|
||||
* Truncate all tables in the database.
|
||||
*
|
||||
* Declares a cursor called statements that contains all table names in the public
|
||||
* schema then loops through each table and truncates it.
|
||||
*/
|
||||
await prisma.$executeRaw`
|
||||
DO $$
|
||||
DECLARE
|
||||
statements CURSOR FOR
|
||||
SELECT tablename FROM pg_tables
|
||||
WHERE schemaname = 'public';
|
||||
BEGIN
|
||||
FOR statement IN statements LOOP
|
||||
EXECUTE 'TRUNCATE TABLE ' || quote_ident(statement.tablename) || ' CASCADE;';
|
||||
END LOOP;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
`;
|
||||
|
||||
await prisma.$disconnect();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user