45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
// Generated by setup_mariadb_project.sh — edit as needed.
|
|
define('DB_HOST', '127.0.0.1');
|
|
define('DB_NAME', 'app_31009');
|
|
define('DB_USER', 'app_31009');
|
|
define('DB_PASS', '2c66b530-2a65-423a-a106-6760b49ad1a2');
|
|
|
|
function db() {
|
|
static $pdo;
|
|
if ($pdo) {
|
|
return $pdo;
|
|
}
|
|
|
|
try {
|
|
$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,
|
|
]);
|
|
} catch (PDOException $e) {
|
|
// If the database doesn't exist, we can't run migrations.
|
|
// The error will be caught and displayed on the page.
|
|
throw $e;
|
|
}
|
|
|
|
|
|
// Migration logic
|
|
$pdo->exec('CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY)');
|
|
$ran_migrations = $pdo->query('SELECT migration FROM migrations')->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
$migration_files = glob(__DIR__ . '/migrations/*.sql');
|
|
sort($migration_files);
|
|
|
|
foreach ($migration_files as $file) {
|
|
$migration_name = basename($file);
|
|
if (!in_array($migration_name, $ran_migrations)) {
|
|
$sql = file_get_contents($file);
|
|
$pdo->exec($sql);
|
|
$stmt = $pdo->prepare('INSERT INTO migrations (migration) VALUES (?)');
|
|
$stmt->execute([$migration_name]);
|
|
}
|
|
}
|
|
|
|
return $pdo;
|
|
}
|