@@ -37,7 +37,7 @@ type Config struct {
37
37
38
38
type Postgres struct {
39
39
// Locking and unlocking need to use the same connection
40
- db * sql.Conn
40
+ conn * sql.Conn
41
41
isLocked bool
42
42
43
43
// Open and WithInstance need to garantuee that config is never nil
@@ -76,7 +76,7 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
76
76
}
77
77
78
78
px := & Postgres {
79
- db : conn ,
79
+ conn : conn ,
80
80
config : config ,
81
81
}
82
82
@@ -115,7 +115,7 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
115
115
}
116
116
117
117
func (p * Postgres ) Close () error {
118
- return p .db .Close ()
118
+ return p .conn .Close ()
119
119
}
120
120
121
121
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS
@@ -133,7 +133,7 @@ func (p *Postgres) Lock() error {
133
133
// or return false if the lock cannot be acquired immediately.
134
134
query := `SELECT pg_try_advisory_lock($1)`
135
135
var success bool
136
- if err := p .db .QueryRowContext (context .Background (), query , aid ).Scan (& success ); err != nil {
136
+ if err := p .conn .QueryRowContext (context .Background (), query , aid ).Scan (& success ); err != nil {
137
137
return & database.Error {OrigErr : err , Err : "try lock failed" , Query : []byte (query )}
138
138
}
139
139
@@ -156,7 +156,7 @@ func (p *Postgres) Unlock() error {
156
156
}
157
157
158
158
query := `SELECT pg_advisory_unlock($1)`
159
- if _ , err := p .db .ExecContext (context .Background (), query , aid ); err != nil {
159
+ if _ , err := p .conn .ExecContext (context .Background (), query , aid ); err != nil {
160
160
return & database.Error {OrigErr : err , Query : []byte (query )}
161
161
}
162
162
p .isLocked = false
@@ -171,7 +171,7 @@ func (p *Postgres) Run(migration io.Reader) error {
171
171
172
172
// run migration
173
173
query := string (migr [:])
174
- if _ , err := p .db .ExecContext (context .Background (), query ); err != nil {
174
+ if _ , err := p .conn .ExecContext (context .Background (), query ); err != nil {
175
175
// TODO: cast to postgress error and get line number
176
176
return database.Error {OrigErr : err , Err : "migration failed" , Query : migr }
177
177
}
@@ -180,7 +180,7 @@ func (p *Postgres) Run(migration io.Reader) error {
180
180
}
181
181
182
182
func (p * Postgres ) SetVersion (version int , dirty bool ) error {
183
- tx , err := p .db .BeginTx (context .Background (), & sql.TxOptions {})
183
+ tx , err := p .conn .BeginTx (context .Background (), & sql.TxOptions {})
184
184
if err != nil {
185
185
return & database.Error {OrigErr : err , Err : "transaction start failed" }
186
186
}
@@ -208,7 +208,7 @@ func (p *Postgres) SetVersion(version int, dirty bool) error {
208
208
209
209
func (p * Postgres ) Version () (version int , dirty bool , err error ) {
210
210
query := `SELECT version, dirty FROM "` + p .config .MigrationsTable + `" LIMIT 1`
211
- err = p .db .QueryRowContext (context .Background (), query ).Scan (& version , & dirty )
211
+ err = p .conn .QueryRowContext (context .Background (), query ).Scan (& version , & dirty )
212
212
switch {
213
213
case err == sql .ErrNoRows :
214
214
return database .NilVersion , false , nil
@@ -229,7 +229,7 @@ func (p *Postgres) Version() (version int, dirty bool, err error) {
229
229
func (p * Postgres ) Drop () error {
230
230
// select all tables in current schema
231
231
query := `SELECT table_name FROM information_schema.tables WHERE table_schema=(SELECT current_schema())`
232
- tables , err := p .db .QueryContext (context .Background (), query )
232
+ tables , err := p .conn .QueryContext (context .Background (), query )
233
233
if err != nil {
234
234
return & database.Error {OrigErr : err , Query : []byte (query )}
235
235
}
@@ -251,7 +251,7 @@ func (p *Postgres) Drop() error {
251
251
// delete one by one ...
252
252
for _ , t := range tableNames {
253
253
query = `DROP TABLE IF EXISTS ` + t + ` CASCADE`
254
- if _ , err := p .db .ExecContext (context .Background (), query ); err != nil {
254
+ if _ , err := p .conn .ExecContext (context .Background (), query ); err != nil {
255
255
return & database.Error {OrigErr : err , Query : []byte (query )}
256
256
}
257
257
}
@@ -267,7 +267,7 @@ func (p *Postgres) ensureVersionTable() error {
267
267
// check if migration table exists
268
268
var count int
269
269
query := `SELECT COUNT(1) FROM information_schema.tables WHERE table_name = $1 AND table_schema = (SELECT current_schema()) LIMIT 1`
270
- if err := p .db .QueryRowContext (context .Background (), query , p .config .MigrationsTable ).Scan (& count ); err != nil {
270
+ if err := p .conn .QueryRowContext (context .Background (), query , p .config .MigrationsTable ).Scan (& count ); err != nil {
271
271
return & database.Error {OrigErr : err , Query : []byte (query )}
272
272
}
273
273
if count == 1 {
@@ -276,7 +276,7 @@ func (p *Postgres) ensureVersionTable() error {
276
276
277
277
// if not, create the empty migration table
278
278
query = `CREATE TABLE "` + p .config .MigrationsTable + `" (version bigint not null primary key, dirty boolean not null)`
279
- if _ , err := p .db .ExecContext (context .Background (), query ); err != nil {
279
+ if _ , err := p .conn .ExecContext (context .Background (), query ); err != nil {
280
280
return & database.Error {OrigErr : err , Query : []byte (query )}
281
281
}
282
282
return nil
0 commit comments