34000-vm/db/migrate.php
Flatlogic Bot 8d45670c2f Ver 1
2025-09-10 20:32:30 +00:00

43 lines
1.4 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
try {
$pdo = db();
// 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
$stmt = $pdo->query('SELECT migration FROM migrations');
$executedMigrations = $stmt ? $stmt->fetchAll(PDO::FETCH_COLUMN) : [];
// Find all migration files
$migrationFiles = glob(__DIR__ . '/migrations/*.sql') ?: [];
sort($migrationFiles);
$migrationsRun = false;
// Run pending migrations
foreach ($migrationFiles as $migrationFile) {
$migrationName = basename($migrationFile);
if (!in_array($migrationName, $executedMigrations)) {
$sql = file_get_contents($migrationFile);
if (!empty(trim($sql))) {
$pdo->exec($sql);
// Log the migration
$stmt = $pdo->prepare('INSERT INTO migrations (migration) VALUES (?)');
$stmt->execute([$migrationName]);
echo "Migration from $migrationName ran successfully.\n";
$migrationsRun = true;
}
}
}
if (!$migrationsRun) {
echo "All migrations are up to date.\n";
}
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}