44 lines
1.1 KiB
PHP
44 lines
1.1 KiB
PHP
<?php
|
|
function db_connect() {
|
|
static $pdoconn;
|
|
|
|
if ($pdoconn) {
|
|
return $pdoconn;
|
|
}
|
|
|
|
$config = [
|
|
'host' => '127.0.0.1',
|
|
'dbname' => 'app',
|
|
'user' => 'app',
|
|
'password' => 'app'
|
|
];
|
|
|
|
$dsn = "mysql:host={$config['host']};dbname={$config['dbname']};charset=utf8mb4";
|
|
$options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
];
|
|
|
|
try {
|
|
$pdoconn = new PDO($dsn, $config['user'], $config['password'], $options);
|
|
return $pdoconn;
|
|
} catch (PDOException $e) {
|
|
// In a real app, you'd log this error and show a generic message
|
|
throw new PDOException($e->getMessage(), (int)$e->getCode());
|
|
}
|
|
}
|
|
|
|
function run_migrations() {
|
|
$pdo = db_connect();
|
|
$migrationsDir = __DIR__ . '/migrations';
|
|
$files = glob($migrationsDir . '/*.sql');
|
|
sort($files);
|
|
foreach ($files as $file) {
|
|
$sql = file_get_contents($file);
|
|
if ($sql) {
|
|
$pdo->exec($sql);
|
|
}
|
|
}
|
|
}
|