35908-vm/db/config.php
2025-11-21 18:15:18 +00:00

49 lines
1.6 KiB
PHP

<?php
// Generated by setup_mariadb_project.sh — edit as needed.
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'app_35908');
define('DB_USER', 'app_35908');
define('DB_PASS', '490da4bb-3eb2-465e-a8c9-7894b8c5cb06');
function db() {
static $pdo;
if (!$pdo) {
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
}
return $pdo;
}
function run_migrations() {
$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 UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
// Get all run migrations
$run_migrations_stmt = $pdo->query("SELECT migration FROM migrations");
$run_migrations = $run_migrations_stmt->fetchAll(PDO::FETCH_COLUMN);
// Get all migration files
$migration_files = glob(__DIR__ . '/migrations/*.sql');
foreach ($migration_files as $file) {
$migration_name = basename($file);
if (!in_array($migration_name, $run_migrations)) {
$sql = file_get_contents($file);
$pdo->exec($sql);
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$stmt->execute([$migration_name]);
}
}
}
// Run migrations on every request for simplicity in this context.
// For production, this should be a separate script.
run_migrations();