✨ feat(user): foundation for parallel-safe BDD isolation (T12 stage 1/2) (#34)
NewPostgresRepositoryFromDSN factory + BuildSchemaIsolatedDSN helper + integration test proving per-schema isolation works at repo level. Foundation for T12. Wiring into testserver is stage 2/2. Co-authored-by: Gabriel Radureau <arcodange@gmail.com> Co-committed-by: Gabriel Radureau <arcodange@gmail.com>
This commit was merged in pull request #34.
This commit is contained in:
@@ -125,6 +125,67 @@ func NewPostgresRepository(cfg *config.Config) (*PostgresRepository, error) {
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
// NewPostgresRepositoryFromDSN creates a PostgresRepository connected via the given DSN
|
||||
// and runs AutoMigrate against it. Used by BDD test infra to create a per-scenario
|
||||
// repository pointing at an isolated schema (the DSN typically includes search_path=<schema>).
|
||||
//
|
||||
// Pass the same cfg used elsewhere (it is required by methods that read pool settings),
|
||||
// but the DSN passed here OVERRIDES the host/port/dbname/etc that cfg would have built.
|
||||
func NewPostgresRepositoryFromDSN(cfg *config.Config, dsn string) (*PostgresRepository, error) {
|
||||
repo := &PostgresRepository{
|
||||
config: cfg,
|
||||
spanPrefix: "user.repo.",
|
||||
}
|
||||
|
||||
gormLogger := logger.New(
|
||||
log.New(os.Stderr, "\n", log.LstdFlags),
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second,
|
||||
LogLevel: logger.Warn,
|
||||
IgnoreRecordNotFoundError: true,
|
||||
Colorful: false,
|
||||
},
|
||||
)
|
||||
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: gormLogger})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to PostgreSQL with custom DSN: %w", err)
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get sql.DB from gorm: %w", err)
|
||||
}
|
||||
sqlDB.SetMaxOpenConns(cfg.GetDatabaseMaxOpenConns())
|
||||
sqlDB.SetMaxIdleConns(cfg.GetDatabaseMaxIdleConns())
|
||||
sqlDB.SetConnMaxLifetime(cfg.GetDatabaseConnMaxLifetime())
|
||||
|
||||
if err := db.AutoMigrate(&User{}); err != nil {
|
||||
return nil, fmt.Errorf("failed to auto-migrate via custom DSN: %w", err)
|
||||
}
|
||||
|
||||
repo.db = db
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
// BuildSchemaIsolatedDSN returns a Postgres DSN that targets the given schema via
|
||||
// the search_path connection parameter. Use with NewPostgresRepositoryFromDSN to
|
||||
// get a repository whose connection only sees the per-scenario schema.
|
||||
func BuildSchemaIsolatedDSN(cfg *config.Config, schemaName string) string {
|
||||
return fmt.Sprintf(
|
||||
"host=%s port=%d user=%s password=%s dbname=%s sslmode=%s search_path=%s",
|
||||
cfg.GetDatabaseHost(),
|
||||
cfg.GetDatabasePort(),
|
||||
cfg.GetDatabaseUser(),
|
||||
cfg.GetDatabasePassword(),
|
||||
cfg.GetDatabaseName(),
|
||||
cfg.GetDatabaseSSLMode(),
|
||||
schemaName,
|
||||
)
|
||||
}
|
||||
|
||||
// (Close already exists below; we reuse it.)
|
||||
|
||||
// initializeDatabase sets up the PostgreSQL database connection and runs migrations
|
||||
func (r *PostgresRepository) initializeDatabase() error {
|
||||
// Configure GORM logger based on config
|
||||
|
||||
Reference in New Issue
Block a user