@@ -8,11 +8,11 @@ import (
8
8
"sync"
9
9
"time"
10
10
11
+ "github.com/google/uuid"
11
12
"github.com/lib/pq"
12
13
"golang.org/x/xerrors"
13
14
14
15
"github.com/coder/coder/v2/cryptorand"
15
- "github.com/google/uuid"
16
16
)
17
17
18
18
const CoderTestingDBName = "coder_testing"
@@ -126,7 +126,10 @@ func (b *Broker) init(t TBSubset) error {
126
126
var pqErr * pq.Error
127
127
if xerrors .As (err ,& pqErr )&& pqErr .Code == "3D000" {
128
128
// database does not exist.
129
- err = b .createCoderTestingDB (t )
129
+ if closeErr := coderTestingDB .Close ();closeErr != nil {
130
+ return xerrors .Errorf ("close postgres connection: %w" ,closeErr )
131
+ }
132
+ err = createCoderTestingDB (t )
130
133
if err != nil {
131
134
return xerrors .Errorf ("create coder testing db: %w" ,err )
132
135
}
@@ -135,14 +138,15 @@ func (b *Broker) init(t TBSubset) error {
135
138
return xerrors .Errorf ("open postgres connection: %w" ,err )
136
139
}
137
140
}else if err != nil {
141
+ _ = coderTestingDB .Close ()
138
142
return xerrors .Errorf ("ping '%s' database: %w" ,CoderTestingDBName ,err )
139
143
}
140
144
b .coderTestingDB = coderTestingDB
141
145
b .uuid = uuid .New ()
142
146
return nil
143
147
}
144
148
145
- func ( b * Broker ) createCoderTestingDB (t TBSubset )error {
149
+ func createCoderTestingDB (t TBSubset )error {
146
150
db ,err := sql .Open ("postgres" ,defaultConnectionParams .DSN ())
147
151
if err != nil {
148
152
return xerrors .Errorf ("open postgres connection: %w" ,err )