42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function run_migrations() {
|
|
$pdo = db();
|
|
|
|
// Ensure migrations table exists
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) NOT NULL, PRIMARY KEY (migration))");
|
|
|
|
// Get executed migrations
|
|
$executedMigrations = $pdo->query("SELECT migration FROM migrations")->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$migrationsDir = __DIR__ . '/migrations';
|
|
$files = glob($migrationsDir . '/*.sql');
|
|
sort($files);
|
|
|
|
foreach ($files as $file) {
|
|
$migrationName = basename($file);
|
|
if (in_array($migrationName, $executedMigrations)) {
|
|
continue;
|
|
}
|
|
|
|
echo "Running migration: " . $migrationName . "\n";
|
|
$sql = file_get_contents($file);
|
|
try {
|
|
$pdo->exec($sql);
|
|
|
|
// Record migration
|
|
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
|
|
$stmt->execute([$migrationName]);
|
|
|
|
echo "Success.\n";
|
|
} catch (PDOException $e) {
|
|
echo "Error: " . $e->getMessage() . "\n";
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
echo "All migrations have been run.\n";
|
|
}
|
|
|
|
run_migrations(); |