MCPcopy Index your code
hub / github.com/coder/coder / NewDB

Function NewDB

coderd/database/dbtestutil/db.go:92–146  ·  view source on GitHub ↗
(t testing.TB, opts ...Option)

Source from the content-addressed store, hash-verified

90}
91
92func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
93 t.Helper()
94
95 o := options{logger: testutil.Logger(t).Named("pubsub")}
96 for _, opt := range opts {
97 opt(&o)
98 }
99
100 var db database.Store
101 var ps pubsub.Pubsub
102
103 connectionURL := os.Getenv("CODER_PG_CONNECTION_URL")
104 if connectionURL == "" && o.url != "" {
105 connectionURL = o.url
106 }
107 if connectionURL == "" {
108 var err error
109 connectionURL, err = Open(t)
110 require.NoError(t, err)
111 }
112
113 if o.fixedTimezone == "" {
114 // To make sure we find timezone-related issues, we set the timezone
115 // of the database to a non-UTC one.
116 // The below was picked due to the following properties:
117 // - It has a non-UTC offset
118 // - It has a fractional hour UTC offset
119 // - It includes a daylight savings time component
120 o.fixedTimezone = DefaultTimezone
121 }
122 dbName := dbNameFromConnectionURL(t, connectionURL)
123 setDBTimezone(t, connectionURL, dbName, o.fixedTimezone)
124
125 sqlDB, err := sql.Open("postgres", connectionURL)
126 require.NoError(t, err)
127 t.Cleanup(func() {
128 _ = sqlDB.Close()
129 })
130 if o.returnSQLDB != nil {
131 o.returnSQLDB(sqlDB)
132 }
133 if o.dumpOnFailure {
134 t.Cleanup(func() { DumpOnFailure(t, connectionURL) })
135 }
136 // Unit tests should not retry serial transaction failures.
137 db = database.New(sqlDB, database.WithSerialRetryCount(1))
138
139 ps, err = pubsub.New(context.Background(), o.logger, sqlDB, connectionURL)
140 require.NoError(t, err)
141 t.Cleanup(func() {
142 _ = ps.Close()
143 })
144
145 return db, ps
146}
147
148// setRandDBTimezone sets the timezone of the database to the given timezone.
149// Note that the updated timezone only comes into effect on reconnect, so we

Calls 13

CloseMethod · 0.95
LoggerFunction · 0.92
NewFunction · 0.92
WithSerialRetryCountFunction · 0.92
NewFunction · 0.92
OpenFunction · 0.85
dbNameFromConnectionURLFunction · 0.85
setDBTimezoneFunction · 0.85
DumpOnFailureFunction · 0.85
NamedMethod · 0.80
HelperMethod · 0.65
CleanupMethod · 0.65