Update: create raw seed query to truncate all tables using plp/pgsql

This commit is contained in:
Aaron William Po
2023-05-04 23:40:44 -04:00
parent 7922b6107b
commit 2f8d7d6abb

View File

@@ -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();
};