56 lines
1.8 KiB
PHP
56 lines
1.8 KiB
PHP
<?php
|
|
// Generated by setup_mariadb_project.sh — edit as needed.
|
|
define('DB_HOST', '127.0.0.1');
|
|
define('DB_NAME', 'app_36694');
|
|
define('DB_USER', 'app_36694');
|
|
define('DB_PASS', 'c0ffff39-e90d-4b28-81d8-31752766da7b');
|
|
|
|
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();
|
|
$migrations_dir = __DIR__ . '/migrations';
|
|
|
|
// 1. Create migrations table if it doesn't exist (handle the very first run)
|
|
try {
|
|
$pdo->query('SELECT 1 FROM migrations LIMIT 1');
|
|
} catch (PDOException $e) {
|
|
$pdo->exec("CREATE TABLE migrations (id INT AUTO_INCREMENT PRIMARY KEY, migration_name VARCHAR(255) NOT NULL UNIQUE, executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);");
|
|
}
|
|
|
|
// 2. Get all executed migration names
|
|
$executed_migrations_stmt = $pdo->query('SELECT migration_name FROM migrations');
|
|
$executed_migrations = $executed_migrations_stmt->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
// 3. Find and run new migration files
|
|
if (is_dir($migrations_dir)) {
|
|
$files = glob($migrations_dir . '/*.sql');
|
|
sort($files); // Ensure they run in order
|
|
|
|
foreach ($files as $file) {
|
|
$migration_name = basename($file);
|
|
|
|
if (!in_array($migration_name, $executed_migrations)) {
|
|
$sql = file_get_contents($file);
|
|
if ($sql) {
|
|
$pdo->exec($sql);
|
|
$insert_stmt = $pdo->prepare('INSERT INTO migrations (migration_name) VALUES (?)');
|
|
$insert_stmt->execute([$migration_name]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
run_migrations();
|
|
|