Updates
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import type {
|
||||
BUN_MARIADB_INFORMATION_SCHEMA_TABLES,
|
||||
CreateDBSchemaParams,
|
||||
} from "../../types";
|
||||
import dbHandler from "../db-handler";
|
||||
import getExistingTablesFromTablesManagerTable from "./get-existing-tables-from-tables-manager-table";
|
||||
import handleDBSchemaTable from "./handle-db-schema-table";
|
||||
|
||||
export default async function handleDBSchemaTables(
|
||||
params: CreateDBSchemaParams,
|
||||
) {
|
||||
const { db_schema, config } = params;
|
||||
|
||||
/**
|
||||
* Grab Tables that have been recorded in the Schema
|
||||
* Manager Table
|
||||
*/
|
||||
const existing_schema_tables =
|
||||
await getExistingTablesFromTablesManagerTable(params);
|
||||
|
||||
const existing_live_tables =
|
||||
await dbHandler<BUN_MARIADB_INFORMATION_SCHEMA_TABLES>({
|
||||
query: `SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME != ?`,
|
||||
config: params.config,
|
||||
values: [AppData["DbSchemaManagerTableName"]],
|
||||
});
|
||||
|
||||
for (let i = 0; i < db_schema.tables.length; i++) {
|
||||
const table = db_schema.tables[i];
|
||||
|
||||
const existing_table = existing_schema_tables.find(
|
||||
(t) => t == table?.tableName,
|
||||
);
|
||||
|
||||
const existing_live_table = existing_live_tables.payload?.find(
|
||||
(t) => t.TABLE_NAME == table?.tableName,
|
||||
);
|
||||
|
||||
if (!table) {
|
||||
if (existing_live_table?.TABLE_NAME) {
|
||||
await dbHandler<BUN_MARIADB_INFORMATION_SCHEMA_TABLES>({
|
||||
query: `DROP TABLE ${existing_live_table.TABLE_NAME}`,
|
||||
config: params.config,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
await handleDBSchemaTable({
|
||||
...params,
|
||||
table,
|
||||
db_manager_table_name: existing_table,
|
||||
existing_live_table,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user