import { DSQL_DatabaseSchemaType, DSQL_FieldSchemaType, DSQL_MYSQL_SHOW_COLUMNS_Type, } from "../../types"; import dbHandler from "../../functions/backend/dbHandler"; import defaultFieldsRegexp from "../../functions/dsql/default-fields-regexp"; import { writeUpdatedDbSchema } from "../createDbFromSchema/grab-required-database-schemas"; import _ from "lodash"; type Param = { dbFullName: string; tableName: string; fields: DSQL_FieldSchemaType[]; dbSchema: DSQL_DatabaseSchemaType; userId?: number | string | null; }; /** * Handle MYSQL Columns (Fields) * =================================================== * @description Now handle all fields/columns */ export default async function handleMariaDBExistingColumns({ dbFullName, tableName, fields, dbSchema, userId, }: Param) { let upToDateTableFieldsArray = _.cloneDeep(fields); let allExistingColumns: DSQL_MYSQL_SHOW_COLUMNS_Type[] = (await dbHandler({ query: `SHOW COLUMNS FROM \`${dbFullName}\`.\`${tableName}\``, })) as DSQL_MYSQL_SHOW_COLUMNS_Type[]; /** * @description Iterate through every existing column */ for (let e = 0; e < allExistingColumns.length; e++) { const { Field } = allExistingColumns[e]; if (Field.match(defaultFieldsRegexp)) 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) { await dbHandler({ query: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP COLUMN \`${Field}\``, }); continue; } if (existingEntry) { /** * @description Check if Field name has been updated */ if (existingEntry.updatedField && existingEntry.fieldName) { await dbHandler({ 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 = _.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) { writeUpdatedDbSchema({ dbSchema: updatedSchemaData, userId, }); } allExistingColumns = (await dbHandler({ query: `SHOW COLUMNS FROM \`${dbFullName}\`.\`${tableName}\``, })) as DSQL_MYSQL_SHOW_COLUMNS_Type[]; } catch (error: any) { console.log("Update table error =>", error.message); } //////////////////////////////////////// } continue; } } return { upToDateTableFieldsArray, allExistingColumns }; }