maintenance: limpiar departamentos importados y resetear configuración editable
This commit is contained in:
@@ -16,7 +16,6 @@ const PRESERVED_TABLES = new Set([
|
||||
'asset_types',
|
||||
'asset_type_parent_rules',
|
||||
'asset_attribute_definitions',
|
||||
'administrative_departments',
|
||||
'finding_categories',
|
||||
'finding_catalog_items',
|
||||
'finding_catalog_item_versions',
|
||||
@@ -129,6 +128,7 @@ async function main(): Promise<void> {
|
||||
for (const table of tablesToPreserve) {
|
||||
process.stdout.write(`KEEP ${table.padEnd(46)} ${await rowCount(table)}\n`);
|
||||
}
|
||||
process.stdout.write('RESET institutional_delivery_settings emails/updated_by\n');
|
||||
|
||||
process.stdout.write('\n========== DATOS A ELIMINAR ==========\n');
|
||||
let rowsToDelete = 0;
|
||||
@@ -150,19 +150,28 @@ async function main(): Promise<void> {
|
||||
}
|
||||
|
||||
await migrationDataSource.transaction(async (manager) => {
|
||||
// Los datos operativos se eliminan con triggers de FK desactivados porque
|
||||
// la limpieza vacía el grafo completo y el orden físico de tablas no importa.
|
||||
// Se vacía el grafo operacional completo sin depender del orden físico
|
||||
// de las FK. La estructura maestra está excluida por PRESERVED_TABLES.
|
||||
await manager.query(`SET LOCAL session_replication_role = replica`);
|
||||
|
||||
for (const table of tablesToClear) {
|
||||
await manager.query(`DELETE FROM public.${quoteIdentifier(table)}`);
|
||||
}
|
||||
|
||||
// Antes de tocar usuarios reactivamos las FK. De esta manera cualquier
|
||||
// referencia desde configuración preservada ejecuta su ON DELETE real
|
||||
// (SET NULL/CASCADE) o aborta toda la transacción si fuera RESTRICT.
|
||||
// Antes de tocar cualquier tabla preservada reactivamos las FK reales.
|
||||
// Si apareciera una dependencia RESTRICT inesperada, PostgreSQL aborta
|
||||
// la transacción completa en lugar de dejar referencias huérfanas.
|
||||
await manager.query(`SET LOCAL session_replication_role = origin`);
|
||||
|
||||
await manager.query(`
|
||||
UPDATE institutional_delivery_settings
|
||||
SET office_email = NULL,
|
||||
director_email = NULL,
|
||||
updated_by = NULL,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = 1
|
||||
`);
|
||||
|
||||
await manager.query(
|
||||
`DELETE FROM user_roles WHERE user_id <> $1::uuid`,
|
||||
[admin.id],
|
||||
@@ -234,7 +243,8 @@ async function main(): Promise<void> {
|
||||
process.stdout.write('\n========== VERIFICACIÓN FRESH START ==========\n');
|
||||
process.stdout.write(`Usuarios: 1 (${admin.username})\n`);
|
||||
process.stdout.write('Datos operativos/importados: 0\n');
|
||||
process.stdout.write('Roles/permisos/tipos/catálogos/configuración estructural: conservados\n');
|
||||
process.stdout.write('Roles/permisos/tipos/catálogo base: conservados\n');
|
||||
process.stdout.write('Configuración institucional editable: reiniciada\n');
|
||||
process.stdout.write('FRESH_START_OK\n');
|
||||
} finally {
|
||||
if (migrationDataSource.isInitialized) {
|
||||
|
||||
Reference in New Issue
Block a user