updating install file
This commit is contained in:
parent
ad6d09dcf3
commit
85c1b9dd67
@ -3,17 +3,80 @@ require_once __DIR__ . '/../config.php';
|
|||||||
|
|
||||||
function runMigrations() {
|
function runMigrations() {
|
||||||
$pdo = db();
|
$pdo = db();
|
||||||
|
// Create migration table if not exists
|
||||||
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
migration_name VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)");
|
||||||
|
|
||||||
|
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
|
||||||
|
|
||||||
$migrationsDir = __DIR__;
|
$migrationsDir = __DIR__;
|
||||||
$files = glob($migrationsDir . '/*.sql');
|
$files = glob($migrationsDir . '/*.sql');
|
||||||
sort($files);
|
sort($files);
|
||||||
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
|
$migration_name = basename($file);
|
||||||
|
|
||||||
|
// Check if already applied
|
||||||
|
$stmt = $pdo->prepare("SELECT id FROM migrations WHERE migration_name = ?");
|
||||||
|
$stmt->execute([$migration_name]);
|
||||||
|
if ($stmt->fetch()) {
|
||||||
|
echo "Already applied: $migration_name" . PHP_EOL;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Applying: $migration_name" . PHP_EOL;
|
||||||
$sql = file_get_contents($file);
|
$sql = file_get_contents($file);
|
||||||
try {
|
|
||||||
$pdo->exec($sql);
|
// Split SQL into individual statements by ; followed by newline
|
||||||
echo "Successfully applied migration: " . basename($file) . PHP_EOL;
|
$statements = preg_split('/;(?:\\s*[\r\n]+)/', $sql);
|
||||||
} catch (PDOException $e) {
|
|
||||||
echo "Error applying migration " . basename($file) . ": " . $e->getMessage() . PHP_EOL;
|
$success = true;
|
||||||
|
foreach ($statements as $statement) {
|
||||||
|
$statement = trim($statement);
|
||||||
|
if (empty($statement)) continue;
|
||||||
|
|
||||||
|
// Basic comment removal
|
||||||
|
$statement_lines = explode("\n", $statement);
|
||||||
|
$clean_statement = "";
|
||||||
|
foreach ($statement_lines as $line) {
|
||||||
|
if (trim(substr(trim($line), 0, 2)) === '--') continue;
|
||||||
|
$clean_statement .= $line . "\n";
|
||||||
|
}
|
||||||
|
$clean_statement = trim($clean_statement);
|
||||||
|
if (empty($clean_statement)) continue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$stmt = $pdo->query($clean_statement);
|
||||||
|
if ($stmt) {
|
||||||
|
$stmt->closeCursor();
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$msg = $e->getMessage();
|
||||||
|
// If the error is about a table already existing, it's fine for our idempotency
|
||||||
|
if (strpos($msg, "already exists") !== false ||
|
||||||
|
strpos($msg, "Duplicate column") !== false ||
|
||||||
|
strpos($msg, "Duplicate entry") !== false ||
|
||||||
|
strpos($msg, "already a column") !== false ||
|
||||||
|
strpos($msg, "Duplicate key") !== false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
echo "Error in $migration_name at statement: " . substr($clean_statement, 0, 100) . "... " . PHP_EOL;
|
||||||
|
echo "Error: " . $msg . PHP_EOL;
|
||||||
|
$success = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($success) {
|
||||||
|
$stmt = $pdo->prepare("INSERT INTO migrations (migration_name) VALUES (?)");
|
||||||
|
$stmt->execute([$migration_name]);
|
||||||
|
echo "Successfully applied migration: $migration_name" . PHP_EOL;
|
||||||
|
} else {
|
||||||
|
echo "Migration failed: $migration_name. Stopping." . PHP_EOL;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -111,7 +111,7 @@ if (isLoggedIn()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auth Check (after fetch to ensure session is updated)
|
// Auth Check (after fetch to ensure session is updated)
|
||||||
if (!isLoggedIn() && basename($_SERVER['PHP_SELF']) !== 'login.php' && basename($_SERVER['PHP_SELF']) !== 'forgot_password.php') {
|
if (!isLoggedIn() && basename($_SERVER['PHP_SELF']) !== 'login.php' && basename($_SERVER['PHP_SELF']) !== 'forgot_password.php' && basename($_SERVER['PHP_SELF']) !== 'install.php') {
|
||||||
redirect('login.php');
|
redirect('login.php');
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
83
install.php
Normal file → Executable file
83
install.php
Normal file → Executable file
@ -56,6 +56,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$content .= " \$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$content .= " \$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
";
|
";
|
||||||
$content .= " \$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
$content .= " \$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||||||
|
";
|
||||||
|
$content .= " \$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
|
||||||
";
|
";
|
||||||
$content .= " } catch (PDOException \$e) {\n";
|
$content .= " } catch (PDOException \$e) {\n";
|
||||||
$content .= " die('Connection failed: ' . \$e->getMessage());\n";
|
$content .= " die('Connection failed: ' . \$e->getMessage());\n";
|
||||||
@ -85,6 +87,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
throw new Exception("The 'db()' function is not defined in your config file.");
|
throw new Exception("The 'db()' function is not defined in your config file.");
|
||||||
}
|
}
|
||||||
$pdo = db();
|
$pdo = db();
|
||||||
|
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
|
||||||
|
|
||||||
|
// Create migration table if not exists
|
||||||
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
migration_name VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)");
|
||||||
|
|
||||||
$migrations_dir = __DIR__ . '/db/migrations/';
|
$migrations_dir = __DIR__ . '/db/migrations/';
|
||||||
$files = glob($migrations_dir . '*.sql');
|
$files = glob($migrations_dir . '*.sql');
|
||||||
if ($files === false) $files = [];
|
if ($files === false) $files = [];
|
||||||
@ -94,35 +105,69 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
$errors = [];
|
$errors = [];
|
||||||
|
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
|
$migration_name = basename($file);
|
||||||
|
|
||||||
|
// Check if already applied
|
||||||
|
$stmt = $pdo->prepare("SELECT id FROM migrations WHERE migration_name = ?");
|
||||||
|
$stmt->execute([$migration_name]);
|
||||||
|
if ($stmt->fetch()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$sql = file_get_contents($file);
|
$sql = file_get_contents($file);
|
||||||
if (empty($sql)) continue;
|
if (empty($sql)) continue;
|
||||||
|
|
||||||
try {
|
// Split SQL into individual statements by ; followed by newline
|
||||||
$statements = array_filter(array_map('trim', explode(';', $sql)));
|
$statements = preg_split('/;(?:\\s*[
|
||||||
foreach ($statements as $stmt_sql) {
|
]+)/', $sql);
|
||||||
if (empty($stmt_sql)) continue;
|
|
||||||
try {
|
$file_success = true;
|
||||||
$pdo->exec($stmt_sql);
|
foreach ($statements as $stmt_sql) {
|
||||||
} catch (Throwable $e) {
|
$stmt_sql = trim($stmt_sql);
|
||||||
$msg = $e->getMessage();
|
if (empty($stmt_sql)) continue;
|
||||||
if (strpos($msg, 'Duplicate column name') !== false ||
|
|
||||||
strpos($msg, 'Duplicate key name') !== false ||
|
// Basic comment removal
|
||||||
strpos($msg, 'Duplicate table') !== false ||
|
$stmt_lines = explode("\n", $stmt_sql);
|
||||||
strpos($msg, 'already exists') !== false || strpos($msg, 'Duplicate key on write or update') !== false || strpos($msg, 'errno: 121') !== false) {
|
$clean_stmt = "";
|
||||||
continue;
|
foreach ($stmt_lines as $line) {
|
||||||
} else {
|
if (trim(substr(trim($line), 0, 2)) === '--') continue;
|
||||||
throw $e;
|
$clean_stmt .= $line . "\n";
|
||||||
}
|
}
|
||||||
|
$clean_stmt = trim($clean_stmt);
|
||||||
|
if (empty($clean_stmt)) continue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$res = $pdo->query($clean_stmt);
|
||||||
|
if ($res) {
|
||||||
|
$res->closeCursor();
|
||||||
|
}
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
$msg = $e->getMessage();
|
||||||
|
// If the error is about a table already existing, it's fine
|
||||||
|
if (strpos($msg, "already exists") !== false ||
|
||||||
|
strpos($msg, "Duplicate column") !== false ||
|
||||||
|
strpos($msg, "Duplicate entry") !== false ||
|
||||||
|
strpos($msg, "already a column") !== false ||
|
||||||
|
strpos($msg, "Duplicate key") !== false ||
|
||||||
|
strpos($msg, "errno: 121") !== false) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
$errors[] = $migration_name . " at statement: " . substr($clean_stmt, 0, 50) . "... Error: " . $msg;
|
||||||
|
$file_success = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($file_success) {
|
||||||
|
$ins = $pdo->prepare("INSERT INTO migrations (migration_name) VALUES (?)");
|
||||||
|
$ins->execute([$migration_name]);
|
||||||
$applied++;
|
$applied++;
|
||||||
} catch (Throwable $e) {
|
|
||||||
$errors[] = basename($file) . ": " . $e->getMessage();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($errors)) {
|
if (empty($errors)) {
|
||||||
$success = "Successfully applied $applied migrations.";
|
$success = "Successfully applied migrations.";
|
||||||
header('Location: ' . htmlspecialchars($_SERVER['SCRIPT_NAME']) . '?step=4');
|
header('Location: ' . htmlspecialchars($_SERVER['SCRIPT_NAME']) . '?step=4');
|
||||||
exit;
|
exit;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
1
test_install.php
Normal file
1
test_install.php
Normal file
@ -0,0 +1 @@
|
|||||||
|
<?php echo "Test file works!"; ?>
|
||||||
Loading…
x
Reference in New Issue
Block a user