import fs from 'node:fs'; import path from 'node:path'; import {fileURLToPath} from 'node:url'; const root=fileURLToPath(new URL('../../',import.meta.url)); const files=JSON.parse(fs.readFileSync(new URL('manifest.json',import.meta.url),'utf8')); let sql=`-- Caterium fresh recovery; target usfjwhztqoopzzfmfbis only.\n-- Do not run against an existing database.\nbegin;\nset local lock_timeout='10s';\nset local statement_timeout='120s';\ndo $$begin if to_regclass('public.sun_workspaces') is not null then raise exception 'Fresh recovery requires an empty Caterium schema'; end if; end $$;\ncreate extension if not exists pg_cron;\n`; for(const file of files)sql+=`\n-- Source: ${file}\n`+fs.readFileSync(path.join(root,file),'utf8').replace(/^\s*(begin|commit);\s*$/gim,'')+'\n'; sql+=`\nnotify pgrst,'reload schema';\ncommit;\nselect (select count(*) from pg_tables where schemaname='public') as tables,(select count(*) from public.sun_workspaces) as workspaces,(select count(*) from auth.users) as users;\n`; fs.mkdirSync(path.join(root,'supabase/migrations'),{recursive:true}); fs.writeFileSync(path.join(root,'supabase/migrations/20260917150000_fresh_caterium.sql'),sql); console.log(JSON.stringify({bytes:Buffer.byteLength(sql),sources:files.length}));