import type { Sequelize } from "sequelize"; import { DataTypes, QueryTypes } from "sequelize"; import { inventoryColumns, normalizeResourceInventory, specificationColumns, } from "./resource-normalization-migration"; const id = { type: DataTypes.UUID, primaryKey: true }; const fk = (model: string) => ({ type: DataTypes.UUID, allowNull: false, references: { model, key: "id" }, onDelete: "RESTRICT", }); export async function migrateInventory(database: Sequelize) { const query = database.getQueryInterface(); const tables = await database.query<{ name: string }>( "SELECT TABLE_NAME AS name FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE()", { type: QueryTypes.SELECT } ); const resource = { id, companyId: fk("companies"), templateId: fk("resource_definitions"), ...inventoryColumns, ...specificationColumns, }; const revision = { id, resourceId: fk("resources"), actorId: fk("users"), recordedAt: { type: DataTypes.STRING(30), allowNull: false }, ...inventoryColumns, templateId: fk("resource_definitions"), position: { type: DataTypes.INTEGER, allowNull: false }, }; if (!tables.some((table) => table.name === "resources")) { await query.createTable("resources", resource); } if (!tables.some((table) => table.name === "resource_revisions")) { await query.createTable("resource_revisions", revision); } const indexes = await database.query<{ name: string }>( "SELECT DISTINCT INDEX_NAME AS name FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='resource_revisions'", { type: QueryTypes.SELECT } ); if (!indexes.some((index) => index.name === "resource_revision_position_unique")) { await query.addIndex("resource_revisions", ["resourceId", "position"], { unique: true, name: "resource_revision_position_unique", }); } await normalizeResourceInventory(database); }