137 lines
6.2 KiB
JavaScript
137 lines
6.2 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 = updateTableInit;
|
|
const create_table_handle_table_record_1 = __importDefault(require("./create-table-handle-table-record"));
|
|
const dbHandler_1 = __importDefault(require("../../functions/backend/dbHandler"));
|
|
/**
|
|
* # Update table function
|
|
*/
|
|
function updateTableInit(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ dbFullName, tableName, tableSchema, recordedDbEntry, isMain, }) {
|
|
/**
|
|
* @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 Default Collation
|
|
*
|
|
* @description Update Column Collation
|
|
*/
|
|
if (tableSchema.collation) {
|
|
try {
|
|
const existingCollation = (yield (0, dbHandler_1.default)({
|
|
query: `SHOW TABLE STATUS LIKE '${tableName}'`,
|
|
config: { database: dbFullName },
|
|
}));
|
|
const existingCollationStr = existingCollation === null || existingCollation === void 0 ? void 0 : existingCollation[0].Collation;
|
|
if (existingCollationStr !== tableSchema.collation) {
|
|
yield (0, dbHandler_1.default)({
|
|
query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE ${tableSchema.collation}`,
|
|
});
|
|
}
|
|
}
|
|
catch (error) { }
|
|
}
|
|
/**
|
|
* Drop All Foreign Keys
|
|
* ===================================================
|
|
* @description Find all existing foreign keys and drop
|
|
* them
|
|
*/
|
|
const allForeignKeys = (yield (0, dbHandler_1.default)({
|
|
query: `SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = '${dbFullName}' AND TABLE_NAME='${tableName}' AND CONSTRAINT_TYPE='FOREIGN KEY'`,
|
|
}));
|
|
if (allForeignKeys === null || allForeignKeys === void 0 ? void 0 : allForeignKeys[0]) {
|
|
let dropFkSQL = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\``;
|
|
for (let c = 0; c < allForeignKeys.length; c++) {
|
|
const { CONSTRAINT_NAME } = allForeignKeys[c];
|
|
if (CONSTRAINT_NAME.match(/PRIMARY/))
|
|
continue;
|
|
dropFkSQL += ` DROP FOREIGN KEY \`${CONSTRAINT_NAME}\`,`;
|
|
}
|
|
const finalSQL = dropFkSQL.endsWith(",")
|
|
? dropFkSQL.replace(/\,$/, "")
|
|
: undefined;
|
|
if (finalSQL) {
|
|
yield (0, dbHandler_1.default)({
|
|
query: finalSQL,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* Drop All Unique Constraints
|
|
* ===================================================
|
|
* @description Find all existing unique field constraints
|
|
* and remove them
|
|
*/
|
|
const allUniqueConstraints = (yield (0, dbHandler_1.default)({
|
|
query: `SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = '${dbFullName}' AND TABLE_NAME='${tableName}' AND CONSTRAINT_TYPE='UNIQUE'`,
|
|
}));
|
|
if (allUniqueConstraints === null || allUniqueConstraints === void 0 ? void 0 : allUniqueConstraints[0]) {
|
|
let dropIndxSQL = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\``;
|
|
for (let c = 0; c < allUniqueConstraints.length; c++) {
|
|
const { CONSTRAINT_NAME } = allUniqueConstraints[c];
|
|
dropIndxSQL += ` DROP INDEX ${CONSTRAINT_NAME},`;
|
|
}
|
|
const finalDropIndxSQL = dropIndxSQL.endsWith(",")
|
|
? dropIndxSQL.replace(/\,$/, "")
|
|
: undefined;
|
|
if (finalDropIndxSQL) {
|
|
yield (0, dbHandler_1.default)({
|
|
query: finalDropIndxSQL,
|
|
});
|
|
}
|
|
}
|
|
/**
|
|
* Drop All Indexes
|
|
* ===================================================
|
|
* @description Find all existing foreign keys and drop
|
|
* them
|
|
*/
|
|
const allMariadbIndexes = (yield (0, dbHandler_1.default)({
|
|
query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
|
}));
|
|
if (allMariadbIndexes === null || allMariadbIndexes === void 0 ? void 0 : allMariadbIndexes[0]) {
|
|
let dropIndxs = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\``;
|
|
for (let c = 0; c < allMariadbIndexes.length; c++) {
|
|
const { Key_name } = allMariadbIndexes[c];
|
|
if (Key_name.match(/PRIMARY/))
|
|
continue;
|
|
dropIndxs += ` DROP INDEX \`${Key_name}\`,`;
|
|
}
|
|
const finalDropIndxs = dropIndxs.endsWith(",")
|
|
? dropIndxs.replace(/\,$/, "")
|
|
: undefined;
|
|
if (finalDropIndxs) {
|
|
const dropFkRes = yield (0, dbHandler_1.default)({
|
|
query: finalDropIndxs,
|
|
});
|
|
}
|
|
}
|
|
return { tableID };
|
|
});
|
|
}
|