40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
echo "Connected to database.\n";
|
|
|
|
// 1. Create migrations table if it doesn't exist
|
|
$migrationTableSql = file_get_contents(__DIR__ . '/migrations/000_create_migrations_table.sql');
|
|
$pdo->exec($migrationTableSql);
|
|
echo "Migrations table ensured.\n";
|
|
|
|
// 2. Get executed migrations
|
|
$stmt = $pdo->query("SELECT migration FROM migrations");
|
|
$executedMigrations = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// 3. Find and execute new migrations
|
|
$migrationFiles = glob(__DIR__ . '/migrations/*.sql');
|
|
sort($migrationFiles);
|
|
|
|
foreach ($migrationFiles as $file) {
|
|
$migrationName = basename($file);
|
|
if (!in_array($migrationName, $executedMigrations)) {
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
|
|
$stmt->execute([$migrationName]);
|
|
|
|
echo "Executed migration: $migrationName\n";
|
|
}
|
|
}
|
|
|
|
echo "All migrations have been run.\n";
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database migration failed: " . $e->getMessage() . "\n");
|
|
}
|
|
|