Initial commit
This commit is contained in:
48
scripts/smoke-cleanup.mjs
Normal file
48
scripts/smoke-cleanup.mjs
Normal file
@ -0,0 +1,48 @@
|
||||
import { env as processEnv } from "node:process";
|
||||
import { Pool } from "pg";
|
||||
|
||||
function required(value, fallback) {
|
||||
return value || fallback;
|
||||
}
|
||||
|
||||
const pool = processEnv.DATABASE_URL
|
||||
? new Pool({ connectionString: processEnv.DATABASE_URL })
|
||||
: new Pool({
|
||||
host: required(processEnv.PGHOST, "127.0.0.1"),
|
||||
port: Number(required(processEnv.PGPORT, "5432")),
|
||||
user: required(processEnv.PGUSER, "postgres"),
|
||||
password: processEnv.PGPASSWORD || "",
|
||||
database: required(processEnv.PGDATABASE, "qris_soundbox_platform")
|
||||
});
|
||||
|
||||
async function main() {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
|
||||
const txResult = await client.query("DELETE FROM transactions WHERE partner_reference LIKE 'PR-%' RETURNING id");
|
||||
const devResult = await client.query("DELETE FROM devices WHERE device_code LIKE 'DEV-%' RETURNING id");
|
||||
const merchantResult = await client.query("DELETE FROM merchants WHERE legal_name LIKE 'Smoke Merchant %' RETURNING id");
|
||||
|
||||
await client.query("COMMIT");
|
||||
|
||||
console.log(JSON.stringify({
|
||||
transactions_deleted: txResult.rowCount,
|
||||
devices_deleted: devResult.rowCount,
|
||||
merchants_deleted: merchantResult.rowCount,
|
||||
note: "outlets/terminals are removed via merchant cascade"
|
||||
}));
|
||||
} catch (error) {
|
||||
await client.query("ROLLBACK");
|
||||
console.error("cleanup failed", error instanceof Error ? error.message : String(error));
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
client.release();
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error("cleanup failed", error instanceof Error ? error.message : String(error));
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user