Skip to content

Commit

Permalink
Merge pull request #3 from cjlapao/improve-migrations
Browse files Browse the repository at this point in the history
improved migrations
  • Loading branch information
cjlapao committed Nov 21, 2022
2 parents f710234 + a4ae2a2 commit 19905c9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
10 changes: 10 additions & 0 deletions helpers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package helpers

import "strings"

func NormalizeName(name string) string {
name = strings.ReplaceAll(name, " ", "_")
name = strings.ReplaceAll(name, "-", "_")

return strings.ToLower(name)
}
7 changes: 5 additions & 2 deletions migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/cjlapao/common-go-database/helpers"
"github.com/cjlapao/common-go/guard"
"github.com/cjlapao/common-go/log"
"github.com/elliotchance/orderedmap/v2"
Expand Down Expand Up @@ -45,7 +46,7 @@ func NewMigrationService(repo MigrationsRepository) *SqlMigrationService {

func (m *SqlMigrationService) WasApplied(name string) bool {
for _, migration := range m.AppliedMigrations {
if strings.EqualFold(name, migration.Name) {
if strings.EqualFold(helpers.NormalizeName(name), helpers.NormalizeName(migration.Name)) {
m.logger.Info("Migration %v was already applied on %v", migration.Name, migration.ExecutedOn.Format(time.RFC3339))
return true
}
Expand All @@ -69,7 +70,7 @@ func (m *SqlMigrationService) Run() error {

for el := m.Migrations.Front(); el != nil; el = el.Next() {
migration := MigrationEntity{
Name: el.Value.Name(),
Name: strings.ReplaceAll(el.Value.Name(), " ", "_"),
Status: false,
}
if m.WasApplied(el.Value.Name()) {
Expand All @@ -81,6 +82,8 @@ func (m *SqlMigrationService) Run() error {
if !el.Value.Down() {
logger.Error("there was an error applying migration down for %v, database might be inconsistent", el.Value.Name())
}
// Stopping migrations as they need to be run in order
break
} else {
logger.Info("Migration %v was applied successfully", el.Value.Name())
migration.Status = true
Expand Down
3 changes: 2 additions & 1 deletion sql/sql_migration_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/cjlapao/common-go-database/helpers"
"github.com/cjlapao/common-go-database/migrations"
"github.com/cjlapao/common-go/log"
"github.com/google/uuid"
Expand Down Expand Up @@ -104,7 +105,7 @@ VALUES(?, ?, ?, ?)
`,
migration.ID,
migration.ExecutedOn,
migration.Name,
helpers.NormalizeName(migration.Name),
migration.Status,
)

Expand Down

0 comments on commit 19905c9

Please sign in to comment.