38 lines
1.3 KiB
PHP
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");
|
|
} |