36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
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)');
|
|
|
|
// 2. Get all executed migrations
|
|
$executedMigrations = $pdo->query('SELECT migration FROM migrations')->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// 3. Find all migration files
|
|
$migrationFiles = glob(__DIR__ . '/migrations/*.sql');
|
|
|
|
// 4. Determine which migrations to run
|
|
foreach ($migrationFiles as $file) {
|
|
$migrationName = basename($file);
|
|
|
|
if (!in_array($migrationName, $executedMigrations)) {
|
|
// 5. Execute the migration
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
|
|
// 6. Record the migration
|
|
$stmt = $pdo->prepare('INSERT INTO migrations (migration) VALUES (?)');
|
|
$stmt->execute([$migrationName]);
|
|
|
|
echo "Migration from $migrationName executed successfully.\n";
|
|
}
|
|
}
|
|
echo "All new migrations have been executed.";
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database migration failed: " . $e->getMessage());
|
|
} |