35583-vm/db/migrate.php
Flatlogic Bot 58e3bb7010 1.2
2025-11-09 03:37:51 +00:00

38 lines
1.3 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 (migration VARCHAR(255) NOT NULL, PRIMARY KEY (migration))");
// Get all migration files
$migration_files = glob(__DIR__ . '/*.sql');
// Get executed migrations
$executed_migrations = $pdo->query("SELECT migration FROM migrations")->fetchAll(PDO::FETCH_COLUMN);
foreach ($migration_files as $file) {
$migration_name = basename($file);
if (!in_array($migration_name, $executed_migrations)) {
$sql = file_get_contents($file);
$pdo->exec($sql);
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$stmt->execute([$migration_name]);
echo "Executed migration: $migration_name\n";
} else {
echo "Skipping already executed migration: $migration_name\n";
}
}
echo "Database migrations completed successfully.\n";
// Seed the database with default data
echo "Seeding database...\n";
require_once __DIR__ . '/seed.php';
echo "Database seeding completed.\n";
} catch (PDOException $e) {
die("Database migration failed: " . $e->getMessage() . "\n");
}