chore: change analytics_type to string type from enum (#7544)

This commit is contained in:
Louis Lam
2026-06-25 19:06:54 +08:00
committed by GitHub
parent e1937a6d25
commit 584fc0761c
2 changed files with 22 additions and 28 deletions

View File

@@ -1,33 +1,9 @@
const newValues = ["google", "umami", "plausible", "matomo", "rybbit"];
const oldValues = ["google", "umami", "plausible", "matomo"];
/**
* Rebuild the status_page.analytics_type enum with the given values, keeping existing data.
* The column is dropped and re-created because SQLite's .enu().alter() does not replace the old CHECK constraint.
* @param {import("knex").Knex} knex The knex instance
* @param {string[]} allowedValues Allowed analytics_type values
* @returns {Promise<void>}
*/
async function rebuildAnalyticsType(knex, allowedValues) {
const rows = await knex("status_page").whereNotNull("analytics_type").select("id", "analytics_type");
await knex.schema.alterTable("status_page", (table) => {
table.dropColumn("analytics_type");
});
await knex.schema.alterTable("status_page", (table) => {
table.enu("analytics_type", allowedValues).defaultTo(null);
});
for (const row of rows) {
await knex("status_page").where("id", row.id).update({ analytics_type: row.analytics_type });
}
}
exports.up = function (knex) {
return rebuildAnalyticsType(knex, newValues);
// Intentionally left blank - no operation
return Promise.resolve();
};
exports.down = async function (knex) {
await knex("status_page").where("analytics_type", "rybbit").update({ analytics_type: null });
await rebuildAnalyticsType(knex, oldValues);
// Intentionally left blank - no operation
return Promise.resolve();
};

View File

@@ -0,0 +1,18 @@
const oldValues = ["google", "umami", "plausible", "matomo", "rybbit"];
exports.up = function (knex) {
return knex.schema.alterTable("status_page", function (table) {
table.string("analytics_type").nullable().defaultTo(null).alter();
});
};
exports.down = async function (knex) {
await knex("status_page")
.whereNotNull("analytics_type")
.whereNotIn("analytics_type", oldValues)
.update({ analytics_type: null });
return knex.schema.alterTable("status_page", function (table) {
table.enu("analytics_type", oldValues).nullable().defaultTo(null).alter();
});
};