maintenance: preservar configuración estructural y reforzar FK
This commit is contained in:
@@ -16,10 +16,13 @@ const PRESERVED_TABLES = new Set([
|
|||||||
'asset_types',
|
'asset_types',
|
||||||
'asset_type_parent_rules',
|
'asset_type_parent_rules',
|
||||||
'asset_attribute_definitions',
|
'asset_attribute_definitions',
|
||||||
|
'administrative_departments',
|
||||||
'finding_categories',
|
'finding_categories',
|
||||||
'finding_catalog_items',
|
'finding_catalog_items',
|
||||||
|
'finding_catalog_item_versions',
|
||||||
'finding_catalog_item_asset_types',
|
'finding_catalog_item_asset_types',
|
||||||
'finding_catalog_asset_type_profiles',
|
'finding_catalog_asset_type_profiles',
|
||||||
|
'institutional_delivery_settings',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
type AdminRow = {
|
type AdminRow = {
|
||||||
@@ -147,12 +150,19 @@ async function main(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await migrationDataSource.transaction(async (manager) => {
|
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.
|
||||||
await manager.query(`SET LOCAL session_replication_role = replica`);
|
await manager.query(`SET LOCAL session_replication_role = replica`);
|
||||||
|
|
||||||
for (const table of tablesToClear) {
|
for (const table of tablesToClear) {
|
||||||
await manager.query(`DELETE FROM public.${quoteIdentifier(table)}`);
|
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.
|
||||||
|
await manager.query(`SET LOCAL session_replication_role = origin`);
|
||||||
|
|
||||||
await manager.query(
|
await manager.query(
|
||||||
`DELETE FROM user_roles WHERE user_id <> $1::uuid`,
|
`DELETE FROM user_roles WHERE user_id <> $1::uuid`,
|
||||||
[admin.id],
|
[admin.id],
|
||||||
@@ -224,7 +234,7 @@ async function main(): Promise<void> {
|
|||||||
process.stdout.write('\n========== VERIFICACIÓN FRESH START ==========\n');
|
process.stdout.write('\n========== VERIFICACIÓN FRESH START ==========\n');
|
||||||
process.stdout.write(`Usuarios: 1 (${admin.username})\n`);
|
process.stdout.write(`Usuarios: 1 (${admin.username})\n`);
|
||||||
process.stdout.write('Datos operativos/importados: 0\n');
|
process.stdout.write('Datos operativos/importados: 0\n');
|
||||||
process.stdout.write('Roles/permisos/tipos/catálogos estructurales: conservados\n');
|
process.stdout.write('Roles/permisos/tipos/catálogos/configuración estructural: conservados\n');
|
||||||
process.stdout.write('FRESH_START_OK\n');
|
process.stdout.write('FRESH_START_OK\n');
|
||||||
} finally {
|
} finally {
|
||||||
if (migrationDataSource.isInitialized) {
|
if (migrationDataSource.isInitialized) {
|
||||||
|
|||||||
Reference in New Issue
Block a user