MCPcopy Create free account
hub / github.com/cortexproject/cortex / New

Function New

pkg/configs/db/postgres/postgres.go:67–102  ·  view source on GitHub ↗

New creates a new postgres DB

(uri, migrationsDir string)

Source from the content-addressed store, hash-verified

65
66// New creates a new postgres DB
67func New(uri, migrationsDir string) (DB, error) {
68 db, err := sql.Open("postgres", uri)
69 if err != nil {
70 return DB{}, errors.Wrap(err, "cannot open postgres db")
71 }
72
73 if err := dbWait(db); err != nil {
74 return DB{}, errors.Wrap(err, "cannot establish db connection")
75 }
76
77 if migrationsDir != "" {
78 // Add file scheme if no scheme is present
79 if !strings.HasPrefix(migrationsDir, "file:") {
80 migrationsDir = "file:" + migrationsDir
81 }
82
83 m, err := migrate.New(migrationsDir, uri)
84 if err != nil {
85 return DB{}, errors.Wrap(err, "database migrations initialization failed")
86 }
87
88 level.Info(util_log.Logger).Log("msg", "running database migrations...")
89
90 if err := m.Up(); err != nil {
91 if err != migrate.ErrNoChange {
92 return DB{}, errors.Wrap(err, "database migrations failed")
93 }
94 level.Debug(util_log.Logger).Log("msg", "no change in schema, error (ignored)", "err", err)
95 }
96 }
97
98 return DB{
99 dbProxy: db,
100 StatementBuilderType: statementBuilder(db),
101 }, err
102}
103
104var statementBuilder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).RunWith
105

Callers 2

NewFunction · 0.92
SetupFunction · 0.92

Calls 3

dbWaitFunction · 0.85
WrapMethod · 0.65
LogMethod · 0.45

Tested by

no test coverage detected