63 lines
2.5 KiB
JavaScript
63 lines
2.5 KiB
JavaScript
import createTable from "./create-table";
|
|
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
|
import resolveTable from "./resolve-table";
|
|
import runSchemaQuery, { querySchemaRows } from "./run-schema-query";
|
|
import schemaCondition from "./schema-condition";
|
|
import syncIndexes from "./sync-indexes";
|
|
import updateTable from "./update-table";
|
|
import upsertDbManagerTable, { removeDbManagerTable, } from "./upsert-db-manager-table";
|
|
export default async function handleDBSchemaTable({ db_schema, config, table, db_manager_table_name, existing_live_table, }) {
|
|
const resolvedTable = resolveTable(table, db_schema);
|
|
let tableExistsTracked = Boolean(db_manager_table_name);
|
|
let tableExistsLive = Boolean(existing_live_table?.TABLE_NAME);
|
|
let wasRenamed = false;
|
|
if (resolvedTable.tableNameOld &&
|
|
resolvedTable.tableNameOld !== resolvedTable.tableName) {
|
|
// Only hit information_schema when a rename is declared
|
|
const schemaCond = schemaCondition(config);
|
|
const liveTables = await querySchemaRows({
|
|
query: `SELECT TABLE_NAME FROM information_schema.TABLES WHERE ${schemaCond.where} AND TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = ?`,
|
|
values: [...schemaCond.values, resolvedTable.tableNameOld],
|
|
config,
|
|
});
|
|
if (liveTables.length > 0) {
|
|
console.log(`Renaming table: ${resolvedTable.tableNameOld} -> ${resolvedTable.tableName}`);
|
|
await runSchemaQuery({
|
|
query: `RENAME TABLE ${MariaDBQuoteGen(resolvedTable.tableNameOld)} TO ${MariaDBQuoteGen(resolvedTable.tableName)}`,
|
|
config,
|
|
});
|
|
await upsertDbManagerTable({
|
|
tableName: resolvedTable.tableName,
|
|
config,
|
|
});
|
|
await removeDbManagerTable({
|
|
tableName: resolvedTable.tableNameOld,
|
|
config,
|
|
});
|
|
tableExistsTracked = true;
|
|
tableExistsLive = true;
|
|
wasRenamed = true;
|
|
}
|
|
}
|
|
if (!tableExistsTracked && !tableExistsLive) {
|
|
await createTable({ table: resolvedTable, config });
|
|
await upsertDbManagerTable({
|
|
tableName: resolvedTable.tableName,
|
|
config,
|
|
});
|
|
}
|
|
else {
|
|
if (!wasRenamed) {
|
|
await updateTable({
|
|
table: resolvedTable,
|
|
config,
|
|
});
|
|
}
|
|
await upsertDbManagerTable({
|
|
tableName: resolvedTable.tableName,
|
|
config,
|
|
});
|
|
}
|
|
await syncIndexes({ table: resolvedTable, config });
|
|
}
|