diff --git a/install.php b/install.php
index 976f985..45b21d3 100644
--- a/install.php
+++ b/install.php
@@ -35,8 +35,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Test connection
try {
- $pdo = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ $test_pdo = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
+ $test_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Generate config file content
$content = "setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n";
- $content .= " $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);\n";
- $content .= " } catch (PDOException $e) {\n";
- $content .= " die('Connection failed: ' . $e->getMessage());\n";
+ $content .= " \$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);\n";
+ $content .= " \$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n";
+ $content .= " \$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);\n";
+ $content .= " } catch (PDOException \$e) {\n";
+ $content .= " die('Connection failed: ' . \$e->getMessage());\n";
$content .= " }\n";
$content .= " }\n";
- $content .= " return $pdo;\n";
+ $content .= " return \$pdo;\n";
$content .= " }\n";
$content .= "}\n";
@@ -84,6 +84,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pdo = db();
$migrations_dir = __DIR__ . '/db/migrations/';
$files = glob($migrations_dir . '*.sql');
+ if ($files === false) $files = [];
sort($files);
$applied = 0;
@@ -94,20 +95,30 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (empty($sql)) continue;
try {
- // PDO::exec might fail if there's an error in the SQL,
- // but we want to continue if it's "column already exists" or "table already exists"
- $pdo->exec($sql);
+ // Split SQL into multiple statements if necessary
+ // Simple split by semicolon. This might fail on some complex SQL,
+ // but usually works for basic migrations.
+ $statements = array_filter(array_map('trim', explode(';', $sql)));
+ foreach ($statements as $stmt_sql) {
+ if (empty($stmt_sql)) continue;
+ try {
+ $pdo->exec($stmt_sql);
+ } catch (Throwable $e) {
+ $msg = $e->getMessage();
+ // Check if it's a common "already exists" error which we can safely ignore
+ if (strpos($msg, 'Duplicate column name') !== false ||
+ strpos($msg, 'Duplicate key name') !== false ||
+ strpos($msg, 'Duplicate table') !== false ||
+ strpos($msg, 'already exists') !== false) {
+ continue;
+ } else {
+ throw $e;
+ }
+ }
+ }
$applied++;
} catch (Throwable $e) {
- $msg = $e->getMessage();
- // Check if it's a common "already exists" error which we can safely ignore
- if (strpos($msg, 'Duplicate column name') !== false ||
- strpos($msg, 'Duplicate key name') !== false ||
- strpos($msg, 'already exists') !== false) {
- $applied++;
- } else {
- $errors[] = basename($file) . ": " . $msg;
- }
+ $errors[] = basename($file) . ": " . $e->getMessage();
}
}
@@ -116,7 +127,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
header('Location: install.php?step=4');
exit;
} else {
- $error = "Applied $applied migrations, but some errors occurred: