33 lines
942 B
PHP
33 lines
942 B
PHP
<?php
|
|
require_once 'src/db.php';
|
|
|
|
// Ensure migrations table exists
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
migration VARCHAR(255) NOT NULL UNIQUE,
|
|
ran_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
// Get already run migrations
|
|
$stmt = $pdo->query("SELECT migration FROM migrations");
|
|
$ranMigrations = $stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// Scan migrations directory
|
|
$migrationFiles = glob('migrations/*.php');
|
|
sort($migrationFiles);
|
|
|
|
foreach ($migrationFiles as $migrationFile) {
|
|
$migrationName = basename($migrationFile);
|
|
|
|
if (!in_array($migrationName, $ranMigrations)) {
|
|
require_once $migrationFile;
|
|
|
|
// Record migration as run
|
|
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
|
|
$stmt->execute([$migrationName]);
|
|
|
|
echo "Ran migration: $migrationName\n";
|
|
}
|
|
}
|
|
|
|
echo "Migrations complete."; |