39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
function run_migrations() {
|
|
$pdo = db();
|
|
$migrations_dir = __DIR__ . '/migrations';
|
|
|
|
// Ensure migrations table exists
|
|
$pdo->exec(file_get_contents($migrations_dir . '/000_create_migrations_table.sql'));
|
|
|
|
$ran_migrations_stmt = $pdo->query("SELECT migration FROM migrations");
|
|
$ran_migrations = $ran_migrations_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$migration_files = glob($migrations_dir . '/*.sql');
|
|
sort($migration_files);
|
|
|
|
foreach ($migration_files as $file) {
|
|
$migration_name = basename($file);
|
|
if ($migration_name === '000_create_migrations_table.sql') {
|
|
continue;
|
|
}
|
|
|
|
if (!in_array($migration_name, $ran_migrations)) {
|
|
echo "Running migration: $migration_name...\n";
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
|
|
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
|
|
$stmt->execute([$migration_name]);
|
|
echo "Success.\n";
|
|
} else {
|
|
echo "Migration already ran: $migration_name.\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
run_migrations();
|
|
|