52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
|
|
import type { Sequelize } from "sequelize";
|
||
|
|
import { QueryTypes } from "sequelize";
|
||
|
|
|
||
|
|
// Primero se retiran los hijos y las referencias; las tablas raíz quedan al final.
|
||
|
|
export const ARCHIVED_SUPPORT_TABLES = [
|
||
|
|
"intervention_procedure_steps",
|
||
|
|
"intervention_procedures",
|
||
|
|
"intervention_revision_targets",
|
||
|
|
"intervention_revision_steps",
|
||
|
|
"intervention_targets",
|
||
|
|
"intervention_steps",
|
||
|
|
"intervention_revisions",
|
||
|
|
"interventions",
|
||
|
|
"intervention_counter",
|
||
|
|
"ticket_revision_targets",
|
||
|
|
"ticket_targets",
|
||
|
|
"ticket_revisions",
|
||
|
|
"tickets",
|
||
|
|
"ticket_counter",
|
||
|
|
"intervention_template_revision_steps",
|
||
|
|
"intervention_template_steps",
|
||
|
|
"intervention_template_revisions",
|
||
|
|
"intervention_templates",
|
||
|
|
"intervention_template_lock",
|
||
|
|
] as const;
|
||
|
|
|
||
|
|
/** Retirada idempotente: MariaDB confirma el DDL y permite continuar tras una interrupción. */
|
||
|
|
export async function removeArchivedSupportTables(database: Sequelize) {
|
||
|
|
const references = await database.query<{ tableName: string }>(
|
||
|
|
"SELECT TABLE_NAME AS tableName FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA=DATABASE() AND REFERENCED_TABLE_NAME IN (:tables)",
|
||
|
|
{ replacements: { tables: [...ARCHIVED_SUPPORT_TABLES] }, type: QueryTypes.SELECT }
|
||
|
|
);
|
||
|
|
const archivedTables = new Set<string>(ARCHIVED_SUPPORT_TABLES);
|
||
|
|
|
||
|
|
if (references.some((reference) => !archivedTables.has(reference.tableName))) {
|
||
|
|
throw new Error(
|
||
|
|
"Una tabla activa referencia tablas de soporte archivadas. Revisar la migración."
|
||
|
|
);
|
||
|
|
}
|
||
|
|
const existing = await database.query<{ name: string }>(
|
||
|
|
"SELECT TABLE_NAME AS name FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME IN (:tables)",
|
||
|
|
{ replacements: { tables: [...ARCHIVED_SUPPORT_TABLES] }, type: QueryTypes.SELECT }
|
||
|
|
);
|
||
|
|
const query = database.getQueryInterface();
|
||
|
|
|
||
|
|
for (const table of ARCHIVED_SUPPORT_TABLES) {
|
||
|
|
if (existing.some((item) => item.name === table)) {
|
||
|
|
await query.dropTable(table);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|