Skip to content

Commit 912e9c9

Browse files
committed
Migrate to sql.DB driver
This is to reduce dependency on a particular driver. In the future this will be used to implement different ways of cleaning the database.
1 parent 883c9e0 commit 912e9c9

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 3-Clause License
22

3-
Copyright (c) 2023, Vasiliy Vasilyuk
3+
Copyright (c) 2023-2024, Vasiliy Vasilyuk
44

55
Redistribution and use in source and binary forms, with or without
66
modification, are permitted provided that the following conditions are met:

testingpg/testingpg.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
1+
// Copyright (c) 2023-2024 Vasiliy Vasilyuk. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -7,6 +7,7 @@ package testingpg
77
import (
88
"context"
99
"crypto/rand"
10+
"database/sql"
1011
"encoding/base64"
1112
"fmt"
1213
"net/url"
@@ -16,7 +17,7 @@ import (
1617
"time"
1718
"unicode"
1819

19-
"github.com/jackc/pgx/v5/pgxpool"
20+
_ "github.com/jackc/pgx/v5/stdlib"
2021
"github.com/stretchr/testify/require"
2122
)
2223

@@ -40,8 +41,8 @@ type Postgres struct {
4041
url string
4142
ref string
4243

43-
pgxpool *pgxpool.Pool
44-
pgxpoolOnce sync.Once
44+
sqlDB *sql.DB
45+
sqlDBOnce sync.Once
4546
}
4647

4748
func newPostgres(t TestingT) *Postgres {
@@ -71,12 +72,12 @@ func (p *Postgres) URL() string {
7172
return p.url
7273
}
7374

74-
func (p *Postgres) PgxPool() *pgxpool.Pool {
75-
p.pgxpoolOnce.Do(func() {
76-
p.pgxpool = newPGxPool(p.t, p.URL())
75+
func (p *Postgres) DB() *sql.DB {
76+
p.sqlDBOnce.Do(func() {
77+
p.sqlDB = open(p.t, p.URL())
7778
})
7879

79-
return p.pgxpool
80+
return p.sqlDB
8081
}
8182

8283
func (p *Postgres) cloneFromReference() *Postgres {
@@ -90,7 +91,7 @@ func (p *Postgres) cloneFromReference() *Postgres {
9091
p.ref,
9192
)
9293

93-
_, err := p.PgxPool().Exec(context.Background(), sql)
94+
_, err := p.DB().ExecContext(context.Background(), sql)
9495
require.NoError(p.t, err)
9596

9697
// Automatically drop database copy after the test is completed.
@@ -100,7 +101,7 @@ func (p *Postgres) cloneFromReference() *Postgres {
100101
ctx, done := context.WithTimeout(context.Background(), time.Minute)
101102
defer done()
102103

103-
_, err := p.PgxPool().Exec(ctx, sql)
104+
_, err := p.DB().ExecContext(ctx, sql)
104105
require.NoError(p.t, err)
105106
})
106107

@@ -169,17 +170,14 @@ func replaceDBName(t TestingT, dataSourceURL, dbname string) string {
169170
return r.String()
170171
}
171172

172-
func newPGxPool(t TestingT, dataSourceURL string) *pgxpool.Pool {
173-
ctx, done := context.WithTimeout(context.Background(), 1*time.Second)
174-
defer done()
175-
176-
pool, err := pgxpool.New(ctx, dataSourceURL)
173+
func open(t TestingT, dataSourceURL string) *sql.DB {
174+
db, err := sql.Open("pgx/v5", dataSourceURL)
177175
require.NoError(t, err)
178176

179177
// Automatically close connection after the test is completed.
180178
t.Cleanup(func() {
181-
pool.Close()
179+
db.Close()
182180
})
183181

184-
return pool
182+
return db
185183
}

testingpg/testingpg_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
1+
// Copyright (c) 2023-2024 Vasiliy Vasilyuk. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -50,7 +50,7 @@ func TestNewPostgres(t *testing.T) {
5050

5151
// Act
5252
var version string
53-
err := postgres.PgxPool().QueryRow(ctx, "SELECT version();").Scan(&version)
53+
err := postgres.DB().QueryRowContext(ctx, "SELECT version();").Scan(&version)
5454

5555
// Assert
5656
require.NoError(t, err)
@@ -70,8 +70,8 @@ func TestNewPostgres(t *testing.T) {
7070

7171
// Act
7272
const sql = `CREATE TABLE "no_conflict" (id integer PRIMARY KEY)`
73-
_, err1 := postgres1.PgxPool().Exec(ctx, sql)
74-
_, err2 := postgres2.PgxPool().Exec(ctx, sql)
73+
_, err1 := postgres1.DB().ExecContext(ctx, sql)
74+
_, err2 := postgres2.DB().ExecContext(ctx, sql)
7575

7676
// Assert
7777
require.NoError(t, err1)

user_repository.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
1+
// Copyright (c) 2023-2024 Vasiliy Vasilyuk. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

55
package testing_go_code_with_postgres
66

77
import (
88
"context"
9+
"database/sql"
910
"fmt"
1011

1112
"github.com/google/uuid"
12-
"github.com/jackc/pgx/v5/pgxpool"
1313
)
1414

15-
func NewUserRepository(db *pgxpool.Pool) *UserRepository {
15+
func NewUserRepository(db *sql.DB) *UserRepository {
1616
return &UserRepository{db: db}
1717
}
1818

1919
type UserRepository struct {
20-
db *pgxpool.Pool
20+
db *sql.DB
2121
}
2222

2323
func (r *UserRepository) ReadUser(ctx context.Context, userID uuid.UUID) (User, error) {
2424
const sql = `SELECT user_id, username, created_at FROM users WHERE user_id = $1;`
2525

2626
user := User{}
2727

28-
row := r.db.QueryRow(ctx, sql, userID)
28+
row := r.db.QueryRowContext(ctx, sql, userID)
2929

3030
err := row.Scan(&user.ID, &user.Username, &user.CreatedAt)
3131
if err != nil {
@@ -40,7 +40,7 @@ func (r *UserRepository) ReadUser(ctx context.Context, userID uuid.UUID) (User,
4040
func (r *UserRepository) CreateUser(ctx context.Context, user User) error {
4141
const sql = `INSERT INTO users (user_id, username, created_at) VALUES ($1,$2,$3);`
4242

43-
_, err := r.db.Exec(
43+
_, err := r.db.ExecContext(
4444
ctx,
4545
sql,
4646
user.ID,

user_repository_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
1+
// Copyright (c) 2023-2024 Vasiliy Vasilyuk. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

@@ -36,7 +36,7 @@ func TestUserRepository_CreateUser(t *testing.T) {
3636

3737
// Arrange
3838
postgres := testingpg.New(t)
39-
repo := rootpkg.NewUserRepository(postgres.PgxPool())
39+
repo := rootpkg.NewUserRepository(postgres.DB())
4040

4141
user := newFullyFiledUser()
4242

@@ -57,7 +57,7 @@ func TestUserRepository_CreateUser(t *testing.T) {
5757

5858
// Arrange
5959
postgres := testingpg.New(t)
60-
repo := rootpkg.NewUserRepository(postgres.PgxPool())
60+
repo := rootpkg.NewUserRepository(postgres.DB())
6161

6262
user := newFullyFiledUser()
6363

@@ -85,7 +85,7 @@ func TestUserRepository_ReadUser(t *testing.T) {
8585

8686
// Arrange
8787
postgres := testingpg.New(t)
88-
repo := rootpkg.NewUserRepository(postgres.PgxPool())
88+
repo := rootpkg.NewUserRepository(postgres.DB())
8989

9090
// Act
9191
_, err := repo.ReadUser(context.Background(), uuid.New())

0 commit comments

Comments
 (0)