Skip to content

Commit

Permalink
Make migration provider public again
Browse files Browse the repository at this point in the history
  • Loading branch information
olbrichattila committed May 29, 2024
1 parent 473ec69 commit 21f54c8
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 99 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ Currently the command line utility supports only SqLite, the build in solution s

```
migrationFilePath := "./migration"
migrationProvider, err := migrator.NewMigrationProvider("json")
MigrationProvider, err := migrator.NewMigrationProvider("json")
if err != nil {
panic("Error: " + err.Error())
}
err = migrator.Migrate(db, migrationProvider, migrationFilePath, count)
err = migrator.Migrate(db, MigrationProvider, migrationFilePath, count)
if err != nil {
panic("Error: " + err.Error())
}
Expand All @@ -75,12 +75,12 @@ if err != nil {
## Example rollback: (where the db is your *sql.DB)
```
migrationFilePath := "./migration"
migrationProvider, err := migrator.NewMigrationProvider("json", nil)
MigrationProvider, err := migrator.NewMigrationProvider("json", nil)
if err != nil {
panic("Error: " + err.Error())
}
err = migrator.Rollback(db, migrationProvider, migrationFilePath, count)
err = migrator.Rollback(db, MigrationProvider, migrationFilePath, count)
if err != nil {
panic("Error: " + err.Error())
}
Expand All @@ -90,12 +90,12 @@ if err != nil {
Refresh is when everithing rolled back and migrated from scratch
```
migrationFilePath := "./migration"
migrationProvider, err := migrator.NewMigrationProvider("json", nil)
MigrationProvider, err := migrator.NewMigrationProvider("json", nil)
if err != nil {
panic("Error: " + err.Error())
}
err = migrator.Refresh(db, migrationProvider, migrationFilePath)
err = migrator.Refresh(db, MigrationProvider, migrationFilePath)
if err != nil {
panic("Error: " + err.Error())
}
Expand All @@ -104,12 +104,12 @@ if err != nil {
## Migrate with database provider
```
migrationFilePath := "./migration"
migrationProvider, err := migrator.NewMigrationProvider("db", db)
MigrationProvider, err := migrator.NewMigrationProvider("db", db)
if err != nil {
panic("Error: " + err.Error())
}
err = migrator.Migrate(db, migrationProvider, migrationFilePath, count)
err = migrator.Migrate(db, MigrationProvider, migrationFilePath, count)
if err != nil {
panic("Error: " + err.Error())
}
Expand All @@ -118,12 +118,12 @@ if err != nil {
## Rollback with database provider
```
migrationFilePath := "./migration"
migrationProvider, err := migrator.NewMigrationProvider("db", db)
MigrationProvider, err := migrator.NewMigrationProvider("db", db)
if err != nil {
panic("Error: " + err.Error())
}
err = migrator.Rollback(db, migrationProvider, migrationFilePath, count)
err = migrator.Rollback(db, MigrationProvider, migrationFilePath, count)
if err != nil {
panic("Error: " + err.Error())
}
Expand All @@ -133,12 +133,12 @@ if err != nil {
Refresh is when everithing rolled back and migrated from scratch
```
migrationFilePath := "./migration"
migrationProvider, err := migrator.NewMigrationProvider("db", db)
MigrationProvider, err := migrator.NewMigrationProvider("db", db)
if err != nil {
panic("Error: " + err.Error())
}
err = migrator.Refresh(db, migrationProvider, migrationFilePath)
err = migrator.Refresh(db, MigrationProvider, migrationFilePath)
if err != nil {
panic("Error: " + err.Error())
}
Expand All @@ -160,12 +160,12 @@ Fetching the migratio report to a readable string:

```
migrationFilePath := "./migration"
migrationProvider, err := migrator.NewMigrationProvider("db", db)
MigrationProvider, err := migrator.NewMigrationProvider("db", db)
if err != nil {
panic("Error: " + err.Error())
}
report, err := migrator.Report(db, migrationProvider, migrationFilePath)
report, err := migrator.Report(db, MigrationProvider, migrationFilePath)
if err != nil {
panic("Error: " + err.Error())
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/cmd-run.go.bak
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
}
defer db.Close()

migrationProvider, err := migrator.NewMigrationProvider(provider, db)
MigrationProvider, err := migrator.NewMigrationProvider(provider, db)
if err != nil {
panic(err.Error())
}
Expand All @@ -40,13 +40,13 @@ func main() {

switch function {
case "migrate":
err = migrator.Migrate(db, migrationProvider, migrationFilePath, count)
err = migrator.Migrate(db, MigrationProvider, migrationFilePath, count)
case "rollback":
err = migrator.Rollback(db, migrationProvider, migrationFilePath, count)
err = migrator.Rollback(db, MigrationProvider, migrationFilePath, count)
case "refresh":
err = migrator.Refresh(db, migrationProvider, migrationFilePath)
err = migrator.Refresh(db, MigrationProvider, migrationFilePath)
case "report":
report, err := migrator.Report(db, migrationProvider, migrationFilePath)
report, err := migrator.Report(db, MigrationProvider, migrationFilePath)
if err != nil {
panic("Error: " + err.Error())
}
Expand Down
14 changes: 7 additions & 7 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type migration struct {
db *sql.DB
migrationProvider migrationProvider
MigrationProvider MigrationProvider
migrationFilePath string
}

Expand Down Expand Up @@ -42,7 +42,7 @@ func (m *migration) orderedMigrationFiles() ([]string, error) {
}

func (m *migration) executeSQLFile(fileName string) (bool, error) {
exists, err := m.migrationProvider.migrationExistsForFile(fileName)
exists, err := m.MigrationProvider.migrationExistsForFile(fileName)
if err != nil {
return false, err
}
Expand All @@ -59,13 +59,13 @@ func (m *migration) executeSQLFile(fileName string) (bool, error) {

err = m.executeSQL(string(content))
if err == nil {
err = m.migrationProvider.addToMigration(fileName)
err = m.MigrationProvider.addToMigration(fileName)
if err != nil {
return false, err
}
}

_ = m.migrationProvider.AddToMigrationReport(fileName, err)
_ = m.MigrationProvider.addToMigrationReport(fileName, err)

return true, err
}
Expand All @@ -74,7 +74,7 @@ func (m *migration) executeRollbackSQLFile(fileName string) error {
rollbackFileName, err := m.resolveRollbackFile(fileName)
if err != nil {
fmt.Printf("Skip rollback for %s as rollback file does not exists\n", fileName)
err := m.migrationProvider.removeFromMigration(fileName)
err := m.MigrationProvider.removeFromMigration(fileName)
if err != nil {
return err
}
Expand All @@ -90,13 +90,13 @@ func (m *migration) executeRollbackSQLFile(fileName string) error {

err = m.executeSQL(string(content))
if err == nil {
err = m.migrationProvider.removeFromMigration(fileName)
err = m.MigrationProvider.removeFromMigration(fileName)
if err != nil {
return err
}
}

err = m.migrationProvider.AddToMigrationReport(rollbackFileName, err)
err = m.MigrationProvider.addToMigrationReport(rollbackFileName, err)

return err
}
Expand Down
2 changes: 1 addition & 1 deletion migration_db_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (m *dbMigration) SetJSONFilePath(_ string) {
// dummy, not used in db version, need due to interface
}

func (m *dbMigration) AddToMigrationReport(fileName string, errorToLog error) error {
func (m *dbMigration) addToMigrationReport(fileName string, errorToLog error) error {
sql := fmt.Sprintf(`INSERT INTO migration_reports
(file_name, created_at, result_status, message)
VALUES (%s, %s, %s, %s)`,
Expand Down
2 changes: 1 addition & 1 deletion migration_json_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (m *jsonMigration) SetJSONFilePath(filePath string) {
m.jsonReporFileName = filePath + "/migration_reports.json"
}

func (m *jsonMigration) AddToMigrationReport(fileName string, errorToLog error) error {
func (m *jsonMigration) addToMigrationReport(fileName string, errorToLog error) error {
storeFileName := m.getJSONReportFileName()
message := "ok"
status := "success"
Expand Down
7 changes: 4 additions & 3 deletions migration_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import (
"fmt"
)

type migrationProvider interface {
// MigrationProvider is the base migrator interface
type MigrationProvider interface {
migrations(bool) ([]string, error)
addToMigration(string) error
removeFromMigration(string) error
migrationExistsForFile(string) (bool, error)
resetDate()
getJSONFileName() string
SetJSONFilePath(string)
AddToMigrationReport(string, error) error
addToMigrationReport(string, error) error
Report() (string, error)
}

// NewMigrationProvider returns a migration provider, which follows the provider type
// The provider type can be json or db, error returned if the type incorrectly provided
// db should be your database *sql.DB, which can be MySQL, Postgres, Sqlite or Firebird
func NewMigrationProvider(providerType string, db *sql.DB) (migrationProvider, error) {
func NewMigrationProvider(providerType string, db *sql.DB) (MigrationProvider, error) {
switch providerType {
case "json":
return newJSONMigration()
Expand Down
34 changes: 17 additions & 17 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ import (
// Rollback rolls back last migrated items or all if count is 0
func Rollback(
db *sql.DB,
migrationProvider migrationProvider,
MigrationProvider MigrationProvider,
migrationFilePath string,
count int,
) error {
return rollback(db, migrationProvider, migrationFilePath, count, false)
return rollback(db, MigrationProvider, migrationFilePath, count, false)
}

// Refresh runs a full rollback and migrate again
func Refresh(
db *sql.DB,
migrationProvider migrationProvider,
MigrationProvider MigrationProvider,
migrationFilePath string,
) error {
err := rollback(db, migrationProvider, migrationFilePath, 0, true)
err := rollback(db, MigrationProvider, migrationFilePath, 0, true)
if err != nil {
return err
}

return Migrate(db, migrationProvider, migrationFilePath, 0)
return Migrate(db, MigrationProvider, migrationFilePath, 0)
}

// Migrate execute migrations
func Migrate(
db *sql.DB,
migrationProvider migrationProvider,
MigrationProvider MigrationProvider,
migrationFilePath string,
count int,
) error {
m := newMigrator(db)
m.migrationFilePath = migrationFilePath
m.migrationProvider = migrationProvider
m.migrationProvider.SetJSONFilePath(migrationFilePath)
m.migrationProvider.resetDate()
m.MigrationProvider = MigrationProvider
m.MigrationProvider.SetJSONFilePath(migrationFilePath)
m.MigrationProvider.resetDate()
fileNames, err := m.orderedMigrationFiles()
if err != nil {
return err
Expand Down Expand Up @@ -74,15 +74,15 @@ func Migrate(
// Report return a report of the alredy executed migrations
func Report(
db *sql.DB,
migrationProvider migrationProvider,
MigrationProvider MigrationProvider,
migrationFilePath string,
) (string, error) {

m := newMigrator(db)
m.migrationFilePath = migrationFilePath
m.migrationProvider = migrationProvider
m.migrationProvider.SetJSONFilePath(migrationFilePath)
return m.migrationProvider.Report()
m.MigrationProvider = MigrationProvider
m.MigrationProvider.SetJSONFilePath(migrationFilePath)
return m.MigrationProvider.Report()
}

// AddNewMigrationFiles adds a new blank migration file and a rollback file
Expand All @@ -102,17 +102,17 @@ func AddNewMigrationFiles(migrationFilePath, customText string) error {

func rollback(
db *sql.DB,
migrationProvider migrationProvider,
MigrationProvider MigrationProvider,
migrationFilePath string,
count int,
isCompleteRollback bool,
) error {
var err error
m := newMigrator(db)
m.migrationFilePath = migrationFilePath
m.migrationProvider = migrationProvider
m.migrationProvider.SetJSONFilePath(migrationFilePath)
migrations, err := m.migrationProvider.migrations(!isCompleteRollback)
m.MigrationProvider = MigrationProvider
m.MigrationProvider.SetJSONFilePath(migrationFilePath)
migrations, err := m.MigrationProvider.migrations(!isCompleteRollback)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 21f54c8

Please sign in to comment.