feat(03-01): add pgx/v5, PostgreSQL migrations, rename RunMigrations to RunSQLiteMigrations
- Add github.com/jackc/pgx/v5 v5.9.1 dependency - Add golang-migrate pgx/v5 driver - Create migrations/postgres/0001_initial_schema.up.sql with SERIAL PRIMARY KEY - Create migrations/postgres/0001_initial_schema.down.sql - Rename RunMigrations to RunSQLiteMigrations in migrate.go - Add RunPostgresMigrations with pgxmigrate driver and 'pgx5' name - Update export_test.go to use RunSQLiteMigrations (go vet compliance)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
||||
sqlitemigrate "github.com/golang-migrate/migrate/v4/database/sqlite"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
_ "modernc.org/sqlite"
|
||||
@@ -14,9 +15,12 @@ import (
|
||||
//go:embed migrations/sqlite
|
||||
var sqliteMigrations embed.FS
|
||||
|
||||
// RunMigrations applies all pending schema migrations to the given SQLite database.
|
||||
//go:embed migrations/postgres
|
||||
var postgresMigrations embed.FS
|
||||
|
||||
// RunSQLiteMigrations applies all pending schema migrations to the given SQLite database.
|
||||
// Returns nil if all migrations applied successfully or if database is already up to date.
|
||||
func RunMigrations(db *sql.DB) error {
|
||||
func RunSQLiteMigrations(db *sql.DB) error {
|
||||
src, err := iofs.New(sqliteMigrations, "migrations/sqlite")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -34,3 +38,24 @@ func RunMigrations(db *sql.DB) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunPostgresMigrations applies all pending schema migrations to the given PostgreSQL database.
|
||||
// Returns nil if all migrations applied successfully or if database is already up to date.
|
||||
func RunPostgresMigrations(db *sql.DB) error {
|
||||
src, err := iofs.New(postgresMigrations, "migrations/postgres")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
driver, err := pgxmigrate.WithInstance(db, &pgxmigrate.Config{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m, err := migrate.NewWithInstance("iofs", src, "pgx5", driver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user