Add column existence checks in schema bootstrap to prevent redundant ALTER queries

- Introduced `columnExists` helper to verify column presence before performing ALTER operations.
- Updated schema bootstrap logic to use `columnExists` for `wash_date` and `category` columns.
This commit is contained in:
Jeppe Bundgaard
2026-04-14 15:57:26 +02:00
parent 2ba7451669
commit 55a5ce453a
@@ -34,17 +34,21 @@ class department_daily_report_complaints_schema_bootstrap
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"
);
$db->query(
"ALTER TABLE " . self::TABLE . "
ADD COLUMN IF NOT EXISTS wash_date DATE NULL
AFTER customer_number"
);
if (!self::columnExists(self::TABLE, 'wash_date')) {
$db->query(
"ALTER TABLE " . self::TABLE . "
ADD COLUMN wash_date DATE NULL
AFTER customer_number"
);
}
$db->query(
"ALTER TABLE " . self::TABLE . "
ADD COLUMN IF NOT EXISTS category VARCHAR(64) NULL
AFTER wash_date"
);
if (!self::columnExists(self::TABLE, 'category')) {
$db->query(
"ALTER TABLE " . self::TABLE . "
ADD COLUMN category VARCHAR(64) NULL
AFTER wash_date"
);
}
if (!self::indexExists(self::TABLE, self::WASH_DATE_INDEX)) {
$db->query(
@@ -68,4 +72,17 @@ class department_daily_report_complaints_schema_bootstrap
return $result !== false && $result->num_rows > 0;
}
private static function columnExists(string $table, string $column): bool
{
global $db;
$table_sql = $db->escape_string($table);
$column_sql = $db->escape_string($column);
$result = $db->query(
"SHOW COLUMNS FROM `$table_sql` LIKE '$column_sql'"
);
return $result !== false && $result->num_rows > 0;
}
}