This commit is contained in:
2026-01-03 05:34:24 +01:00
parent f0d44092e5
commit 290dfe303c
49 changed files with 1497 additions and 1016 deletions
@@ -4,6 +4,7 @@ import {
} from "../../types";
import grabDSQLSchemaIndexComment from "../utils/grab-dsql-schema-index-comment";
import dbHandler from "../../functions/backend/dbHandler";
import AppData from "../../data/app-data";
type Param = {
tableName: string;
@@ -22,48 +23,59 @@ export default async function handleIndexescreateDbFromSchema({
tableName,
indexes,
}: Param) {
/**
* Handle MYSQL Table Indexes
* ===================================================
* @description Iterate through each table index(if available)
* and perform operations
*/
const allExistingIndexes = (await dbHandler({
query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
})) as DSQL_MYSQL_SHOW_INDEXES_Type[];
query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\` WHERE Index_comment LIKE '%${AppData["IndexComment"]}%'`,
})) as DSQL_MYSQL_SHOW_INDEXES_Type[] | null;
if (allExistingIndexes) {
for (let f = 0; f < allExistingIndexes.length; f++) {
const { Key_name } = allExistingIndexes[f];
try {
const existingKeyInSchema = 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
*/
await dbHandler({
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?.match(/./)) continue;
/**
* @description Check for existing Index in MYSQL db
*/
try {
const existingKeyInDb = allExistingIndexes.filter(
(indexObject) => indexObject.Key_name === alias
);
const queryString = `CREATE${
indexType == "full_text"
? " FULLTEXT"
: indexType == "vector"
? " VECTOR"
: ""
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
?.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(",")}) COMMENT '${AppData["IndexComment"]} ${indexName}'`;
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
?.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(
","
)}) COMMENT '${grabDSQLSchemaIndexComment()} ${indexName}'`;
const addIndex = await dbHandler({ query: queryString });
}
const addIndex = await dbHandler({ query: queryString });
}
const allExistingIndexesAfterUpdate = (await dbHandler({
query: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
})) as DSQL_MYSQL_SHOW_INDEXES_Type[];
}
@@ -0,0 +1,80 @@
import {
DSQL_MYSQL_FOREIGN_KEYS_Type,
DSQL_MYSQL_SHOW_INDEXES_Type,
DSQL_UniqueConstraintSchemaType,
} from "../../types";
import dbHandler from "../../functions/backend/dbHandler";
import AppData from "../../data/app-data";
import normalizeText from "../../utils/normalize-text";
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 async function handleUniqueConstraintsCreateDbFromSchema({
dbFullName,
tableName,
tableUniqueConstraints,
}: Param) {
/**
* # 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?.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
?.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(",")}) COMMENT '${
AppData["UniqueConstraintComment"]
} ${constraintName}'`;
const addIndex = await dbHandler({ query: queryString });
}
}
@@ -131,7 +131,7 @@ export default async function createDbFromSchema({
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;
@@ -169,6 +169,7 @@ export default async function createDbFromSchema({
recordedDbEntry,
tableSchema: table,
isMain,
tableUniqueConstraints: uniqueConstraints,
});
if (table.childrenTables && table.childrenTables[0]) {
@@ -201,6 +202,8 @@ export default async function createDbFromSchema({
userId,
dbSchema: childTableParentDbSchema,
tableIndexes: childTableSchema.indexes,
tableUniqueConstraints:
childTableSchema.uniqueConstraints,
clone: true,
recordedDbEntry,
tableSchema: table,
@@ -214,11 +217,13 @@ export default async function createDbFromSchema({
*/
const createNewTable = await createTable({
tableName: tableName,
tableInfoArray: fields,
fields,
dbFullName: dbFullName,
tableSchema: table,
recordedDbEntry,
isMain,
indexes,
uniqueConstraints,
});
/**