lipgrate
Version:
Lipgrate is a clean and safe migration toolkit for SQL databases. Designed to be readable, minimal, and powerful.
25 lines (21 loc) • 945 B
JavaScript
const logger = require('../../common/logger');
async function resetDatabase(db) {
logger.running('Dropping all tables and types for PostgreSQL...');
const dropQuery = `
DO $do$ DECLARE
r RECORD;
BEGIN
-- Drop all tables in the public schema
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
-- Drop all custom ENUM types in the public schema
FOR r IN (SELECT t.typname FROM pg_type t JOIN pg_enum e ON t.oid = e.enumtypid JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace WHERE n.nspname = 'public' GROUP BY t.typname) LOOP
EXECUTE 'DROP TYPE IF EXISTS ' || quote_ident(r.typname) || ' CASCADE';
END LOOP;
END $do$;
`;
await db.query(dropQuery);
logger.success('All PostgreSQL tables and types dropped.');
}
module.exports = { resetDatabase };