Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1519c59

Browse files
committed
Rename Postgres.db to Postgres.conn
1 parent ab7dfb3 commit 1519c59

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

database/postgres/postgres.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Config struct {
3737

3838
type Postgres struct {
3939
// Locking and unlocking need to use the same connection
40-
db *sql.Conn
40+
conn *sql.Conn
4141
isLocked bool
4242

4343
// 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) {
7676
}
7777

7878
px := &Postgres{
79-
db: conn,
79+
conn: conn,
8080
config: config,
8181
}
8282

@@ -115,7 +115,7 @@ func (p *Postgres) Open(url string) (database.Driver, error) {
115115
}
116116

117117
func (p *Postgres) Close() error {
118-
return p.db.Close()
118+
return p.conn.Close()
119119
}
120120

121121
// https://www.postgresql.org/docs/9.6/static/explicit-locking.html#ADVISORY-LOCKS
@@ -133,7 +133,7 @@ func (p *Postgres) Lock() error {
133133
// or return false if the lock cannot be acquired immediately.
134134
query := `SELECT pg_try_advisory_lock($1)`
135135
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 {
137137
return &database.Error{OrigErr: err, Err: "try lock failed", Query: []byte(query)}
138138
}
139139

@@ -156,7 +156,7 @@ func (p *Postgres) Unlock() error {
156156
}
157157

158158
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 {
160160
return &database.Error{OrigErr: err, Query: []byte(query)}
161161
}
162162
p.isLocked = false
@@ -171,7 +171,7 @@ func (p *Postgres) Run(migration io.Reader) error {
171171

172172
// run migration
173173
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 {
175175
// TODO: cast to postgress error and get line number
176176
return database.Error{OrigErr: err, Err: "migration failed", Query: migr}
177177
}
@@ -180,7 +180,7 @@ func (p *Postgres) Run(migration io.Reader) error {
180180
}
181181

182182
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{})
184184
if err != nil {
185185
return &database.Error{OrigErr: err, Err: "transaction start failed"}
186186
}
@@ -208,7 +208,7 @@ func (p *Postgres) SetVersion(version int, dirty bool) error {
208208

209209
func (p *Postgres) Version() (version int, dirty bool, err error) {
210210
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)
212212
switch {
213213
case err == sql.ErrNoRows:
214214
return database.NilVersion, false, nil
@@ -229,7 +229,7 @@ func (p *Postgres) Version() (version int, dirty bool, err error) {
229229
func (p *Postgres) Drop() error {
230230
// select all tables in current schema
231231
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)
233233
if err != nil {
234234
return &database.Error{OrigErr: err, Query: []byte(query)}
235235
}
@@ -251,7 +251,7 @@ func (p *Postgres) Drop() error {
251251
// delete one by one ...
252252
for _, t := range tableNames {
253253
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 {
255255
return &database.Error{OrigErr: err, Query: []byte(query)}
256256
}
257257
}
@@ -267,7 +267,7 @@ func (p *Postgres) ensureVersionTable() error {
267267
// check if migration table exists
268268
var count int
269269
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 {
271271
return &database.Error{OrigErr: err, Query: []byte(query)}
272272
}
273273
if count == 1 {
@@ -276,7 +276,7 @@ func (p *Postgres) ensureVersionTable() error {
276276

277277
// if not, create the empty migration table
278278
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 {
280280
return &database.Error{OrigErr: err, Query: []byte(query)}
281281
}
282282
return nil

database/postgres/postgres_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestMultiStatement(t *testing.T) {
7373

7474
// make sure second table exists
7575
var exists bool
76-
if err := d.(*Postgres).db.QueryRowContext(context.Background(), "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'bar' AND table_schema = (SELECT current_schema()))").Scan(&exists); err != nil {
76+
if err := d.(*Postgres).conn.QueryRowContext(context.Background(), "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'bar' AND table_schema = (SELECT current_schema()))").Scan(&exists); err != nil {
7777
t.Fatal(err)
7878
}
7979
if !exists {

0 commit comments

Comments
 (0)