2026-07-06 20:40:19 +03:30
|
|
|
/**
|
|
|
|
|
* Dev-only: truncate treatment and lab case data (preserves catalog tables).
|
|
|
|
|
* Usage: npx ts-node prisma/reset-treatment-data.ts
|
2026-07-07 13:13:04 +03:30
|
|
|
*
|
|
|
|
|
* Safe to run before or after `prisma migrate deploy`: tables that do not yet
|
|
|
|
|
* exist are skipped instead of throwing.
|
2026-07-06 20:40:19 +03:30
|
|
|
*/
|
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
|
import { config } from 'dotenv';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
|
|
|
|
|
const envPath = path.join(__dirname, '..', '.env');
|
|
|
|
|
config({ path: envPath });
|
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
|
console.error('reset-treatment-data is not allowed in production');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
|
|
2026-07-07 13:13:04 +03:30
|
|
|
// FK-safe order: children before parents.
|
|
|
|
|
const TABLES_IN_ORDER = [
|
2026-07-07 15:31:09 +03:30
|
|
|
'lab_case_task_status_events',
|
|
|
|
|
'lab_case_comments',
|
2026-07-07 13:13:04 +03:30
|
|
|
'lab_case_tasks',
|
|
|
|
|
'lab_case_sends',
|
|
|
|
|
'lab_case_tooth_prosthesis',
|
|
|
|
|
'lab_case_details',
|
|
|
|
|
'lab_cases',
|
|
|
|
|
'treatment_detail_attachments',
|
|
|
|
|
'treatment_details',
|
|
|
|
|
'treatments',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
async function tableExists(table: string): Promise<boolean> {
|
|
|
|
|
const rows = await prisma.$queryRawUnsafe<Array<{ exists: string | null }>>(
|
|
|
|
|
`SELECT to_regclass('public."${table}"')::text AS exists`,
|
|
|
|
|
);
|
|
|
|
|
return rows[0]?.exists != null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 20:40:19 +03:30
|
|
|
async function main() {
|
|
|
|
|
console.log('Truncating treatment and lab case data...');
|
|
|
|
|
|
2026-07-07 13:13:04 +03:30
|
|
|
const existing: string[] = [];
|
|
|
|
|
for (const table of TABLES_IN_ORDER) {
|
|
|
|
|
if (await tableExists(table)) {
|
|
|
|
|
existing.push(table);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(` - skipping "${table}" (does not exist yet)`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (existing.length === 0) {
|
|
|
|
|
console.log('No target tables exist yet. Run `prisma migrate deploy` first.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const targets = existing.map((t) => `"${t}"`).join(', ');
|
|
|
|
|
await prisma.$executeRawUnsafe(`TRUNCATE TABLE ${targets} CASCADE`);
|
2026-07-06 20:40:19 +03:30
|
|
|
|
|
|
|
|
console.log('Done.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main()
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.error(e);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
})
|
|
|
|
|
.finally(async () => {
|
|
|
|
|
await prisma.$disconnect();
|
|
|
|
|
});
|