From 5bd0ea9bce3660852d2c25397c00a6a530fe38f7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 02:14:18 +0000 Subject: [PATCH 1/2] Initial plan From ea7cc7d40d3c4a8982e8c767488973eb198e4540 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 02:17:24 +0000 Subject: [PATCH 2/2] Add DB config validation to check for required username on postgres/mysql Co-authored-by: slhmy <31381093+slhmy@users.noreply.github.com> --- .../infrastructure/persistence/gorm/db.go | 15 ++++ .../persistence/gorm/db_test.go | 81 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 internal/infrastructure/persistence/gorm/db_test.go diff --git a/internal/infrastructure/persistence/gorm/db.go b/internal/infrastructure/persistence/gorm/db.go index 8e4d92c..44f23ca 100644 --- a/internal/infrastructure/persistence/gorm/db.go +++ b/internal/infrastructure/persistence/gorm/db.go @@ -23,8 +23,23 @@ type Config struct { SSLMode string } +// Validate checks that the configuration is valid for the specified driver. +// For postgres and mysql drivers, username must not be empty. +func (c Config) Validate() error { + switch c.Driver { + case "postgres", "mysql": + if c.Username == "" { + return fmt.Errorf("username is required for %s driver", c.Driver) + } + } + return nil +} + // NewDB creates a new GORM database connection based on the configuration. func NewDB(cfg Config) *gorm.DB { + if err := cfg.Validate(); err != nil { + panic(err) + } driver := cfg.Driver switch driver { case "postgres": diff --git a/internal/infrastructure/persistence/gorm/db_test.go b/internal/infrastructure/persistence/gorm/db_test.go new file mode 100644 index 0000000..2c29abc --- /dev/null +++ b/internal/infrastructure/persistence/gorm/db_test.go @@ -0,0 +1,81 @@ +package gorm + +import ( + "testing" +) + +func TestConfigValidate(t *testing.T) { + tests := []struct { + name string + config Config + wantErr bool + }{ + { + name: "postgres with username", + config: Config{ + Driver: "postgres", + Host: "localhost", + Port: 5432, + Username: "testuser", + Password: "testpass", + DbName: "testdb", + SSLMode: "disable", + }, + wantErr: false, + }, + { + name: "postgres without username", + config: Config{ + Driver: "postgres", + Host: "localhost", + Port: 5432, + Username: "", + Password: "testpass", + DbName: "testdb", + SSLMode: "disable", + }, + wantErr: true, + }, + { + name: "mysql with username", + config: Config{ + Driver: "mysql", + Host: "localhost", + Port: 3306, + Username: "testuser", + Password: "testpass", + DbName: "testdb", + }, + wantErr: false, + }, + { + name: "mysql without username", + config: Config{ + Driver: "mysql", + Host: "localhost", + Port: 3306, + Username: "", + Password: "testpass", + DbName: "testdb", + }, + wantErr: true, + }, + { + name: "sqlite without username (allowed)", + config: Config{ + Driver: "sqlite", + DbName: "test.db", + }, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.config.Validate() + if (err != nil) != tt.wantErr { + t.Errorf("Config.Validate() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}