Skip to content

Commit

Permalink
add latest migration check
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandremr01 committed Apr 7, 2022
1 parent 90f7f31 commit 3de7480
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 48 deletions.
94 changes: 55 additions & 39 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,82 +28,98 @@ func (a *apply) Execute(cmd *cobra.Command, args []string) {
driverName = args[1]
}

err := a.execute(folderName, driverName)
if err != nil {
log.Fatal(err)
}
}

func (a *apply) execute(folderName, driverName string) error {
driver, err := drivers.GetDriver(driverName)
if err != nil {
log.Fatalf(err.Error())
return err
}

fmt.Printf("Driver %s started\n", driverName)

err = driver.Begin(a.Database)
if err != nil {
log.Fatalf("could not connect to database: %s", err)
}
return driver.ExecuteTransaction(a.Database, func() error {
previousMigrationNumber, err := a.getPreviousMigrationNumber(driver)
if err != nil {
return err
}

hasTables, err := driver.HasBaseTable()
if err != nil {
log.Fatalf(err.Error())
}
if !hasTables {
fmt.Printf("Installing mgr8 into the database...\n")
err := driver.CreateBaseTable()
latestMigrationNumber, err := a.runFolderMigrations(folderName, previousMigrationNumber, driver)
if err != nil {
log.Fatalf(err.Error())
return err
}
}

previousMigrationNumber, err := driver.GetLatestMigration()
if latestMigrationNumber <= previousMigrationNumber {
return nil
}

return driver.UpdateLatestMigration(latestMigrationNumber)
})
}

func (a *apply) runFolderMigrations(folderName string, previousMigrationNumber int, driver drivers.Driver) (int, error) {
latestMigrationNumber := 0
items, err := ioutil.ReadDir(folderName)
if err != nil {
log.Fatalf("%s", err)
return 0, err
}

latestMigrationNumber := 0
items, _ := ioutil.ReadDir(folderName)
for _, item := range items {
itemMigrationNumber, err := a.getMigrationNumber(item.Name())
if err != nil {
continue
}
if itemMigrationNumber > latestMigrationNumber {
latestMigrationNumber = itemMigrationNumber
}
if itemMigrationNumber <= previousMigrationNumber {
continue
}
if itemMigrationNumber > latestMigrationNumber {
latestMigrationNumber = itemMigrationNumber
err = a.applyMigrationScript(driver, path.Join(folderName, item.Name()))
if err != nil {
return 0, err
}
a.applyMigrationScript(driver, path.Join(folderName, item.Name()))
}

err = driver.UpdateLatestMigration(latestMigrationNumber)
if err != nil {
log.Fatalf("%s", err)
}

err = driver.Commit()
if err != nil {
log.Fatalf("could not commit transaction: %s", err)
}
return latestMigrationNumber, nil
}

func (a *apply) getMigrationNumber(itemName string) (int, error) {
itemNameParts := strings.Split(itemName, "_")
migrationVersionStr := itemNameParts[0]
migrationVersion, err := strconv.Atoi(migrationVersionStr)
func (a *apply) getPreviousMigrationNumber(driver drivers.Driver) (int, error) {
hasTables, err := driver.HasBaseTable()
if err != nil {
return 0, err
}
return migrationVersion, nil
if hasTables {
return driver.GetLatestMigration()
}
fmt.Printf("Installing mgr8 into the database...\n")
return 0, driver.CreateBaseTable()
}

func (a *apply) applyMigrationScript(driver drivers.Driver, scriptName string) {
func (a *apply) applyMigrationScript(driver drivers.Driver, scriptName string) error {
fmt.Printf("Applying file %s\n", scriptName)
content, err := os.ReadFile(scriptName)
if err != nil {
log.Fatal("could not read from file")
return fmt.Errorf("could not read from file: %s", err)
}

statements := strings.Split(string(content), ";")
err = driver.Execute(statements)
if err != nil {
log.Fatalf("could not execute transaction: %s", err)
return fmt.Errorf("could not execute transaction: %s", err)
}
return nil
}

func (a *apply) getMigrationNumber(itemName string) (int, error) {
itemNameParts := strings.Split(itemName, "_")
migrationVersionStr := itemNameParts[0]
migrationVersion, err := strconv.Atoi(migrationVersionStr)
if err != nil {
return 0, err
}
return migrationVersion, nil
}
3 changes: 1 addition & 2 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
)

type Driver interface {
Begin(url string) error
Commit() error
ExecuteTransaction(url string, f func() error) error

Execute(statements []string) error
GetLatestMigration() (int, error)
Expand Down
16 changes: 9 additions & 7 deletions drivers/postgres/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package postgres

import (
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"log"
Expand Down Expand Up @@ -30,7 +29,7 @@ func (p *postgresDriver) Execute(statements []string) error {
return nil
}

func (p *postgresDriver) Begin(url string) error {
func (p *postgresDriver) ExecuteTransaction(url string, f func() error) error {
db, err := sqlx.Connect("postgres", url)
if err != nil {
log.Fatalln(err)
Expand All @@ -42,13 +41,16 @@ func (p *postgresDriver) Begin(url string) error {
}

p.tx = tx
return nil
}

func (p *postgresDriver) Commit() error {
if p.tx == nil {
return fmt.Errorf("no transaction running")
err = f()
if err != nil {
err2 := p.tx.Rollback()
if err2 != nil {
return err2
}
return err
}

return p.tx.Commit()
}

Expand Down

0 comments on commit 3de7480

Please sign in to comment.