43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
// FIXME: this is at the wrong level of specificity
|
|
|
|
import { connectionConfig, migrate, pool } from "../database";
|
|
|
|
const exitIfUnforced = () => {
|
|
const args = process.argv.slice(2);
|
|
|
|
// Require explicit confirmation unless --force is passed
|
|
if (!args.includes("--force")) {
|
|
console.error("This will DROP ALL TABLES in the database!");
|
|
console.error(` Database: ${connectionConfig.database}`);
|
|
console.error(
|
|
` Host: ${connectionConfig.host}:${connectionConfig.port}`,
|
|
);
|
|
console.error("");
|
|
console.error("Run with --force to proceed.");
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
const dropTables = async () => {
|
|
console.log("Dropping all tables...");
|
|
|
|
// Get all table names in the public schema
|
|
const result = await pool.query<{ tablename: string }>(`
|
|
SELECT tablename FROM pg_tables
|
|
WHERE schemaname = 'public'
|
|
`);
|
|
|
|
if (result.rows.length > 0) {
|
|
// Drop all tables with CASCADE to handle foreign key constraints
|
|
const tableNames = result.rows
|
|
.map((r) => `"${r.tablename}"`)
|
|
.join(", ");
|
|
await pool.query(`DROP TABLE IF EXISTS ${tableNames} CASCADE`);
|
|
console.log(`Dropped ${result.rows.length} table(s)`);
|
|
} else {
|
|
console.log("No tables to drop");
|
|
}
|
|
};
|
|
|
|
export { dropTables, exitIfUnforced };
|