Skip to content

Commit 3c28a8a

Browse files
committed
refactor: replace database/sql with native pgx usage
1 parent 14b5ce0 commit 3c28a8a

File tree

23 files changed

+257
-234
lines changed

23 files changed

+257
-234
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ require (
9191
github.com/containerd/containerd v1.7.12 // indirect
9292
github.com/containerd/log v0.1.0 // indirect
9393
github.com/cpuguy83/dockercfg v0.3.1 // indirect
94-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
94+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
9595
github.com/docker/docker v25.0.5+incompatible // indirect
9696
github.com/docker/go-connections v0.5.0 // indirect
9797
github.com/docker/go-units v0.5.0 // indirect

internal/config/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ type (
144144

145145
// Database contains configuration for the database.
146146
Database struct {
147-
Engine string `mapstructure:"engine"` // Database engine type (e.g., "postgres" or "memory")
148-
URI string `mapstructure:"uri"` // Database connection URI
149-
AutoMigrate bool `mapstructure:"auto_migrate"` // Whether to enable automatic migration
147+
Engine string `mapstructure:"engine"` // Database engine type (e.g., "postgres" or "memory")
148+
URI string `mapstructure:"uri"` // Database connection URI
149+
AutoMigrate bool `mapstructure:"auto_migrate"` // Whether to enable automatic migration
150+
SimpleMode bool `mapstructure:"simple_mode"`
150151
MaxOpenConnections int `mapstructure:"max_open_connections"` // Maximum number of open connections to the database
151152
MaxIdleConnections int `mapstructure:"max_idle_connections"` // Maximum number of idle connections to the database
152153
MaxConnectionLifetime time.Duration `mapstructure:"max_connection_lifetime"` // Maximum duration a connection can be reused
@@ -317,6 +318,7 @@ func DefaultConfig() *Config {
317318
Database: Database{
318319
Engine: "memory",
319320
AutoMigrate: true,
321+
SimpleMode: false,
320322
MaxDataPerWrite: 1000,
321323
MaxRetries: 10,
322324
WatchBufferSize: 100,

internal/factories/database.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ func DatabaseFactory(conf config.Database) (db database.Database, err error) {
4040
PQDatabase.WatchBufferSize(conf.WatchBufferSize),
4141
PQDatabase.MaxDataPerWrite(conf.MaxDataPerWrite),
4242
PQDatabase.MaxRetries(conf.MaxRetries),
43+
PQDatabase.SimpleMode(conf.SimpleMode),
4344
)
4445
if err != nil {
4546
return nil, err
4647
}
48+
4749
// check postgres version
48-
_, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).DB)
50+
_, err = utils.EnsureDBVersion(db.(*PQDatabase.Postgres).ReadPool)
4951
if err != nil {
5052
return nil, err
5153
}

internal/storage/migration.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"log"
77

8+
"github.com/jackc/pgx/v5/stdlib"
9+
810
"github.com/pressly/goose/v3"
911

1012
"github.com/Permify/permify/internal/config"
@@ -29,15 +31,15 @@ func Migrate(conf config.Database) (err error) {
2931
case database.POSTGRES.String():
3032
// Create a new Postgres database connection
3133
var db *PQDatabase.Postgres
32-
db, err = PQDatabase.New(conf.URI)
34+
db, err = PQDatabase.New(conf.URI, PQDatabase.SimpleMode(conf.SimpleMode))
3335
if err != nil {
3436
return err
3537
}
3638
// Ensure database connection is closed when function returns
3739
defer closeDB(db)
3840

3941
// check postgres version
40-
_, err = utils.EnsureDBVersion(db.DB)
42+
_, err = utils.EnsureDBVersion(db.ReadPool)
4143
if err != nil {
4244
return err
4345
}
@@ -53,8 +55,10 @@ func Migrate(conf config.Database) (err error) {
5355
// Set file system for migration scripts
5456
goose.SetBaseFS(postgresMigrations)
5557

58+
pool := stdlib.OpenDBFromPool(db.WritePool)
59+
5660
// Perform migration
57-
if err = goose.Up(db.DB, postgresMigrationDir); err != nil {
61+
if err = goose.Up(pool, postgresMigrationDir); err != nil {
5862
return err
5963
}
6064

@@ -86,8 +90,9 @@ func MigrateUp(engine, uri string) (err error) {
8690
}
8791

8892
goose.SetBaseFS(postgresMigrations)
93+
pool := stdlib.OpenDBFromPool(db.WritePool)
8994

90-
if err = goose.Up(db.DB, postgresMigrationDir); err != nil {
95+
if err = goose.Up(pool, postgresMigrationDir); err != nil {
9196
return err
9297
}
9398

@@ -117,8 +122,9 @@ func MigrateUpTo(engine, uri string, p int64) (err error) {
117122
}
118123

119124
goose.SetBaseFS(postgresMigrations)
125+
pool := stdlib.OpenDBFromPool(db.WritePool)
120126

121-
if err = goose.UpTo(db.DB, postgresMigrationDir, p); err != nil {
127+
if err = goose.UpTo(pool, postgresMigrationDir, p); err != nil {
122128
return err
123129
}
124130

@@ -148,8 +154,9 @@ func MigrateDown(engine, uri string) (err error) {
148154
}
149155

150156
goose.SetBaseFS(postgresMigrations)
157+
pool := stdlib.OpenDBFromPool(db.WritePool)
151158

152-
if err = goose.Down(db.DB, postgresMigrationDir); err != nil {
159+
if err = goose.Down(pool, postgresMigrationDir); err != nil {
153160
return err
154161
}
155162

@@ -179,8 +186,9 @@ func MigrateDownTo(engine, uri string, p int64) (err error) {
179186
}
180187

181188
goose.SetBaseFS(postgresMigrations)
189+
pool := stdlib.OpenDBFromPool(db.WritePool)
182190

183-
if err = goose.DownTo(db.DB, postgresMigrationDir, p); err != nil {
191+
if err = goose.DownTo(pool, postgresMigrationDir, p); err != nil {
184192
return err
185193
}
186194

@@ -210,8 +218,9 @@ func MigrateReset(engine, uri string) (err error) {
210218
}
211219

212220
goose.SetBaseFS(postgresMigrations)
221+
pool := stdlib.OpenDBFromPool(db.WritePool)
213222

214-
if err = goose.Reset(db.DB, postgresMigrationDir); err != nil {
223+
if err = goose.Reset(pool, postgresMigrationDir); err != nil {
215224
return err
216225
}
217226

@@ -241,8 +250,9 @@ func MigrateStatus(engine, uri string) (err error) {
241250
}
242251

243252
goose.SetBaseFS(postgresMigrations)
253+
pool := stdlib.OpenDBFromPool(db.WritePool)
244254

245-
if err = goose.Status(db.DB, postgresMigrationDir); err != nil {
255+
if err = goose.Status(pool, postgresMigrationDir); err != nil {
246256
return err
247257
}
248258

internal/storage/postgres/bundleReader.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package postgres
22

33
import (
44
"context"
5-
"database/sql"
65
"errors"
76
"log/slog"
87
"strings"
98

9+
"github.com/jackc/pgx/v5"
10+
1011
"github.com/Masterminds/squirrel"
1112
"github.com/golang/protobuf/jsonpb"
1213
"go.opentelemetry.io/otel/codes"
@@ -18,13 +19,13 @@ import (
1819

1920
type BundleReader struct {
2021
database *db.Postgres
21-
txOptions sql.TxOptions
22+
txOptions pgx.TxOptions
2223
}
2324

2425
func NewBundleReader(database *db.Postgres) *BundleReader {
2526
return &BundleReader{
2627
database: database,
27-
txOptions: sql.TxOptions{Isolation: sql.LevelReadCommitted, ReadOnly: false},
28+
txOptions: pgx.TxOptions{IsoLevel: pgx.ReadCommitted, AccessMode: pgx.ReadWrite},
2829
}
2930
}
3031

@@ -46,13 +47,13 @@ func (b *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle
4647

4748
slog.Debug("executing sql query", slog.Any("query", query), slog.Any("arguments", args))
4849

49-
var row *sql.Row
50-
row = b.database.DB.QueryRowContext(ctx, query, args...)
50+
var row pgx.Row
51+
row = b.database.WritePool.QueryRow(ctx, query, args...)
5152

5253
var jsonData string
5354
err = row.Scan(&jsonData)
5455
if err != nil {
55-
if errors.Is(err, sql.ErrNoRows) {
56+
if errors.Is(err, pgx.ErrNoRows) {
5657
return nil, errors.New(base.ErrorCode_ERROR_CODE_BUNDLE_NOT_FOUND.String())
5758
}
5859
return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SCAN)

internal/storage/postgres/bundleWriter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package postgres
22

33
import (
44
"context"
5-
"database/sql"
65
"log/slog"
76

7+
"github.com/jackc/pgx/v5"
8+
89
"github.com/Masterminds/squirrel"
910
"github.com/golang/protobuf/jsonpb"
1011

@@ -17,13 +18,13 @@ import (
1718
type BundleWriter struct {
1819
database *db.Postgres
1920
// options
20-
txOptions sql.TxOptions
21+
txOptions pgx.TxOptions
2122
}
2223

2324
func NewBundleWriter(database *db.Postgres) *BundleWriter {
2425
return &BundleWriter{
2526
database: database,
26-
txOptions: sql.TxOptions{Isolation: sql.LevelReadCommitted, ReadOnly: false},
27+
txOptions: pgx.TxOptions{IsoLevel: pgx.ReadCommitted, AccessMode: pgx.ReadWrite},
2728
}
2829
}
2930

@@ -60,7 +61,7 @@ func (b *BundleWriter) Write(ctx context.Context, bundles []storage.Bundle) (nam
6061

6162
slog.Debug("executing sql insert query", slog.Any("query", query), slog.Any("arguments", args))
6263

63-
_, err = b.database.DB.ExecContext(ctx, query, args...)
64+
_, err = b.database.WritePool.Exec(ctx, query, args...)
6465
if err != nil {
6566
return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
6667
}
@@ -86,7 +87,7 @@ func (b *BundleWriter) Delete(ctx context.Context, tenantID, name string) (err e
8687
return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SQL_BUILDER)
8788
}
8889

89-
_, err = b.database.DB.ExecContext(ctx, query, args...)
90+
_, err = b.database.WritePool.Exec(ctx, query, args...)
9091
if err != nil {
9192
return utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_EXECUTION)
9293
}

0 commit comments

Comments
 (0)