73 lines
2.8 KiB
TypeScript
73 lines
2.8 KiB
TypeScript
import assert from "node:assert/strict";
|
|
|
|
import type { Sequelize } from "sequelize";
|
|
import { DataTypes, QueryTypes } from "sequelize";
|
|
|
|
import {
|
|
ARCHIVED_SUPPORT_TABLES,
|
|
removeArchivedSupportTables,
|
|
} from "../../../modules/companies/src/api/infrastructure/persistence/remove-archived-support-tables";
|
|
|
|
export async function verifyArchivedSupportRemoval(database: Sequelize) {
|
|
assert.equal(database.getDatabaseName(), "metis_desk_test");
|
|
const query = database.getQueryInterface();
|
|
|
|
const activeCounts = () =>
|
|
database.query<{ companies: number; assets: number; resources: number; users: number }>(
|
|
"SELECT (SELECT COUNT(*) FROM companies) AS companies,(SELECT COUNT(*) FROM assets) AS assets,(SELECT COUNT(*) FROM resources) AS resources,(SELECT COUNT(*) FROM users) AS users",
|
|
{ type: QueryTypes.SELECT }
|
|
);
|
|
|
|
const remaining = () =>
|
|
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 before = await activeCounts();
|
|
|
|
assert.deepEqual(await remaining(), []);
|
|
// Recrea un esquema anterior con filas y dependencias sin cargar el código archivado.
|
|
for (const table of ARCHIVED_SUPPORT_TABLES) {
|
|
await query.createTable(table, { id: { type: DataTypes.INTEGER, primaryKey: true } });
|
|
await query.bulkInsert(table, [{ id: 1 }]);
|
|
}
|
|
for (const [child, parent] of [
|
|
["interventions", "tickets"],
|
|
["intervention_revisions", "interventions"],
|
|
["intervention_procedures", "intervention_template_revisions"],
|
|
["intervention_procedure_steps", "intervention_procedures"],
|
|
["intervention_template_revisions", "intervention_templates"],
|
|
["ticket_revisions", "tickets"],
|
|
] as const) {
|
|
await query.addColumn(child, "parentId", {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: true,
|
|
references: { model: parent, key: "id" },
|
|
onDelete: "RESTRICT",
|
|
});
|
|
await database.query(`UPDATE ${query.quoteIdentifier(child)} SET parentId=:id`, {
|
|
replacements: { id: 1 },
|
|
});
|
|
}
|
|
await query.createTable("archived_support_guard", {
|
|
id: { type: DataTypes.INTEGER, primaryKey: true },
|
|
ticketId: {
|
|
type: DataTypes.INTEGER,
|
|
references: { model: "tickets", key: "id" },
|
|
onDelete: "RESTRICT",
|
|
},
|
|
});
|
|
try {
|
|
await assert.rejects(removeArchivedSupportTables(database), /Una tabla activa referencia/);
|
|
assert.equal((await remaining()).length, ARCHIVED_SUPPORT_TABLES.length);
|
|
} finally {
|
|
await query.dropTable("archived_support_guard");
|
|
}
|
|
await removeArchivedSupportTables(database);
|
|
assert.deepEqual(await remaining(), []);
|
|
await removeArchivedSupportTables(database);
|
|
assert.deepEqual(await remaining(), []);
|
|
assert.deepEqual(await activeCounts(), before);
|
|
}
|