95 lines
5.1 KiB
JavaScript
95 lines
5.1 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 = handleMariaDBExistingColumns;
|
|
const dbHandler_1 = __importDefault(require("../../functions/backend/dbHandler"));
|
|
const default_fields_regexp_1 = __importDefault(require("../../functions/dsql/default-fields-regexp"));
|
|
const grab_required_database_schemas_1 = require("../createDbFromSchema/grab-required-database-schemas");
|
|
const lodash_1 = __importDefault(require("lodash"));
|
|
/**
|
|
* Handle MYSQL Columns (Fields)
|
|
* ===================================================
|
|
* @description Now handle all fields/columns
|
|
*/
|
|
function handleMariaDBExistingColumns(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ dbFullName, tableName, fields, dbSchema, userId, }) {
|
|
let upToDateTableFieldsArray = lodash_1.default.cloneDeep(fields);
|
|
let allExistingColumns = (yield (0, dbHandler_1.default)({
|
|
query: `SHOW COLUMNS FROM \`${dbFullName}\`.\`${tableName}\``,
|
|
}));
|
|
/**
|
|
* @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) {
|
|
yield (0, dbHandler_1.default)({
|
|
query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP COLUMN \`${Field}\``,
|
|
});
|
|
continue;
|
|
}
|
|
if (existingEntry) {
|
|
/**
|
|
* @description Check if Field name has been updated
|
|
*/
|
|
if (existingEntry.updatedField && existingEntry.fieldName) {
|
|
yield (0, dbHandler_1.default)({
|
|
query: `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, dbHandler_1.default)({
|
|
query: `SHOW COLUMNS FROM \`${dbFullName}\`.\`${tableName}\``,
|
|
}));
|
|
}
|
|
catch (error) {
|
|
console.log("Update table error =>", error.message);
|
|
}
|
|
////////////////////////////////////////
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
return { upToDateTableFieldsArray, allExistingColumns };
|
|
});
|
|
}
|