Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Config struct {
TargetPath string `koanf:"target.path"`
SCCPath string `koanf:"scc.path"`
InsightsDatabase DBConfig `koanf:"database.insights"`
CMDatabase DBConfig `koanf:"database.cm"`
}

func getConfig(targetPath string) (Config, error) {
Expand Down Expand Up @@ -64,26 +63,5 @@ func getConfig(targetPath string) (Config, error) {
config.InsightsDatabase.ReadOnly = readOnlyStr == "true"
}

config.CMDatabase.User = os.Getenv("CROWD_DB_USERNAME")
config.CMDatabase.Password = os.Getenv("CROWD_DB_PASSWORD")
config.CMDatabase.DBName = os.Getenv("CROWD_DB_DATABASE")
config.CMDatabase.Host = os.Getenv("CROWD_DB_READ_HOST")
if portStr := os.Getenv("CROWD_DB_PORT"); portStr != "" {
if port, err := strconv.Atoi(portStr); err == nil {
config.CMDatabase.Port = port
}
}
config.CMDatabase.SSLMode = os.Getenv("CROWD_DB_SSLMODE")
if poolMaxStr := os.Getenv("CROWD_DB_POOL_MAX"); poolMaxStr != "" {
if poolMax, err := strconv.Atoi(poolMaxStr); err == nil {
config.CMDatabase.PoolMax = poolMax
}
} else {
config.CMDatabase.PoolMax = 10 // Default pool max
}
if readOnlyStr := os.Getenv("CROWD_DB_READONLY"); readOnlyStr != "" {
config.CMDatabase.ReadOnly = readOnlyStr == "true"
}

return config, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ type InsightsDB struct {
config DBConfig
}

// CMDB holds the connection pool and configuration for the CM database.
type CMDB struct {
pool *pgxpool.Pool
config DBConfig
}

// newDBConnection establishes a new database connection pool.
// This is a helper function used by NewInsightsDB and NewCMDB.
func newDBConnection(ctx context.Context, config DBConfig) (*pgxpool.Pool, error) {
Expand Down Expand Up @@ -81,32 +75,13 @@ func NewInsightsDB(ctx context.Context, config DBConfig) (*InsightsDB, error) {
}, nil
}

// NewCMDB creates a new CMDB instance.
func NewCMDB(ctx context.Context, config DBConfig) (*CMDB, error) {
pool, err := newDBConnection(ctx, config)
if err != nil {
return nil, err
}
return &CMDB{
pool: pool,
config: config,
}, nil
}

// Close closes the database connection pool.
func (db *InsightsDB) Close() {
if db.pool != nil {
db.pool.Close()
}
}

// Close closes the database connection pool.
func (db *CMDB) Close() {
if db.pool != nil {
db.pool.Close()
}
}

// saveProjectCost upserts the project cost into the database table.
func (db *InsightsDB) saveProjectCost(ctx context.Context, repository Repository, estimatedCost float64) error {
// From PostgreSQL 15 and newer, there's another way to do this: https://www.postgresql.org/docs/15/sql-merge.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func main() {
response := processRepository()
outputJSON(response)

// Always exit with code 0 - status details are in JSON response
}

Expand Down Expand Up @@ -63,18 +63,6 @@ func processRepository() StandardResponse {
}
defer insightsDb.Close()

cmdb, err := NewCMDB(ctx, config.CMDatabase)
if err != nil {
errorCode := ErrorCodeDatabaseConnection
errorMessage := fmt.Sprintf("Error connecting to CM database: %v", err)
return StandardResponse{
Status: StatusFailure,
ErrorCode: &errorCode,
ErrorMessage: &errorMessage,
}
}
defer cmdb.Close()

// Get git URL for the repository
gitUrl, err := getGitRepositoryURL(repoDir)
if err != nil {
Expand Down Expand Up @@ -297,4 +285,3 @@ func getErrorCodeFromSCCError(err error) string {
}
return ErrorCodeUnknown
}

Loading