N
Neon2y ago
other-emerald

remove all data from branch?

Whenever we push a commit to an open PR, we are kicking off a new preview Vercel deployment. At the same time we are running a seed script. The issue is that if a previous commit exists on that PR, we would be duplicating the data. Is there a way to wipe the data so that the seed script basically refreshes the branch on every commit? Thanks!
2 Replies
other-emerald
other-emeraldOP2y ago
For now we added:
await db.execute(sql`
DO $$ DECLARE
r RECORD;
BEGIN
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
`)
await db.execute(sql`
DO $$ DECLARE
r RECORD;
BEGIN
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
`)
At the top of our seed script which only runs when VERCEL_ENV is preview. Works for us since our tables are all in the public schema...
sensitive-blue
sensitive-blue2y ago
Oh yes, this'd work just fine. This was also what I suggested in https://discord.com/channels/1176467419317940276/1241319099968262164/1241328843671732234. Another great solve, @juanvilladev!

Did you find this page helpful?