41 lines
1.4 KiB
PHP
41 lines
1.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
echo "Starting migration process...\n";
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// 1. Create migrations table if it doesn't exist
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
|
|
|
|
// 2. Get all migrations that have been run
|
|
$ran_migrations_stmt = $pdo->query("SELECT migration FROM migrations");
|
|
$ran_migrations = $ran_migrations_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// 3. Get all migration files
|
|
$migration_files = glob(__DIR__ . '/migrations/*.sql');
|
|
sort($migration_files);
|
|
|
|
$migrations_to_run = array_filter($migration_files, function($file) use ($ran_migrations) {
|
|
return !in_array(basename($file), $ran_migrations);
|
|
});
|
|
|
|
if (empty($migrations_to_run)) {
|
|
echo "No new migrations to run.\n";
|
|
} else {
|
|
foreach ($migrations_to_run as $file) {
|
|
echo "Running migration: " . basename($file) . "...\n";
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
|
|
// 4. Record the migration
|
|
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
|
|
$stmt->execute([basename($file)]);
|
|
echo "Migration successful: " . basename($file) . "\n";
|
|
}
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
die("Migration failed: " . $e->getMessage() . "\n");
|
|
} |