34 lines
1.3 KiB
JavaScript
34 lines
1.3 KiB
JavaScript
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, }) {
|
|
const schemaCond = schemaCondition(config);
|
|
const rows = await querySchemaRows({
|
|
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 = [];
|
|
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,
|
|
});
|
|
}
|