35428-vm/db/config.php
Flatlogic Bot 2e3424ad5c v.2
2025-11-02 19:42:55 +00:00

55 lines
1.7 KiB
PHP

<?php
// Database Configuration for Supabase (PostgreSQL)
// IMPORTANT: Replace with your actual Supabase credentials.
// You can find these in your Supabase project's Database settings.
define('DB_HOST', 'YOUR_SUPABASE_HOST');
define('DB_PORT', '5432'); // Default PostgreSQL port
define('DB_NAME', 'postgres');
define('DB_USER', 'postgres');
define('DB_PASS', 'YOUR_SUPABASE_PASSWORD');
/**
* Establishes a PDO database connection.
* @return PDO
*/
function db() {
static $pdo;
if (!$pdo) {
$dsn = 'pgsql:host=' . DB_HOST . ';port=' . DB_PORT . ';dbname=' . DB_NAME;
try {
$pdo = new PDO($dsn, DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
} catch (PDOException $e) {
// In a real app, you'd log this error and show a generic message.
// For now, we'll just die and show the error.
die("Database connection failed: " . $e->getMessage());
}
}
return $pdo;
}
// A simple function to run migrations.
// In a real application, you would use a more robust migration tool.
function run_migrations() {
$pdo = db();
$migration_dir = __DIR__ . '/migrations';
$files = glob($migration_dir . '/*.sql');
foreach ($files as $file) {
try {
$sql = file_get_contents($file);
$pdo->exec($sql);
} catch (Exception $e) {
// Log error or handle it
error_log("Failed to run migration: $file. Error: " . $e->getMessage());
}
}
}
// Automatically run migrations when this file is included.
// This is for simplicity in this development environment.
if (DB_HOST !== 'YOUR_SUPABASE_HOST') { // Don't run if not configured
run_migrations();
}