Skip to content

Commit

Permalink
refactor(database): add special handling for PostgreSQL-specific migr…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
jvoisin authored Dec 26, 2024
1 parent 89620a7 commit 518bc4d
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 146 deletions.
12 changes: 10 additions & 2 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

// Postgresql driver import
_ "github.com/lib/pq"
pq "github.com/lib/pq"
)

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

driver := ""
switch db.Driver().(type) {
case *pq.Driver:
driver = "postgresql"
default:
panic(fmt.Sprintf("the driver %s isn't supported", db.Driver()))
}

slog.Info("Running database migrations",
slog.Int("current_version", currentVersion),
slog.Int("latest_version", schemaVersion),
Expand All @@ -45,7 +53,7 @@ func Migrate(db *sql.DB) error {
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
}

if err := migrations[version](tx); err != nil {
if err := migrations[version](tx, driver); err != nil {
tx.Rollback()
return fmt.Errorf("[Migration v%d] %v", newVersion, err)
}
Expand Down
Loading

0 comments on commit 518bc4d

Please sign in to comment.