Skip to content

Commit

Permalink
Restructure and make it to an installable command
Browse files Browse the repository at this point in the history
  • Loading branch information
olbrichattila committed Jul 14, 2024
1 parent 6f7dc7b commit 6c25262
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 88 deletions.
8 changes: 8 additions & 0 deletions cmd/migrator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Package main initiate db migrator command
package main

import migrator "github.com/olbrichattila/godbmigrator_cmd/internal"

func main() {
migrator.Init()
}
14 changes: 14 additions & 0 deletions internal/connection_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package migrator

const (
driverTypeSqLite = "sqlite3"
driverTypeMysql = "mysql"

providerSqLiteDataType = "sqlite"
providerPgSQLDataType = "pgsql"
providerMySQLDataType = "mysql"
providerFirebirdDataType = "firebird"

providerTypeJSON = "json"
providerTypeDB = "db"
)
6 changes: 4 additions & 2 deletions firebird_connection.go → internal/firebird_connection.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main
// Package migrator wraps around migrator package and exposes as a CLI tool
package migrator

import (
"database/sql"
"fmt"

// This needs to be blank imported as not directly referenced, but required
_ "github.com/nakagami/firebirdsql"
)

func NewFirebirdStore(
func newFirebirdStore(
host string,
port int,
user,
Expand Down
8 changes: 4 additions & 4 deletions migration_adapter.go → internal/migration_adapter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package migrator

import (
"database/sql"
Expand All @@ -9,7 +9,7 @@ import (
type migrationAdapter struct {
}

func NewMigrationAdapter() *migrationAdapter {
func newMigrationAdapter() *migrationAdapter {
return &migrationAdapter{}
}

Expand All @@ -25,8 +25,8 @@ func (a *migrationAdapter) Refresh(db *sql.DB, provider migrator.MigrationProvid
return migrator.Refresh(db, provider, migrationPath)
}

func (a *migrationAdapter) AddNewMigrationFiles(migrationPath string, customText string) {
migrator.AddNewMigrationFiles(migrationPath, customText)
func (a *migrationAdapter) AddNewMigrationFiles(migrationPath string, customText string) error {
return migrator.AddNewMigrationFiles(migrationPath, customText)
}

func (a *migrationAdapter) Report(db *sql.DB, provider migrator.MigrationProvider, migrationPath string) (string, error) {
Expand Down
76 changes: 42 additions & 34 deletions migration_init.go → internal/migration_init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package migrator

import (
"database/sql"
Expand All @@ -9,6 +9,14 @@ import (
migrator "github.com/olbrichattila/godbmigrator"
)

const (
envDBHost = "DB_HOST"
envDBUserName = "DB_USERNAME"
envDBPassword = "DB_PASSWORD"
envDBDatabase = "DB_DATABASE"
envDBPort = "DB_PORT"
)

type migrationInitInterface interface {
migrationInit(args []string) (*sql.DB, migrator.MigrationProvider, int, error)
}
Expand Down Expand Up @@ -43,51 +51,51 @@ func (m *migrationInit) connection() (*sql.DB, error) {
dbConnection := os.Getenv("DB_CONNECTION")

switch dbConnection {
case "sqlite":
db, err := NewSqliteStore(os.Getenv("DB_DATABASE"))
case providerSqLiteDataType:
db, err := newSqliteStore(os.Getenv(envDBDatabase))
return db, err
case "pgsql":
case providerPgSQLDataType:
sslMode, err := m.getPostgresSSLMode()
if err != nil {
return nil, err
}
port, err := strconv.Atoi(os.Getenv("DB_PORT"))
port, err := strconv.Atoi(os.Getenv(envDBPort))
if err != nil {
return nil, err
}
db, err := NewPostgresStore(
os.Getenv("DB_HOST"),
db, err := newPostgresStore(
os.Getenv(envDBHost),
port,
os.Getenv("DB_USERNAME"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_DATABASE"),
os.Getenv(envDBUserName),
os.Getenv(envDBPassword),
os.Getenv(envDBDatabase),
sslMode,
)
return db, err
case "mysql":
port, err := strconv.Atoi(os.Getenv("DB_PORT"))
case providerMySQLDataType:
port, err := strconv.Atoi(os.Getenv(envDBPort))
if err != nil {
return nil, err
}
db, err := NewMysqlStore(
os.Getenv("DB_HOST"),
db, err := newMysqlStore(
os.Getenv(envDBHost),
port,
os.Getenv("DB_USERNAME"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_DATABASE"),
os.Getenv(envDBUserName),
os.Getenv(envDBPassword),
os.Getenv(envDBDatabase),
)
return db, err
case "firebird":
port, err := strconv.Atoi(os.Getenv("DB_PORT"))
case providerFirebirdDataType:
port, err := strconv.Atoi(os.Getenv(envDBPort))
if err != nil {
return nil, err
}
db, err := NewFirebirdStore(
os.Getenv("DB_HOST"),
db, err := newFirebirdStore(
os.Getenv(envDBHost),
port,
os.Getenv("DB_USERNAME"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_DATABASE"),
os.Getenv(envDBUserName),
os.Getenv(envDBPassword),
os.Getenv(envDBDatabase),
)
return db, err
default:
Expand All @@ -99,10 +107,10 @@ func (m *migrationInit) provider(db *sql.DB) (migrator.MigrationProvider, error)
migrationProvider := os.Getenv("MIGRATOR_MIGRATION_PROVIDER")

switch migrationProvider {
case "db", "":
return migrator.NewMigrationProvider("db", db)
case "json":
return migrator.NewMigrationProvider("json", nil)
case providerTypeDB, "":
return migrator.NewMigrationProvider(providerTypeDB, db)
case providerTypeJSON:
return migrator.NewMigrationProvider(providerTypeJSON, nil)
default:
return nil, fmt.Errorf("migration provider for type %s does not exists", migrationProvider)
}
Expand All @@ -121,17 +129,17 @@ func (m *migrationInit) getPostgresSSLMode() (string, error) {

switch envSSLMOde {
case "disable", "":
return PgsSslMode.Disable, nil
return pgsSSLMode.Disable, nil
case "require":
return PgsSslMode.Require, nil
return pgsSSLMode.Require, nil
case "verify-ca":
return PgsSslMode.VerifyCa, nil
return pgsSSLMode.VerifyCa, nil
case "verify-full":
return PgsSslMode.VerifyFull, nil
return pgsSSLMode.VerifyFull, nil
case "prefer":
return PgsSslMode.Prefer, nil
return pgsSSLMode.Prefer, nil
case "allow":
return PgsSslMode.Allow, nil
return pgsSSLMode.Allow, nil

default:
return "", fmt.Errorf(`the provided DB_SSLMODE environment variable is invalid '%s'.,
Expand Down
4 changes: 2 additions & 2 deletions migration_init_spy.go → internal/migration_init_spy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package migrator

import (
"database/sql"
Expand All @@ -15,7 +15,7 @@ func newMigrationInitSpy() *migrationInitSpy {
}

func (m *migrationInitSpy) migrationInit(args []string) (*sql.DB, migrator.MigrationProvider, int, error) {
conn, err := sql.Open("sqlite3", ":memory:")
conn, err := sql.Open(driverTypeSqLite, ":memory:")
if err != nil {
return nil, nil, 0, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package migrator

type migrationProviderSpy struct {
}
Expand Down
2 changes: 1 addition & 1 deletion migration_test.go → internal/migration_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package migrator

import (
"testing"
Expand Down
26 changes: 15 additions & 11 deletions main.go → internal/migrator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package migrator

import (
"database/sql"
Expand All @@ -9,26 +9,27 @@ import (
migrator "github.com/olbrichattila/godbmigrator"
)

const defaultMigrationPath = "./migrations"

type providerInterface interface {
}
const (
defaultMigrationPath = "./migrations"
messageRollingBack = "Rolling back"
)

type migratorInterface interface {
Migrate(*sql.DB, migrator.MigrationProvider, string, int) error
Rollback(*sql.DB, migrator.MigrationProvider, string, int) error
Refresh(*sql.DB, migrator.MigrationProvider, string) error
Report(*sql.DB, migrator.MigrationProvider, string) (string, error)
AddNewMigrationFiles(string, string)
AddNewMigrationFiles(string, string) error
}

func main() {
// Init starts migration command, reads .env and command line arguments, and execute what was requested
func Init() {
if err := loadEnv(); err != nil {
fmt.Println("Error loading .env file:", err)
return
}

migrationAdapter := NewMigrationAdapter()
migrationAdapter := newMigrationAdapter()
migrationInit := newMigrationInit()
if err := routeCommandLineParameters(os.Args, migrationAdapter, migrationInit); err != nil {
displayUsage()
Expand Down Expand Up @@ -74,7 +75,7 @@ func migrate(args []string, migrationAdapter migratorInterface, migrationInit mi
}

func rollback(args []string, migrationAdapter migratorInterface, migrationInit migrationInitInterface) {
fmt.Println("Rolling back")
fmt.Println(messageRollingBack)
conn, provider, count, err := migrationInit.migrationInit(args)
if err != nil {
fmt.Println(err)
Expand All @@ -89,7 +90,7 @@ func rollback(args []string, migrationAdapter migratorInterface, migrationInit m
}

func refresh(args []string, migrationAdapter migratorInterface, migrationInit migrationInitInterface) {
fmt.Println("Rolling back")
fmt.Println(messageRollingBack)
conn, provider, _, err := migrationInit.migrationInit(args)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -128,7 +129,10 @@ func add(args []string, migrationAdapter migratorInterface) {
}

migrationPath := migrationPath()
migrationAdapter.AddNewMigrationFiles(migrationPath, customText)
err := migrationAdapter.AddNewMigrationFiles(migrationPath, customText)
if err != nil {
fmt.Println(err)
}
}

func displayUsage() {
Expand Down
11 changes: 6 additions & 5 deletions migrator_spy.go → internal/migrator_spy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package migrator

import (
"database/sql"
Expand All @@ -20,26 +20,27 @@ func newSpyMigrator() *spyMigrator {
return &spyMigrator{}
}

func (a *spyMigrator) Migrate(db *sql.DB, provider migrator.MigrationProvider, migrationPath string, count int) error {
func (a *spyMigrator) Migrate(_ *sql.DB, _ migrator.MigrationProvider, _ string, count int) error {
a.migrateCalled++
a.lastCount = count
return nil
}

func (a *spyMigrator) Rollback(db *sql.DB, provider migrator.MigrationProvider, migrationPath string, count int) error {
func (a *spyMigrator) Rollback(_ *sql.DB, _ migrator.MigrationProvider, _ string, count int) error {
a.rollbackCalled++
a.lastCount = count
return nil
}

func (a *spyMigrator) Refresh(db *sql.DB, provider migrator.MigrationProvider, migrationPath string) error {
func (a *spyMigrator) Refresh(_ *sql.DB, _ migrator.MigrationProvider, _ string) error {
a.refreshCalled++
return nil
}

func (a *spyMigrator) AddNewMigrationFiles(migrationPath string, customText string) {
func (a *spyMigrator) AddNewMigrationFiles(_ string, customText string) error {
a.lastAddCustomText = customText
a.addCalled++
return nil
}

func (a *spyMigrator) Report(*sql.DB, migrator.MigrationProvider, string) (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions mysql_connection.go → internal/mysql_connection.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main
package migrator

import (
"database/sql"
"fmt"

// This needs to be blank imported as not directly referenced, but required
_ "github.com/go-sql-driver/mysql"
)

func NewMysqlStore(
func newMysqlStore(
host string,
port int,
user,
Expand Down
9 changes: 5 additions & 4 deletions postgres_connection.go → internal/postgres_connection.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main
package migrator

import (
"database/sql"
"fmt"

// This needs to be blank imported as not directly referenced, but required
_ "github.com/lib/pq"
)

type pgsSslMode struct {
type pgsSSLModeTypes struct {
Disable string
Require string
VerifyCa string
Expand All @@ -16,7 +17,7 @@ type pgsSslMode struct {
Allow string
}

var PgsSslMode = &pgsSslMode{
var pgsSSLMode = &pgsSSLModeTypes{
Disable: "disable",
Require: "require",
VerifyCa: "verify-ca",
Expand All @@ -25,7 +26,7 @@ var PgsSslMode = &pgsSslMode{
Allow: "allow",
}

func NewPostgresStore(
func newPostgresStore(
host string,
port int,
user,
Expand Down
Loading

0 comments on commit 6c25262

Please sign in to comment.