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