"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = handleUniqueConstraintsCreateDbFromSchema; const dbHandler_1 = __importDefault(require("../../functions/backend/dbHandler")); const app_data_1 = __importDefault(require("../../data/app-data")); /** * Handle DATASQUIREL Table Unique Constraints * =================================================== * @description Iterate through each datasquirel schema * table unique constraint(if available), and perform operations */ function handleUniqueConstraintsCreateDbFromSchema(_a) { return __awaiter(this, arguments, void 0, function* ({ dbFullName, tableName, tableUniqueConstraints, }) { /** * # Delete All Existing Unique Constraints */ // const allExistingUniqueConstraints = (await dbHandler({ // query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\` WHERE Index_comment LIKE '%${AppData["UniqueConstraintComment"]}%'`, // })) as DSQL_MYSQL_SHOW_INDEXES_Type[] | null; // if (allExistingUniqueConstraints?.[0]) { // for (let f = 0; f < allExistingUniqueConstraints.length; f++) { // const { Key_name } = allExistingUniqueConstraints[f]; // try { // const existingKeyInSchema = tableUniqueConstraints?.find( // (indexObject) => indexObject.alias === Key_name // ); // if (!existingKeyInSchema) // throw new Error( // `This Index(${Key_name}) Has been Deleted!` // ); // } catch (error) { // /** // * @description Drop Index: This happens when the MYSQL index is not // * present in the datasquirel DB schema // */ // await dbHandler({ // query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP INDEX \`${Key_name}\``, // }); // } // } // } /** * # Re-Add New Constraints */ for (let g = 0; g < tableUniqueConstraints.length; g++) { const { constraintName, alias, constraintTableFields } = tableUniqueConstraints[g]; if (!(alias === null || alias === void 0 ? void 0 : alias.match(/./))) continue; /** * @description Create new index if determined that it * doesn't exist in MYSQL db */ const queryString = `CREATE UNIQUE INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${constraintTableFields === null || constraintTableFields === void 0 ? void 0 : constraintTableFields.map((nm) => nm.value).map((nm) => `\`${nm}\``).join(",")}) COMMENT '${app_data_1.default["UniqueConstraintComment"]} ${constraintName}'`; const addIndex = yield (0, dbHandler_1.default)({ query: queryString }); } }); }