44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
function run_migrations() {
|
|
$pdo = db();
|
|
|
|
// Create migrations table if it doesn't exist
|
|
try {
|
|
$pdo->exec('CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY)');
|
|
} catch (PDOException $e) {
|
|
die("Could not create migrations table: " . $e->getMessage() . "\n");
|
|
}
|
|
|
|
// Get all migration files
|
|
$migrations_dir = __DIR__ . '/migrations';
|
|
$files = glob($migrations_dir . '/*.sql');
|
|
|
|
// Get already run migrations
|
|
$ran_migrations_stmt = $pdo->query('SELECT migration FROM migrations');
|
|
$ran_migrations = $ran_migrations_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
foreach ($files as $file) {
|
|
$migration_name = basename($file);
|
|
|
|
if (in_array($migration_name, $ran_migrations)) {
|
|
continue; // Skip already run migration
|
|
}
|
|
|
|
$sql = file_get_contents($file);
|
|
try {
|
|
$pdo->exec($sql);
|
|
$stmt = $pdo->prepare('INSERT INTO migrations (migration) VALUES (?)');
|
|
$stmt->execute([$migration_name]);
|
|
echo "Migration from $file ran successfully.\n";
|
|
} catch (PDOException $e) {
|
|
die("Migration failed on file $file: " . $e->getMessage() . "\n");
|
|
}
|
|
}
|
|
echo "All new migrations have been run.\n";
|
|
}
|
|
|
|
run_migrations();
|
|
|