50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
require_once 'config.php';
|
|
|
|
function run_migrations() {
|
|
try {
|
|
$pdo = db();
|
|
if (!$pdo) {
|
|
throw new Exception("Failed to connect to the database.");
|
|
}
|
|
|
|
$migrationsDir = __DIR__ . '/migrations';
|
|
if (!is_dir($migrationsDir)) {
|
|
mkdir($migrationsDir, 0775, true);
|
|
}
|
|
|
|
$files = glob($migrationsDir . '/*.sql');
|
|
sort($files);
|
|
|
|
$output = [];
|
|
foreach ($files as $file) {
|
|
$sql = file_get_contents($file);
|
|
if (!empty(trim($sql))) {
|
|
$pdo->exec($sql);
|
|
$output[] = "Executed migration: " . basename($file);
|
|
}
|
|
}
|
|
|
|
return ['success' => true, 'output' => $output];
|
|
|
|
} catch (Exception $e) {
|
|
return ['success' => false, 'error' => "Database migration failed: " . $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
// If the script is run directly from the command line, execute the migrations.
|
|
if (php_sapi_name() === 'cli') {
|
|
$result = run_migrations();
|
|
if ($result['success']) {
|
|
foreach ($result['output'] as $line) {
|
|
echo $line . "
|
|
";
|
|
}
|
|
echo "Migrations completed successfully.
|
|
";
|
|
} else {
|
|
echo $result['error'] . "
|
|
";
|
|
}
|
|
}
|