up
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user