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()); } } }