Skip to content

Commit 518bc4d

Browse files
authored
refactor(database): add special handling for PostgreSQL-specific migrations
1 parent 89620a7 commit 518bc4d

File tree

2 files changed

+163
-146
lines changed

2 files changed

+163
-146
lines changed

internal/database/database.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111

1212
// Postgresql driver import
13-
_ "github.com/lib/pq"
13+
pq "github.com/lib/pq"
1414
)
1515

1616
// NewConnectionPool configures the database connection pool.
@@ -32,6 +32,14 @@ func Migrate(db *sql.DB) error {
3232
var currentVersion int
3333
db.QueryRow(`SELECT version FROM schema_version`).Scan(&currentVersion)
3434

35+
driver := ""
36+
switch db.Driver().(type) {
37+
case *pq.Driver:
38+
driver = "postgresql"
39+
default:
40+
panic(fmt.Sprintf("the driver %s isn't supported", db.Driver()))
41+
}
42+
3543
slog.Info("Running database migrations",
3644
slog.Int("current_version", currentVersion),
3745
slog.Int("latest_version", schemaVersion),
@@ -45,7 +53,7 @@ func Migrate(db *sql.DB) error {
4553
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
4654
}
4755

48-
if err := migrations[version](tx); err != nil {
56+
if err := migrations[version](tx, driver); err != nil {
4957
tx.Rollback()
5058
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
5159
}

0 commit comments

Comments
 (0)