aboutsummaryrefslogtreecommitdiffstats
path: root/internal/models
diff options
context:
space:
mode:
Diffstat (limited to 'internal/models')
-rw-r--r--internal/models/migration.go13
-rw-r--r--internal/models/migration_test.go32
2 files changed, 41 insertions, 4 deletions
diff --git a/internal/models/migration.go b/internal/models/migration.go
index b63fec9..ed48073 100644
--- a/internal/models/migration.go
+++ b/internal/models/migration.go
@@ -2,6 +2,7 @@ package models
import (
"fmt"
+ "strings"
"time"
)
@@ -31,6 +32,16 @@ func NewSequentialMigration(version uint64, message string) *Migration {
}
}
+// NormalizedMessage gives back the Message in lowercase,
+// leading and trailing whitespace removed, and spaces
+// replaced with underscores.
+func (m Migration) NormalizedMessage() string {
+ message := strings.ToLower(m.Message)
+ message = strings.TrimSpace(message)
+ message = strings.ReplaceAll(message, " ", "_")
+ return message
+}
+
func (m *Migration) Name() string {
- return fmt.Sprintf("%s_%s", m.Version, m.Message)
+ return fmt.Sprintf("%s_%s", m.Version, m.NormalizedMessage())
}
diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go
index b6d3205..809887f 100644
--- a/internal/models/migration_test.go
+++ b/internal/models/migration_test.go
@@ -1,7 +1,6 @@
package models_test
import (
- "fmt"
"testing"
"time"
@@ -34,6 +33,33 @@ func TestNewSequentialMigration(t *testing.T) {
}
func TestName(t *testing.T) {
- m := models.NewSequentialMigration(1, "test message")
- check.Equal(t, m.Name(), fmt.Sprintf("%s_%s", m.Version, m.Message))
+ type test struct {
+ migration *models.Migration
+ expectedName string
+ }
+
+ testCases := []test{
+ // Underscores are used to separate version and message.
+ {
+ migration: models.NewSequentialMigration(1, "test_message"),
+ expectedName: "001_test_message",
+ },
+ {
+ migration: models.NewTimestampMigration(time.Date(1234, 1, 2, 3, 4, 5, 6, time.UTC), "test_message"),
+ expectedName: "12340102030405_test_message",
+ },
+ // Leading/Trailing spaces trimmed, lowercased, and spaces converted to underscores.
+ {
+ migration: models.NewSequentialMigration(1, " teST mesSAGE "),
+ expectedName: "001_test_message",
+ },
+ {
+ migration: models.NewTimestampMigration(time.Date(1234, 1, 2, 3, 4, 5, 6, time.UTC), " teST mesSAGE "),
+ expectedName: "12340102030405_test_message",
+ },
+ }
+
+ for _, tc := range testCases {
+ check.Equal(t, tc.migration.Name(), tc.expectedName)
+ }
}