Updates
This commit is contained in:
+36
-25
@@ -13,8 +13,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = handleIndexescreateDbFromSchema;
|
||||
const grab_dsql_schema_index_comment_1 = __importDefault(require("../utils/grab-dsql-schema-index-comment"));
|
||||
const dbHandler_1 = __importDefault(require("../../functions/backend/dbHandler"));
|
||||
const app_data_1 = __importDefault(require("../../data/app-data"));
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
@@ -23,36 +23,47 @@ const dbHandler_1 = __importDefault(require("../../functions/backend/dbHandler")
|
||||
*/
|
||||
function handleIndexescreateDbFromSchema(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ dbFullName, tableName, indexes, }) {
|
||||
/**
|
||||
* Handle MYSQL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each table index(if available)
|
||||
* and perform operations
|
||||
*/
|
||||
const allExistingIndexes = (yield (0, dbHandler_1.default)({
|
||||
query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\` WHERE Index_comment LIKE '%${app_data_1.default["IndexComment"]}%'`,
|
||||
}));
|
||||
if (allExistingIndexes) {
|
||||
for (let f = 0; f < allExistingIndexes.length; f++) {
|
||||
const { Key_name } = allExistingIndexes[f];
|
||||
try {
|
||||
const existingKeyInSchema = indexes === null || indexes === void 0 ? void 0 : indexes.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
|
||||
*/
|
||||
yield (0, dbHandler_1.default)({
|
||||
query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP INDEX \`${Key_name}\``,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* # Re-Add New Indexes
|
||||
*/
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const { indexType, indexName, indexTableFields, alias } = indexes[g];
|
||||
if (!(alias === null || alias === void 0 ? void 0 : alias.match(/./)))
|
||||
continue;
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
const existingKeyInDb = allExistingIndexes.filter((indexObject) => indexObject.Key_name === alias);
|
||||
if (!existingKeyInDb[0])
|
||||
throw new Error("This Index Does not Exist");
|
||||
}
|
||||
catch (error) {
|
||||
/**
|
||||
* @description Create new index if determined that it
|
||||
* doesn't exist in MYSQL db
|
||||
*/
|
||||
const queryString = `CREATE${indexType == "full_text"
|
||||
? " FULLTEXT"
|
||||
: indexType == "vector"
|
||||
? " VECTOR"
|
||||
: ""} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields === null || indexTableFields === void 0 ? void 0 : indexTableFields.map((nm) => nm.value).map((nm) => `\`${nm}\``).join(",")}) COMMENT '${(0, grab_dsql_schema_index_comment_1.default)()} ${indexName}'`;
|
||||
const addIndex = yield (0, dbHandler_1.default)({ query: queryString });
|
||||
}
|
||||
const queryString = `CREATE${indexType == "full_text"
|
||||
? " FULLTEXT"
|
||||
: indexType == "vector"
|
||||
? " VECTOR"
|
||||
: ""} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields === null || indexTableFields === void 0 ? void 0 : indexTableFields.map((nm) => nm.value).map((nm) => `\`${nm}\``).join(",")}) COMMENT '${app_data_1.default["IndexComment"]} ${indexName}'`;
|
||||
const addIndex = yield (0, dbHandler_1.default)({ query: queryString });
|
||||
}
|
||||
const allExistingIndexesAfterUpdate = (yield (0, dbHandler_1.default)({
|
||||
query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { DSQL_UniqueConstraintSchemaType } from "../../types";
|
||||
type Param = {
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
tableUniqueConstraints: DSQL_UniqueConstraintSchemaType[];
|
||||
};
|
||||
/**
|
||||
* Handle DATASQUIREL Table Unique Constraints
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table unique constraint(if available), and perform operations
|
||||
*/
|
||||
export default function handleUniqueConstraintsCreateDbFromSchema({ dbFullName, tableName, tableUniqueConstraints, }: Param): Promise<void>;
|
||||
export {};
|
||||
@@ -0,0 +1,69 @@
|
||||
"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 });
|
||||
}
|
||||
});
|
||||
}
|
||||
+6
-2
@@ -104,7 +104,7 @@ function createDbFromSchema(_a) {
|
||||
*/
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
const { tableName, fields, indexes } = table;
|
||||
const { tableName, fields, indexes, uniqueConstraints } = table;
|
||||
if (targetTable && tableName !== targetTable)
|
||||
continue;
|
||||
console.log(`Handling table => ${tableName}`);
|
||||
@@ -139,6 +139,7 @@ function createDbFromSchema(_a) {
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
isMain,
|
||||
tableUniqueConstraints: uniqueConstraints,
|
||||
});
|
||||
if (table.childrenTables && table.childrenTables[0]) {
|
||||
for (let ch = 0; ch < table.childrenTables.length; ch++) {
|
||||
@@ -159,6 +160,7 @@ function createDbFromSchema(_a) {
|
||||
userId,
|
||||
dbSchema: childTableParentDbSchema,
|
||||
tableIndexes: childTableSchema.indexes,
|
||||
tableUniqueConstraints: childTableSchema.uniqueConstraints,
|
||||
clone: true,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
@@ -173,11 +175,13 @@ function createDbFromSchema(_a) {
|
||||
*/
|
||||
const createNewTable = yield (0, createTable_1.default)({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
fields,
|
||||
dbFullName: dbFullName,
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
isMain,
|
||||
indexes,
|
||||
uniqueConstraints,
|
||||
});
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
|
||||
Reference in New Issue
Block a user