svalavuo пре 1 дан
родитељ
комит
14df689852
3 измењених фајлова са 64 додато и 18 уклоњено
  1. 24 6
      backend/config/database.php
  2. 8 12
      build.sh
  3. 32 0
      test-env.php

+ 24 - 6
backend/config/database.php

@@ -7,7 +7,8 @@ class Database {
     public $conn;
 
     public function __construct() {
-        // Load local environment file for development
+        // In Docker, environment variables are passed directly
+        // For local development, try to load .env.local file
         $envFile = __DIR__ . '/../.env.local';
         if (file_exists($envFile)) {
             $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
@@ -19,23 +20,40 @@ class Database {
             }
         }
         
-        $this->host = getenv('DB_HOST') ?: 'localhost';
-        $this->db_name = getenv('DB_NAME') ?: 'inventory_db';
-        $this->username = getenv('DB_USER') ?: 'root';
-        $this->password = getenv('DB_PASS') ?: '';
+        // Get database configuration from environment variables
+        $this->host = getenv('DB_HOST');
+        $this->db_name = getenv('DB_NAME');
+        $this->username = getenv('DB_USER');
+        $this->password = getenv('DB_PASS');
+        
+        // Debug: Log environment variables (remove in production)
+        error_log("Database config - Host: " . ($this->host ?: 'NOT SET') . 
+                 ", DB: " . ($this->db_name ?: 'NOT SET') . 
+                 ", User: " . ($this->username ?: 'NOT SET'));
     }
 
     public function getConnection() {
         $this->conn = null;
 
+        // Validate required database configuration
+        if (empty($this->host) || empty($this->db_name) || empty($this->username)) {
+            error_log("Database configuration missing: Host=" . ($this->host ?: 'MISSING') . 
+                     ", DB=" . ($this->db_name ?: 'MISSING') . 
+                     ", User=" . ($this->username ?: 'MISSING'));
+            throw new Exception("Database configuration is incomplete. Please check environment variables.");
+        }
+
         try {
             $dsn = "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4";
+            error_log("Attempting database connection: $dsn with user " . $this->username);
             $this->conn = new PDO($dsn, $this->username, $this->password);
             $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             $this->conn->exec("set names utf8");
+            error_log("Database connection successful");
         } catch(PDOException $exception) {
             error_log("Database connection error: " . $exception->getMessage());
-            throw new Exception("Database connection failed");
+            error_log("Connection details - Host: " . $this->host . ", DB: " . $this->db_name . ", User: " . $this->username);
+            throw new Exception("Database connection failed: " . $exception->getMessage());
         }
 
         return $this->conn;

+ 8 - 12
build.sh

@@ -61,23 +61,19 @@ echo "   🗄️  Database User: $DB_USER"
 echo "   🔴 Redis Port: ${REDIS_PORT:-6379}"
 echo ""
 
-# Test database connectivity
-echo "🔍 Testing database connectivity..."
-if ! docker-compose run --rm inventory-app php -r "
-try {
-    \$pdo = new PDO('mysql:host=$DB_HOST;dbname=$DB_NAME', '$DB_USER', '$DB_PASS');
-    echo '✅ Database connection successful';
-} catch (Exception \$e) {
-    echo '❌ Database connection failed: ' . \$e->getMessage();
-    exit(1);
-}
-" 2>/dev/null; then
-    echo "❌ Database connection test failed. Please check your database configuration and connectivity."
+# Test environment variables and database connectivity
+echo "🔍 Testing environment variables and database connectivity..."
+if ! docker-compose run --rm inventory-app php test-env.php; then
+    echo "❌ Environment variables or database connection test failed."
     echo "💡 Make sure:"
+    echo "   - Environment variables are properly set in .env"
     echo "   - Database server is running and accessible"
     echo "   - Database '$DB_NAME' exists"
     echo "   - User '$DB_USER' has proper permissions"
     echo "   - Network allows connection from Docker container"
+    echo ""
+    echo "🔍 Debug: Check container logs for detailed error information"
+    docker-compose logs inventory-app | tail -20
     exit 1
 fi
 

+ 32 - 0
test-env.php

@@ -0,0 +1,32 @@
+<?php
+// Test script to verify environment variables are loaded in container
+echo "=== Environment Variables Test ===\n";
+echo "DB_HOST: " . (getenv('DB_HOST') ?: 'NOT SET') . "\n";
+echo "DB_NAME: " . (getenv('DB_NAME') ?: 'NOT SET') . "\n";
+echo "DB_USER: " . (getenv('DB_USER') ?: 'NOT SET') . "\n";
+echo "DB_PASS: " . (getenv('DB_PASS') ? 'SET' : 'NOT SET') . "\n";
+echo "FRONTEND_PORT: " . (getenv('FRONTEND_PORT') ?: 'NOT SET') . "\n";
+echo "COMPANY_NAME: " . (getenv('COMPANY_NAME') ?: 'NOT SET') . "\n";
+
+echo "\n=== Database Connection Test ===\n";
+try {
+    require_once 'backend/config/database.php';
+    $db = new Database();
+    $conn = $db->getConnection();
+    echo "✅ Database connection successful!\n";
+    
+    // Test a simple query
+    $stmt = $conn->query("SELECT COUNT(*) as count FROM information_schema.tables WHERE table_schema = '" . getenv('DB_NAME') . "'");
+    $result = $stmt->fetch(PDO::FETCH_ASSOC);
+    echo "📊 Database contains " . $result['count'] . " tables\n";
+    
+} catch (Exception $e) {
+    echo "❌ Database connection failed: " . $e->getMessage() . "\n";
+}
+
+echo "\n=== PHP Environment ===\n";
+echo "PHP Version: " . phpversion() . "\n";
+echo "Current Working Directory: " . getcwd() . "\n";
+echo "Environment file exists (.env.local): " . (file_exists('.env.local') ? 'YES' : 'NO') . "\n";
+echo "Environment file exists (.env): " . (file_exists('.env') ? 'YES' : 'NO') . "\n";
+?>