MCPcopy Create free account
hub / github.com/encoder-run/operator / GetPostgresClient

Function GetPostgresClient

pkg/database/database.go:21–52  ·  view source on GitHub ↗

GetPostgresClient initializes or returns a database connection for a given DSN

(dsn string)

Source from the content-addressed store, hash-verified

19
20// GetPostgresClient initializes or returns a database connection for a given DSN
21func GetPostgresClient(dsn string) (*gorm.DB, error) {
22 mu.Lock()
23 defer mu.Unlock()
24
25 // Check if an instance already exists for this DSN
26 if db, exists := instances[dsn]; exists {
27 return db, nil
28 }
29
30 // Create a new instance since one doesn't exist
31 db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
32 Logger: logger.Default.LogMode(logger.Info), // Set log mode to `Info` to log all SQL queries
33 })
34 if err != nil {
35 log.Printf("failed to connect to database: %v", err)
36 return nil, err
37 }
38
39 // Execute initial setup queries
40 if result := db.Exec("CREATE EXTENSION IF NOT EXISTS vector"); result.Error != nil {
41 log.Printf("Failed to execute initial setup query: %v", result.Error)
42 return nil, result.Error
43 }
44
45 // Auto-migrate the schema
46 db.AutoMigrate(&Object{}, &Reference{}, &Config{}, &Shallow{}, &Index{}, &CodeEmbedding{})
47
48 // Store the instance in the map
49 instances[dsn] = db
50
51 return db, nil
52}
53
54// ConstructPostgresDSN constructs the Data Source Name for a PostgreSQL connection
55func ConstructPostgresDSN(host, user, password, dbname, port, sslmode, timezone string) string {

Callers 3

semanticSearchPostgresFunction · 0.92
AddFunction · 0.92
postgresStorageStorerFunction · 0.92

Calls 1

ExecMethod · 0.80

Tested by

no test coverage detected