133 lines
4.0 KiB
TypeScript
133 lines
4.0 KiB
TypeScript
import {
|
|
DSQL_DatabaseSchemaType,
|
|
DSQL_FieldSchemaType,
|
|
DSQL_IndexSchemaType,
|
|
DSQL_TableSchemaType,
|
|
DSQL_UniqueConstraintSchemaType,
|
|
} from "../../types";
|
|
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
|
import handleIndexescreateDbFromSchema from "../createDbFromSchema/handle-indexes";
|
|
import _ from "lodash";
|
|
import handleUniqueConstraintsCreateDbFromSchema from "../createDbFromSchema/handle-unique-constraints";
|
|
import handleTableForeignKey from "./handle-table-foreign-key";
|
|
import handleDSQLSchemaFields from "./handle-dsql-schema-fields";
|
|
import handleMariaDBExistingColumns from "./handle-mariadb-existing-columns";
|
|
import updateTableInit from "./update-table-init";
|
|
|
|
type Param = {
|
|
dbFullName: string;
|
|
tableName: string;
|
|
tableSchema: DSQL_TableSchemaType;
|
|
tableFields: DSQL_FieldSchemaType[];
|
|
userId?: number | string | null;
|
|
dbSchema: DSQL_DatabaseSchemaType;
|
|
tableIndexes?: DSQL_IndexSchemaType[];
|
|
tableUniqueConstraints?: DSQL_UniqueConstraintSchemaType[];
|
|
clone?: boolean;
|
|
recordedDbEntry?: DSQL_DATASQUIREL_USER_DATABASES;
|
|
isMain?: boolean;
|
|
};
|
|
|
|
/**
|
|
* # Update table function
|
|
*/
|
|
export default async function updateTable({
|
|
dbFullName,
|
|
tableName,
|
|
tableFields,
|
|
userId,
|
|
dbSchema,
|
|
tableIndexes,
|
|
tableSchema,
|
|
clone,
|
|
recordedDbEntry,
|
|
isMain,
|
|
tableUniqueConstraints,
|
|
}: Param): Promise<number | undefined> {
|
|
/**
|
|
* @description Grab Table Record
|
|
*/
|
|
const { tableID } = await updateTableInit({
|
|
dbFullName,
|
|
dbSchema,
|
|
tableName,
|
|
tableSchema,
|
|
isMain,
|
|
recordedDbEntry,
|
|
});
|
|
|
|
/**
|
|
* Handle Table updates
|
|
*
|
|
* @description Try to undate table, catch error if anything goes wrong
|
|
*/
|
|
try {
|
|
const { allExistingColumns, upToDateTableFieldsArray } =
|
|
await handleMariaDBExistingColumns({
|
|
dbFullName,
|
|
dbSchema,
|
|
fields: tableFields,
|
|
tableName,
|
|
userId,
|
|
});
|
|
|
|
/**
|
|
* Handle DATASQUIREL schema fields for current table
|
|
* ===================================================
|
|
* @description Iterate through each field object and
|
|
* perform operations
|
|
*/
|
|
await handleDSQLSchemaFields({
|
|
dbFullName,
|
|
tableName,
|
|
fields: upToDateTableFieldsArray,
|
|
allExistingColumns,
|
|
});
|
|
|
|
/**
|
|
* Handle MYSQL Foreign Keys
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table index(if available), and perform operations
|
|
*/
|
|
await handleTableForeignKey({
|
|
dbFullName,
|
|
fields: upToDateTableFieldsArray,
|
|
tableName,
|
|
clone,
|
|
});
|
|
|
|
/**
|
|
* Handle DATASQUIREL Table Indexes
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table index(if available), and perform operations
|
|
*/
|
|
if (tableIndexes?.[0]) {
|
|
handleIndexescreateDbFromSchema({
|
|
dbFullName,
|
|
indexes: tableIndexes,
|
|
tableName,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Handle DATASQUIREL Table Unique Indexes
|
|
* ===================================================
|
|
* @description Iterate through each datasquirel schema
|
|
* table unique constraint(if available), and perform operations
|
|
*/
|
|
if (tableUniqueConstraints?.[0]) {
|
|
handleUniqueConstraintsCreateDbFromSchema({
|
|
dbFullName,
|
|
tableUniqueConstraints,
|
|
tableName,
|
|
});
|
|
}
|
|
} catch (error: any) {
|
|
console.log('Error in "updateTable" shell function =>', error.message);
|
|
}
|
|
|
|
return tableID;
|
|
}
|