diff --git a/backend/cmd/debug_user/main.go b/backend/cmd/debug_user/main.go index 5f801e0..7842079 100644 --- a/backend/cmd/debug_user/main.go +++ b/backend/cmd/debug_user/main.go @@ -17,20 +17,12 @@ func main() { log.Println("Warning: Error loading .env file, relying on system env") } - host := os.Getenv("DB_HOST") - if host == "" { - // Fallback for debugging if env not picked up - host = "localhost" + dbURL := os.Getenv("DATABASE_URL") + if dbURL == "" { + log.Fatal("DATABASE_URL environment variable not set") } - port := os.Getenv("DB_PORT") - user := os.Getenv("DB_USER") - password := os.Getenv("DB_PASSWORD") - dbname := os.Getenv("DB_NAME") - connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", - host, port, user, password, dbname) - - db, err := sql.Open("postgres", connStr) + db, err := sql.Open("postgres", dbURL) if err != nil { log.Fatal(err) } diff --git a/backend/cmd/fixhash/main.go b/backend/cmd/fixhash/main.go index 7ad40c0..e47b399 100644 --- a/backend/cmd/fixhash/main.go +++ b/backend/cmd/fixhash/main.go @@ -3,13 +3,20 @@ package main import ( "database/sql" "fmt" + "log" + "os" _ "github.com/lib/pq" "golang.org/x/crypto/bcrypt" ) func main() { - db, err := sql.Open("postgres", "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require") + dbURL := os.Getenv("DATABASE_URL") + if dbURL == "" { + log.Fatal("DATABASE_URL environment variable not set") + } + + db, err := sql.Open("postgres", dbURL) if err != nil { fmt.Println("❌ Connection error:", err) return diff --git a/backend/cmd/inspect_schema/main.go b/backend/cmd/inspect_schema/main.go index 6a8cba5..90f9535 100644 --- a/backend/cmd/inspect_schema/main.go +++ b/backend/cmd/inspect_schema/main.go @@ -14,18 +14,7 @@ func main() { godotenv.Load(".env") dbURL := os.Getenv("DATABASE_URL") if dbURL == "" { - // Fallback - host := os.Getenv("DB_HOST") - port := os.Getenv("DB_PORT") - user := os.Getenv("DB_USER") - pass := os.Getenv("DB_PASSWORD") - name := os.Getenv("DB_NAME") - ssl := os.Getenv("DB_SSLMODE") - if host != "" { - dbURL = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", user, pass, host, port, name, ssl) - } else { - log.Fatal("DB URL not found") - } + log.Fatal("DATABASE_URL environment variable not set") } db, err := sql.Open("postgres", dbURL) diff --git a/backend/cmd/manual_migrate/main.go b/backend/cmd/manual_migrate/main.go index eafa9bf..c53e08a 100644 --- a/backend/cmd/manual_migrate/main.go +++ b/backend/cmd/manual_migrate/main.go @@ -24,18 +24,7 @@ func main() { dbURL := os.Getenv("DATABASE_URL") if dbURL == "" { - host := os.Getenv("DB_HOST") - port := os.Getenv("DB_PORT") - user := os.Getenv("DB_USER") - pass := os.Getenv("DB_PASSWORD") - name := os.Getenv("DB_NAME") - ssl := os.Getenv("DB_SSLMODE") - if host != "" { - dbURL = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", user, pass, host, port, name, ssl) - } else { - // Last resort - dbURL = "postgres://postgres:postgres@localhost:5432/gohorsejobs?sslmode=disable" - } + log.Fatal("DATABASE_URL environment variable not set") } db, err := sql.Open("postgres", dbURL) diff --git a/backend/internal/database/database.go b/backend/internal/database/database.go index 2fce50b..f01c98f 100755 --- a/backend/internal/database/database.go +++ b/backend/internal/database/database.go @@ -40,44 +40,12 @@ func InitDB() { } func BuildConnectionString() (string, error) { - // Prefer DATABASE_URL if set (standard format) if dbURL := os.Getenv("DATABASE_URL"); dbURL != "" { log.Println("Using DATABASE_URL for connection") return dbURL, nil } - // Fallback to individual params for backward compatibility - host := os.Getenv("DB_HOST") - if host == "" { - return "", fmt.Errorf("DATABASE_URL or DB_HOST environment variable not set") - } - user := os.Getenv("DB_USER") - if user == "" { - return "", fmt.Errorf("DB_USER environment variable not set") - } - password := os.Getenv("DB_PASSWORD") - if password == "" { - return "", fmt.Errorf("DB_PASSWORD environment variable not set") - } - dbname := os.Getenv("DB_NAME") - if dbname == "" { - return "", fmt.Errorf("DB_NAME environment variable not set") - } - port := os.Getenv("DB_PORT") - if port == "" { - port = "5432" - } - - sslmode := os.Getenv("DB_SSLMODE") - if sslmode == "" { - sslmode = "require" // Default to require for production security - } - - connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", - host, port, user, password, dbname, sslmode) - log.Println("Using individual DB_* params for connection") - return connStr, nil - + return "", fmt.Errorf("DATABASE_URL environment variable not set") } func RunMigrations() { diff --git a/backend/internal/database/database_test.go b/backend/internal/database/database_test.go index 5c96214..032174b 100644 --- a/backend/internal/database/database_test.go +++ b/backend/internal/database/database_test.go @@ -12,21 +12,9 @@ import ( func TestBuildConnectionString(t *testing.T) { // Backup env vars oldURL := os.Getenv("DATABASE_URL") - oldHost := os.Getenv("DB_HOST") - oldUser := os.Getenv("DB_USER") - oldPass := os.Getenv("DB_PASSWORD") - oldName := os.Getenv("DB_NAME") - oldPort := os.Getenv("DB_PORT") - oldSSL := os.Getenv("DB_SSLMODE") defer func() { os.Setenv("DATABASE_URL", oldURL) - os.Setenv("DB_HOST", oldHost) - os.Setenv("DB_USER", oldUser) - os.Setenv("DB_PASSWORD", oldPass) - os.Setenv("DB_NAME", oldName) - os.Setenv("DB_PORT", oldPort) - os.Setenv("DB_SSLMODE", oldSSL) }() // Case 1: DATABASE_URL @@ -39,29 +27,11 @@ func TestBuildConnectionString(t *testing.T) { t.Errorf("Mismatch URL") } - // Case 2: Individual Params + // Case 2: Missing DATABASE_URL os.Unsetenv("DATABASE_URL") - os.Setenv("DB_HOST", "localhost") - os.Setenv("DB_USER", "user") - os.Setenv("DB_PASSWORD", "pass") - os.Setenv("DB_NAME", "mydb") - os.Setenv("DB_PORT", "5432") - os.Setenv("DB_SSLMODE", "disable") - - s, err = database.BuildConnectionString() - if err != nil { - t.Errorf("Unexpected error: %v", err) - } - expected := "host=localhost port=5432 user=user password=pass dbname=mydb sslmode=disable" - if s != expected { - t.Errorf("Expected %s, got %s", expected, s) - } - - // Case 3: Missing Param - os.Unsetenv("DB_HOST") _, err = database.BuildConnectionString() if err == nil { - t.Error("Expected error for missing host") + t.Error("Expected error for missing DATABASE_URL") } } diff --git a/backend/tests/e2e/e2e_test.go b/backend/tests/e2e/e2e_test.go index 133934e..3c7ec6d 100644 --- a/backend/tests/e2e/e2e_test.go +++ b/backend/tests/e2e/e2e_test.go @@ -44,23 +44,8 @@ func TestMain(m *testing.M) { func setTestEnv() { // Only set if not already set (allows override via .env file) // These are defaults for when running tests without sourcing .env - if os.Getenv("DB_HOST") == "" { - os.Setenv("DB_HOST", "db-60059.dc-sp-1.absamcloud.com") - } - if os.Getenv("DB_USER") == "" { - os.Setenv("DB_USER", "yuki") - } - if os.Getenv("DB_PASSWORD") == "" { - os.Setenv("DB_PASSWORD", "xl1zfmr6e9bb") - } - if os.Getenv("DB_NAME") == "" { - os.Setenv("DB_NAME", "gohorsejobs_dev") - } - if os.Getenv("DB_PORT") == "" { - os.Setenv("DB_PORT", "26868") - } - if os.Getenv("DB_SSLMODE") == "" { - os.Setenv("DB_SSLMODE", "require") + if os.Getenv("DATABASE_URL") == "" { + os.Setenv("DATABASE_URL", "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require") } if os.Getenv("JWT_SECRET") == "" { os.Setenv("JWT_SECRET", "gohorse-super-secret-key-2024-production") diff --git a/backend/tests/integration/setup_test.go b/backend/tests/integration/setup_test.go index 17f6da0..7d7b415 100644 --- a/backend/tests/integration/setup_test.go +++ b/backend/tests/integration/setup_test.go @@ -31,23 +31,8 @@ func TestMain(m *testing.M) { } func setTestEnv() { - if os.Getenv("DB_HOST") == "" { - os.Setenv("DB_HOST", "db-60059.dc-sp-1.absamcloud.com") - } - if os.Getenv("DB_USER") == "" { - os.Setenv("DB_USER", "yuki") - } - if os.Getenv("DB_PASSWORD") == "" { - os.Setenv("DB_PASSWORD", "xl1zfmr6e9bb") - } - if os.Getenv("DB_NAME") == "" { - os.Setenv("DB_NAME", "gohorsejobs_dev") - } - if os.Getenv("DB_PORT") == "" { - os.Setenv("DB_PORT", "26868") - } - if os.Getenv("DB_SSLMODE") == "" { - os.Setenv("DB_SSLMODE", "require") + if os.Getenv("DATABASE_URL") == "" { + os.Setenv("DATABASE_URL", "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require") } if os.Getenv("JWT_SECRET") == "" { os.Setenv("JWT_SECRET", "gohorse-super-secret-key-2024-production") diff --git a/backend/tests/verify_login_test.go b/backend/tests/verify_login_test.go index cd8b2b9..1ecb5ff 100644 --- a/backend/tests/verify_login_test.go +++ b/backend/tests/verify_login_test.go @@ -26,7 +26,7 @@ func TestVerifyLogin(t *testing.T) { // Config dbURL := os.Getenv("DATABASE_URL") if dbURL == "" { - dbURL = "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require" + t.Skip("Skipping: DATABASE_URL not set") } // Updated to match deployed backend .env pepper := "some-random-string-for-password-hashing" @@ -82,7 +82,7 @@ func TestVerifyLoginNoPepper(t *testing.T) { dbURL := os.Getenv("DATABASE_URL") if dbURL == "" { - dbURL = "postgres://yuki:xl1zfmr6e9bb@db-60059.dc-sp-1.absamcloud.com:26868/gohorsejobs_dev?sslmode=require" + t.Skip("Skipping: DATABASE_URL not set") } password := "Admin@2025!"