Skip to content

Commit

Permalink
Fixes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
vminkobin committed Apr 14, 2023
1 parent 798525c commit f11ec53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
30 changes: 4 additions & 26 deletions database/migrate.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package database

import (
"database/sql"
"errors"
"fmt"

"github.com/golang-migrate/migrate/v4"
// required by migrate.New... to parse migration files directory
_ "github.com/golang-migrate/migrate/v4/source/file"

migratepg "github.com/golang-migrate/migrate/v4/database/postgres"
)

// supported migrate operations
Expand Down Expand Up @@ -62,35 +59,16 @@ func WithFilesDir(filesDir string) Option {

// NewMigrationRunner creates a new MigrationRunner with the given database connection string (dsn) and options.
func NewMigrationRunner(dsn string, opts ...Option) (*MigrationRunner, error) {
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("sql open dsn: %v", err)
}

driver, err := migratepg.WithInstance(db, &migratepg.Config{})
if err != nil {
return nil, fmt.Errorf("building postgres driver for db migrations: %w", err)
runner := &MigrationRunner{
filesDir: defaultFilesDir,
logger: &noopLogger{},
}

runner := &MigrationRunner{}

for _, opt := range opts {
opt(runner)
}

if runner.filesDir == "" {
runner.filesDir = defaultFilesDir
}

if runner.logger == nil {
runner.logger = &noopLogger{}
}

mgr, err := migrate.NewWithDatabaseInstance(
"file://"+runner.filesDir,
"postgres",
driver,
)
mgr, err := migrate.New("file://"+runner.filesDir, dsn)
if err != nil {
return nil, fmt.Errorf("creating Migrate object: %w", err)
}
Expand Down
25 changes: 10 additions & 15 deletions database/migration_runner_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ import (

// Constants representing environment variable keys for migration configuration.
const (
envKeyPrefix = "MIGRATION"
envKeyDsn = "DSN"
envKeyOp = "OPERATION"
envKeyForceVersion = "FORCE_VERSION"
envKeyFilesDir = "FILES_DIR"
envKeyDsn = "MIGRATION_DSN"
envKeyOp = "MIGRATION_OPERATION"
envKeyForceVersion = "MIGRATION_FORCE_VERSION"
envKeyFilesDir = "MIGRATION_FILES_DIR"
)

func envKey(key string) string {
return fmt.Sprintf("%s_%s", envKeyPrefix, key)
}

func readForceVersion() (int, error) {
forceVersionRaw, ok := os.LookupEnv(envKey(envKeyForceVersion))
forceVersionRaw, ok := os.LookupEnv(envKeyForceVersion)
if !ok {
return 0, nil
}
Expand All @@ -36,22 +31,22 @@ func readForceVersion() (int, error) {
// RunMigrationsFromEnv reads migration configuration from environment variables,
// creates a MigrationRunner, and runs the specified migration operation.
func RunMigrationsFromEnv(logger logger) error {
dsn, ok := os.LookupEnv(envKey(envKeyDsn))
dsn, ok := os.LookupEnv(envKeyDsn)
if !ok {
return fmt.Errorf("missing env: %s", envKey(envKeyDsn))
return fmt.Errorf("missing env: %s", envKeyDsn)
}

operation, ok := os.LookupEnv(envKey(envKeyOp))
operation, ok := os.LookupEnv(envKeyOp)
if !ok {
return fmt.Errorf("missing env: %s", envKey(envKeyOp))
return fmt.Errorf("missing env: %s", envKeyOp)
}

forceVersion, err := readForceVersion()
if err != nil {
return fmt.Errorf("read forceVersion: %v", err)
}

filesDir := os.Getenv(envKey(envKeyFilesDir))
filesDir := os.Getenv(envKeyFilesDir)

runner, err := NewMigrationRunner(dsn, WithFilesDir(filesDir), WithLogger(logger))
if err != nil {
Expand Down

0 comments on commit f11ec53

Please sign in to comment.