diff --git a/src/prisma/seed/clean/cleanDatabase.ts b/src/prisma/seed/clean/cleanDatabase.ts index af0135a..f62e49d 100644 --- a/src/prisma/seed/clean/cleanDatabase.ts +++ b/src/prisma/seed/clean/cleanDatabase.ts @@ -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(); };