35861-vm/db/migrations.php
Flatlogic Bot 1b14267623 v4
2025-11-19 22:24:36 +00:00

40 lines
1.2 KiB
PHP

<?php
require_once 'config.php';
try {
$pdo = db();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create migrations table if it doesn't exist
$pdo->exec("CREATE TABLE IF NOT EXISTS migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
// Get all executed migrations
$executedMigrations = $pdo->query("SELECT migration FROM migrations")->fetchAll(PDO::FETCH_COLUMN);
$migrationFiles = glob(__DIR__ . '/migrations/*.sql');
sort($migrationFiles);
foreach ($migrationFiles as $file) {
$migrationName = basename($file);
if (!in_array($migrationName, $executedMigrations)) {
$sql = file_get_contents($file);
$pdo->exec($sql);
// Record the migration
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$stmt->execute([$migrationName]);
echo "Migration successful: {$migrationName}" . PHP_EOL;
}
}
echo "All migrations are up to date." . PHP_EOL;
} catch (PDOException $e) {
die("Migration failed: " . $e->getMessage());
}