This commit is contained in:
Lizandro Guarnizo
2026-01-24 00:55:33 -05:00
parent 41b4c2fb32
commit 60e41bfa1a
3 changed files with 1579 additions and 2 deletions
+18 -2
View File
@@ -57,7 +57,11 @@ try {
$rows = [];
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
if (empty($cols)) $cols = array_keys($r);
$values = array_map(function($v) use ($pdo) { return $pdo->quote($v); }, array_values($r));
$values = array_map(function($v) use ($pdo) {
if (is_null($v)) return 'NULL';
// Ensure non-binary scalars are properly quoted as strings
return $pdo->quote((string)$v);
}, array_values($r));
$rows[] = '(' . implode(',', $values) . ')';
if (count($rows) >= $batchSize) {
$line = "INSERT INTO `conversations` (`" . implode('`,`', $cols) . "`) VALUES\n" . implode(",\n", $rows) . ";\n";
@@ -93,14 +97,26 @@ $columnsToAdd = [
'status' => "VARCHAR(32) DEFAULT 'received'"
];
// Gather existing columns and their types
$existingColsStmt = $pdo->query("SHOW COLUMNS FROM `conversations`");
$existingCols = [];
foreach ($existingColsStmt->fetchAll(PDO::FETCH_ASSOC) as $c) $existingCols[] = $c['Field'];
$existingTypes = []; // field -> Type
foreach ($existingColsStmt->fetchAll(PDO::FETCH_ASSOC) as $c) {
$existingCols[] = $c['Field'];
$existingTypes[$c['Field']] = strtolower($c['Type']);
}
$alters = [];
foreach ($columnsToAdd as $col => $type) {
if (!in_array($col, $existingCols)) {
$alters[] = "ADD COLUMN `{$col}` {$type}";
} else {
// If the column exists but its type doesn't match the desired one, schedule a MODIFY
$desiredType = strtolower(preg_replace('/\s+default\s+.*/i', '', $type)); // e.g. 'varchar(255)'
if (!str_starts_with($existingTypes[$col], $desiredType)) {
$alters[] = "MODIFY COLUMN `{$col}` {$type}";
echo "Will modify column type for {$col}: found {$existingTypes[$col]} -> desired {$desiredType}\n";
}
}
}