58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import type {
|
|
BUN_MARIADB_TableSchemaType,
|
|
BunMariaDBConfig,
|
|
} from "../../types";
|
|
import MariaDBQuoteGen from "./mariadb-quote-gen";
|
|
import runSchemaQuery, { querySchemaRows } from "./run-schema-query";
|
|
import schemaCondition from "./schema-condition";
|
|
|
|
export default async function syncTableOptions({
|
|
table,
|
|
config,
|
|
}: {
|
|
table: BUN_MARIADB_TableSchemaType;
|
|
config?: BunMariaDBConfig;
|
|
}): Promise<void> {
|
|
const schemaCond = schemaCondition(config);
|
|
const rows = await querySchemaRows<{
|
|
ENGINE: string;
|
|
TABLE_COLLATION: string;
|
|
}>({
|
|
query: `SELECT ENGINE, TABLE_COLLATION FROM information_schema.TABLES WHERE ${schemaCond.where} AND TABLE_NAME = ? AND TABLE_TYPE = 'BASE TABLE'`,
|
|
values: [...schemaCond.values, table.tableName],
|
|
config,
|
|
});
|
|
|
|
const live = rows[0];
|
|
if (!live) {
|
|
return;
|
|
}
|
|
|
|
const alters: string[] = [];
|
|
|
|
if ((live.ENGINE || "").toUpperCase() !== "INNODB") {
|
|
alters.push("ENGINE=InnoDB");
|
|
}
|
|
|
|
if (table.collation) {
|
|
const liveCollation = (live.TABLE_COLLATION || "").toLowerCase();
|
|
if (liveCollation !== table.collation.toLowerCase()) {
|
|
alters.push(
|
|
`CONVERT TO CHARACTER SET utf8mb4 COLLATE ${table.collation}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
if (alters.length === 0) {
|
|
return;
|
|
}
|
|
|
|
console.log(
|
|
`Updating table options on ${table.tableName}: ${alters.join(", ")}`,
|
|
);
|
|
await runSchemaQuery({
|
|
query: `ALTER TABLE ${MariaDBQuoteGen(table.tableName)} ${alters.join(", ")}`,
|
|
config,
|
|
});
|
|
}
|