install file 2
This commit is contained in:
parent
d2bc35de8c
commit
10eedb49b2
94
install.php
94
install.php
@ -35,8 +35,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
|
|
||||||
// Test connection
|
// Test connection
|
||||||
try {
|
try {
|
||||||
$pdo = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
|
$test_pdo = new PDO("mysql:host=$host;dbname=$name;charset=utf8mb4", $user, $pass);
|
||||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$test_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
|
|
||||||
// Generate config file content
|
// Generate config file content
|
||||||
$content = "<?php\n";
|
$content = "<?php\n";
|
||||||
@ -48,17 +48,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$content .= "\n";
|
$content .= "\n";
|
||||||
$content .= "if (!function_exists('db')) {\n";
|
$content .= "if (!function_exists('db')) {\n";
|
||||||
$content .= " function db() {\n";
|
$content .= " function db() {\n";
|
||||||
$content .= " static $pdo;\n";
|
$content .= " static \$pdo;\n";
|
||||||
$content .= " if (!$pdo) {\n";
|
$content .= " if (!\$pdo) {\n";
|
||||||
$content .= " try {\n";
|
$content .= " try {\n";
|
||||||
$content .= " $pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4', DB_USER, DB_PASS);\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_ERRMODE, PDO::ERRMODE_EXCEPTION);\n";
|
||||||
$content .= " $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);\n";
|
$content .= " \$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);\n";
|
||||||
$content .= " } catch (PDOException $e) {\n";
|
$content .= " } catch (PDOException \$e) {\n";
|
||||||
$content .= " die('Connection failed: ' . $e->getMessage());\n";
|
$content .= " die('Connection failed: ' . \$e->getMessage());\n";
|
||||||
$content .= " }\n";
|
$content .= " }\n";
|
||||||
$content .= " }\n";
|
$content .= " }\n";
|
||||||
$content .= " return $pdo;\n";
|
$content .= " return \$pdo;\n";
|
||||||
$content .= " }\n";
|
$content .= " }\n";
|
||||||
$content .= "}\n";
|
$content .= "}\n";
|
||||||
|
|
||||||
@ -84,6 +84,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$pdo = db();
|
$pdo = db();
|
||||||
$migrations_dir = __DIR__ . '/db/migrations/';
|
$migrations_dir = __DIR__ . '/db/migrations/';
|
||||||
$files = glob($migrations_dir . '*.sql');
|
$files = glob($migrations_dir . '*.sql');
|
||||||
|
if ($files === false) $files = [];
|
||||||
sort($files);
|
sort($files);
|
||||||
|
|
||||||
$applied = 0;
|
$applied = 0;
|
||||||
@ -94,20 +95,30 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
if (empty($sql)) continue;
|
if (empty($sql)) continue;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// PDO::exec might fail if there's an error in the SQL,
|
// Split SQL into multiple statements if necessary
|
||||||
// but we want to continue if it's "column already exists" or "table already exists"
|
// Simple split by semicolon. This might fail on some complex SQL,
|
||||||
$pdo->exec($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++;
|
$applied++;
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
$msg = $e->getMessage();
|
$errors[] = basename($file) . ": " . $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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +127,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
header('Location: install.php?step=4');
|
header('Location: install.php?step=4');
|
||||||
exit;
|
exit;
|
||||||
} else {
|
} else {
|
||||||
$error = "Applied $applied migrations, but some errors occurred:<br><ul><li>" . implode('</li><li>', $errors) . "</li></ul>";
|
$error = "Applied migrations, but some errors occurred:<br><ul><li>" . implode('</li><li>', $errors) . "</li></ul>";
|
||||||
}
|
}
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
$error = "Migration failed: " . $e->getMessage();
|
$error = "Migration failed: " . $e->getMessage();
|
||||||
@ -178,9 +189,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
<h2 class="text-center mb-4">Installer</h2>
|
<h2 class="text-center mb-4">Installer</h2>
|
||||||
|
|
||||||
<div class="text-center step-indicator">
|
<div class="text-center step-indicator">
|
||||||
<?php for ($i = 1; $i <= 5; $i++): ?>
|
<?php for ($i = 1; $i <= 5; $i++):
|
||||||
<span class="step-dot <?= $i == $step ? 'active' : ($i < $step ? 'completed' : '') ?>"><?= $i ?></span>
|
echo "<span class=\"step-dot ";
|
||||||
<?php endfor; ?>
|
if ($i == $step) echo "active";
|
||||||
|
elseif ($i < $step) echo "completed";
|
||||||
|
echo "\">$i</span>\n";
|
||||||
|
endfor; ?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?php if ($error): ?>
|
<?php if ($error): ?>
|
||||||
@ -194,23 +208,23 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
<?php if ($step === 1): ?>
|
<?php if ($step === 1): ?>
|
||||||
<h4>Step 1: System Requirements</h4>
|
<h4>Step 1: System Requirements</h4>
|
||||||
<ul class="list-group mb-4">
|
<ul class="list-group mb-4">
|
||||||
<?php foreach ($requirements as $name => $met): ?>
|
<?php foreach ($requirements as $name => $met):
|
||||||
<li class="list-group-item d-flex justify-content-between align-items-center">
|
echo "<li class=\"list-group-item d-flex justify-content-between align-items-center\">";
|
||||||
<?= $name ?>
|
echo "$name\n";
|
||||||
<?php if ($met): ?>
|
if ($met) {
|
||||||
<span class="badge bg-success rounded-pill">OK</span>
|
echo "<span class=\"badge bg-success rounded-pill\">OK</span>";
|
||||||
<?php else: ?>
|
} else {
|
||||||
<span class="badge bg-danger rounded-pill">Failed</span>
|
echo "<span class=\"badge bg-danger rounded-pill\">Failed</span>";
|
||||||
<?php endif; ?>
|
}
|
||||||
</li>
|
echo "</li>\n";
|
||||||
<?php endforeach; ?>
|
endforeach; ?>
|
||||||
</ul>
|
</ul>
|
||||||
<div class="d-grid">
|
<div class="d-grid">
|
||||||
<?php if ($all_requirements_met): ?>
|
<?php if ($all_requirements_met): ?>
|
||||||
<a href="install.php?step=2" class="btn btn-primary">Next: Database Config</a>
|
<a href="install.php?step=2" class="btn btn-primary">Next: Database Config</a>
|
||||||
<?php else: ?>
|
<?php else:
|
||||||
<button class="btn btn-secondary" disabled>Fix requirements to continue</button>
|
echo "<button class=\"btn btn-secondary\" disabled>Fix requirements to continue</button>";
|
||||||
<?php endif; ?>
|
endif; ?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?php elseif ($step === 2): ?>
|
<?php elseif ($step === 2): ?>
|
||||||
@ -279,4 +293,4 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
x
Reference in New Issue
Block a user