335 lines
16 KiB
JavaScript
335 lines
16 KiB
JavaScript
"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 = updateTable;
|
|
const varDatabaseDbHandler_1 = __importDefault(require("./varDatabaseDbHandler"));
|
|
const generateColumnDescription_1 = __importDefault(require("./generateColumnDescription"));
|
|
const handle_table_foreign_key_1 = __importDefault(require("./handle-table-foreign-key"));
|
|
const drop_all_foreign_keys_1 = __importDefault(require("./drop-all-foreign-keys"));
|
|
const create_table_handle_table_record_1 = __importDefault(require("./create-table-handle-table-record"));
|
|
const default_fields_regexp_1 = __importDefault(require("../../functions/dsql/default-fields-regexp"));
|
|
const handle_indexes_1 = __importDefault(require("../createDbFromSchema/handle-indexes"));
|
|
const lodash_1 = __importDefault(require("lodash"));
|
|
const grab_required_database_schemas_1 = require("../createDbFromSchema/grab-required-database-schemas");
|
|
const normalize_text_1 = __importDefault(require("../../utils/normalize-text"));
|
|
/**
|
|
* # Update table function
|
|
*/
|
|
function updateTable(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ dbFullName, tableName, tableFields, userId, dbSchema, tableIndexes, tableSchema, clone, recordedDbEntry, isMain, }) {
|
|
/**
|
|
* Initialize
|
|
* ==========================================
|
|
* @description Initial setup
|
|
*/
|
|
let errorLogs = [];
|
|
/**
|
|
* @description Initialize table info array. This value will be
|
|
* changing depending on if a field is renamed or not.
|
|
*/
|
|
let upToDateTableFieldsArray = lodash_1.default.cloneDeep(tableFields);
|
|
/**
|
|
* @type {string[]}
|
|
* @description Table update query string array
|
|
*/
|
|
const updateTableQueryArray = [];
|
|
/**
|
|
* @description Push the query initial value
|
|
*/
|
|
updateTableQueryArray.push(`ALTER TABLE \`${dbFullName}\`.\`${tableName}\``);
|
|
/**
|
|
* @description Grab Table Record
|
|
*/
|
|
if (!recordedDbEntry && !isMain) {
|
|
throw new Error("Recorded Db entry not found!");
|
|
}
|
|
let tableID = yield (0, create_table_handle_table_record_1.default)({
|
|
recordedDbEntry,
|
|
tableSchema,
|
|
update: true,
|
|
isMain,
|
|
});
|
|
if (!tableID && !isMain) {
|
|
throw new Error("Recorded Table entry not found!");
|
|
}
|
|
/**
|
|
* Handle Table updates
|
|
*
|
|
* @description Try to undate table, catch error if anything goes wrong
|
|
*/
|
|
try {
|
|
/**
|
|
* Handle MYSQL Table Indexes
|
|
* ===================================================
|
|
* @description Iterate through each table index(if available)
|
|
* and perform operations
|
|
*/
|
|
const allExistingIndexes = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\` WHERE Index_comment LIKE '%schema_index%'`,
|
|
});
|
|
for (let f = 0; f < allExistingIndexes.length; f++) {
|
|
const { Key_name } = allExistingIndexes[f];
|
|
try {
|
|
const existingKeyInSchema = tableIndexes === null || tableIndexes === void 0 ? void 0 : tableIndexes.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, varDatabaseDbHandler_1.default)({
|
|
queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP INDEX \`${Key_name}\``,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* Handle DATASQUIREL Table Indexes
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table index(if available), and perform operations
|
|
*/
|
|
if (tableIndexes && tableIndexes[0]) {
|
|
(0, handle_indexes_1.default)({
|
|
dbFullName,
|
|
indexes: tableIndexes,
|
|
tableName,
|
|
});
|
|
}
|
|
/**
|
|
* Handle MYSQL Foreign Keys
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table index(if available), and perform operations
|
|
*/
|
|
const allForeignKeys = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = '${dbFullName}' AND TABLE_NAME='${tableName}' AND CONSTRAINT_TYPE='FOREIGN KEY'`,
|
|
});
|
|
if (allForeignKeys) {
|
|
for (let c = 0; c < allForeignKeys.length; c++) {
|
|
const { CONSTRAINT_NAME } = allForeignKeys[c];
|
|
/**
|
|
* @description Skip if Key is the PRIMARY Key
|
|
*/
|
|
if (CONSTRAINT_NAME.match(/PRIMARY/))
|
|
continue;
|
|
/**
|
|
* @description Drop all foreign Keys to avoid MYSQL errors when adding/updating
|
|
* Foreign keys
|
|
*/
|
|
const dropForeignKey = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP FOREIGN KEY \`${CONSTRAINT_NAME}\``,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* Handle MYSQL Unique Fields
|
|
* ===================================================
|
|
* @description Find all existing unique field constraints
|
|
* and remove them
|
|
*/
|
|
const allUniqueConstraints = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: (0, normalize_text_1.default)(`SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS \
|
|
WHERE CONSTRAINT_SCHEMA = '${dbFullName}' AND TABLE_NAME='${tableName}' AND \
|
|
CONSTRAINT_TYPE='UNIQUE'`),
|
|
});
|
|
if (allUniqueConstraints) {
|
|
for (let c = 0; c < allUniqueConstraints.length; c++) {
|
|
const { CONSTRAINT_NAME } = allUniqueConstraints[c];
|
|
const dropUniqueConstraint = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP INDEX \`${CONSTRAINT_NAME}\``,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* Handle MYSQL Columns (Fields)
|
|
* ===================================================
|
|
* @description Now handle all fields/columns
|
|
*/
|
|
let allExistingColumns = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `SHOW COLUMNS FROM \`${dbFullName}\`.\`${tableName}\``,
|
|
});
|
|
/**
|
|
* @type {string[]}
|
|
* @description Updated column names Array
|
|
*/
|
|
const updatedColumnsArray = [];
|
|
/**
|
|
* @description Iterate through every existing column
|
|
*/
|
|
for (let e = 0; e < allExistingColumns.length; e++) {
|
|
const { Field } = allExistingColumns[e];
|
|
if (Field.match(default_fields_regexp_1.default))
|
|
continue;
|
|
/**
|
|
* @description This finds out whether the fieldName corresponds with the MSQL Field name
|
|
* if the fildName doesn't match any MYSQL Field name, the field is deleted.
|
|
*/
|
|
let existingEntry = upToDateTableFieldsArray.find((column) => column.fieldName === Field || column.originName === Field);
|
|
if (existingEntry) {
|
|
/**
|
|
* @description Check if Field name has been updated
|
|
*/
|
|
if (existingEntry.updatedField && existingEntry.fieldName) {
|
|
updatedColumnsArray.push(existingEntry.fieldName);
|
|
const renameColumn = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` RENAME COLUMN \`${existingEntry.originName}\` TO \`${existingEntry.fieldName}\``,
|
|
});
|
|
console.log(`Column Renamed from "${existingEntry.originName}" to "${existingEntry.fieldName}"`);
|
|
/**
|
|
* Update Db Schema
|
|
* ===================================================
|
|
* @description Update Db Schema after renaming column
|
|
*/
|
|
try {
|
|
const updatedSchemaData = lodash_1.default.cloneDeep(dbSchema);
|
|
const targetTableIndex = updatedSchemaData.tables.findIndex((table) => table.tableName === tableName);
|
|
const targetFieldIndex = updatedSchemaData.tables[targetTableIndex].fields.findIndex((field) => field.fieldName === existingEntry.fieldName);
|
|
delete updatedSchemaData.tables[targetTableIndex]
|
|
.fields[targetFieldIndex]["originName"];
|
|
delete updatedSchemaData.tables[targetTableIndex]
|
|
.fields[targetFieldIndex]["updatedField"];
|
|
/**
|
|
* @description Set New Table Fields Array
|
|
*/
|
|
upToDateTableFieldsArray =
|
|
updatedSchemaData.tables[targetTableIndex].fields;
|
|
if (userId) {
|
|
(0, grab_required_database_schemas_1.writeUpdatedDbSchema)({
|
|
dbSchema: updatedSchemaData,
|
|
userId,
|
|
});
|
|
}
|
|
allExistingColumns = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `SHOW COLUMNS FROM \`${dbFullName}\`.\`${tableName}\``,
|
|
});
|
|
}
|
|
catch (error) {
|
|
console.log("Update table error =>", error.message);
|
|
}
|
|
////////////////////////////////////////
|
|
}
|
|
////////////////////////////////////////
|
|
continue;
|
|
////////////////////////////////////////
|
|
}
|
|
else {
|
|
yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP COLUMN \`${Field}\``,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* Handle DATASQUIREL schema fields for current table
|
|
* ===================================================
|
|
* @description Iterate through each field object and
|
|
* perform operations
|
|
*/
|
|
for (let i = 0; i < upToDateTableFieldsArray.length; i++) {
|
|
const column = upToDateTableFieldsArray[i];
|
|
// const prevColumn = upToDateTableFieldsArray[i - 1];
|
|
// const nextColumn = upToDateTableFieldsArray[i + 1];
|
|
const { fieldName, dataType, foreignKey } = column;
|
|
if (!fieldName)
|
|
continue;
|
|
if (default_fields_regexp_1.default.test(fieldName))
|
|
continue;
|
|
let updateText = "";
|
|
const existingColumnIndex = allExistingColumns === null || allExistingColumns === void 0 ? void 0 : allExistingColumns.findIndex((_column, _index) => _column.Field === fieldName);
|
|
const existingColumn = existingColumnIndex >= 0
|
|
? allExistingColumns[existingColumnIndex]
|
|
: undefined;
|
|
let { fieldEntryText } = (0, generateColumnDescription_1.default)({
|
|
columnData: column,
|
|
});
|
|
/**
|
|
* @description Modify Column(Field) if it already exists
|
|
* in MYSQL database
|
|
*/
|
|
if (existingColumn === null || existingColumn === void 0 ? void 0 : existingColumn.Field) {
|
|
const { Field, Type } = existingColumn;
|
|
updateText += `MODIFY COLUMN ${fieldEntryText}`;
|
|
// if (
|
|
// Field === fieldName &&
|
|
// dataType?.toUpperCase() === Type.toUpperCase()
|
|
// ) {
|
|
// } else {
|
|
// updateText += `MODIFY COLUMN ${fieldEntryText}`;
|
|
// }
|
|
}
|
|
else {
|
|
/**
|
|
* @description Append new column to the end of existing columns
|
|
*/
|
|
updateText += `ADD COLUMN ${fieldEntryText}`;
|
|
}
|
|
/**
|
|
* @description Pust SQL code snippet to updateTableQueryArray Array
|
|
* Add a comma(,) to separate from the next snippet
|
|
*/
|
|
if (updateText.match(/./)) {
|
|
updateTableQueryArray.push(updateText + ",");
|
|
}
|
|
}
|
|
/**
|
|
* @description Construct final SQL query by combning all SQL snippets in
|
|
* updateTableQueryArray Arry, and trimming the final comma(,)
|
|
*/
|
|
const updateTableQuery = updateTableQueryArray
|
|
.filter((q) => Boolean(q.match(/./)))
|
|
.join(" ")
|
|
.replace(/,$/, "");
|
|
////////////////////////////////////////
|
|
/**
|
|
* @description Check if SQL snippets array has more than 1 entries
|
|
* This is because 1 entry means "ALTER TABLE table_name" only, without any
|
|
* Alter directives like "ADD COLUMN" or "MODIFY COLUMN"
|
|
*/
|
|
if (updateTableQueryArray.length > 1) {
|
|
const updateTable = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: updateTableQuery,
|
|
});
|
|
/**
|
|
* # Handle Foreign Keys
|
|
*/
|
|
yield (0, drop_all_foreign_keys_1.default)({ dbFullName, tableName });
|
|
for (let i = 0; i < upToDateTableFieldsArray.length; i++) {
|
|
const { fieldName, foreignKey } = upToDateTableFieldsArray[i];
|
|
if (!clone && foreignKey && fieldName) {
|
|
yield (0, handle_table_foreign_key_1.default)({
|
|
dbFullName,
|
|
errorLogs,
|
|
foreignKey,
|
|
fieldName,
|
|
tableName,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
/**
|
|
* @description If only 1 SQL snippet is left in updateTableQueryArray, this
|
|
* means that no updates have been made to the table
|
|
*/
|
|
}
|
|
return tableID;
|
|
}
|
|
catch (error) {
|
|
console.log('Error in "updateTable" shell function =>', error.message);
|
|
return tableID;
|
|
}
|
|
});
|
|
}
|