50 lines
1.8 KiB
PHP
50 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_35607');
|
|
define('DB_USER', 'app_35607');
|
|
define('DB_PASS', '6ba26df8-c17d-4aa6-b713-e17374a6fbd9');
|
|
|
|
function db() {
|
|
static $pdo;
|
|
if (!$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 database doesn't exist, create it
|
|
if ($e->getCode() == 1049) {
|
|
try {
|
|
$tempPdo = new PDO('mysql:host='.DB_HOST, DB_USER, DB_PASS);
|
|
$tempPdo->exec('CREATE DATABASE IF NOT EXISTS '.DB_NAME.' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci');
|
|
$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 $e2) {
|
|
die('DB ERROR: Could not create database. ' . $e2->getMessage());
|
|
}
|
|
} else {
|
|
die('DB ERROR: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
return $pdo;
|
|
}
|
|
|
|
function run_migrations() {
|
|
$pdo = db();
|
|
$migration_files = glob(__DIR__ . '/migrations/*.sql');
|
|
foreach ($migration_files as $file) {
|
|
$sql = file_get_contents($file);
|
|
try {
|
|
$pdo->exec($sql);
|
|
} catch (PDOException $e) {
|
|
// You might want to log this error instead of dying
|
|
error_log('Migration failed for file ' . basename($file) . ': ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|