aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/models/migration.go15
-rw-r--r--internal/models/migration_test.go7
-rw-r--r--internal/repositories/migration_fs_repo.go26
3 files changed, 23 insertions, 25 deletions
diff --git a/internal/models/migration.go b/internal/models/migration.go
index a449c44..f647a1b 100644
--- a/internal/models/migration.go
+++ b/internal/models/migration.go
@@ -2,7 +2,6 @@ package models
import (
"fmt"
- "strings"
"time"
)
@@ -21,19 +20,13 @@ func NewMigration(timestamp time.Time, message string) *Migration {
return &Migration{Version: version, Message: message, Applied: false}
}
-func (m *Migration) NormalizedMessage() string {
- lowercaseMessage := strings.ToLower(m.Message)
- trimmedMessage := strings.TrimSpace(lowercaseMessage)
- return strings.ReplaceAll(trimmedMessage, " ", "_")
-}
-
func (m *Migration) String() string {
- checked := " "
+ checkmark := " "
if m.Applied {
- checked = "x"
+ checkmark = "x"
}
- message := m.NormalizedMessage()
+ message := m.Message
if len(message) > 0 {
message = fmt.Sprintf("- %s ", message)
}
@@ -42,6 +35,6 @@ func (m *Migration) String() string {
"%s %s- [%s]",
m.Version,
message,
- checked,
+ checkmark,
)
}
diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go
index b25f137..8d1fb06 100644
--- a/internal/models/migration_test.go
+++ b/internal/models/migration_test.go
@@ -27,14 +27,7 @@ func TestAppliedNewMigration(t *testing.T) {
assert.Equal(t, m.String(), "20200101000000 - test_message - [x]")
}
-func TestNewMigrationMessageIsNormalized(t *testing.T) {
- m := models.NewMigration(timestamp, " test MESSAGE ")
- assert.Equal(t, m.NormalizedMessage(), "test_message")
-}
-
func TestEmptyMessageMigration(t *testing.T) {
m := models.NewMigration(timestamp, "")
-
- assert.Equal(t, "", m.NormalizedMessage())
assert.Equal(t, "20200101000000 - [ ]", m.String())
}
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index e064c72..2add285 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -41,11 +41,8 @@ func NewMigrationFsRepo(migrationsDirPath string) (*MigrationFsRepo, error) {
return &MigrationFsRepo{migrationsDirPath: migrationsDirPath}, nil
}
-func (mr *MigrationFsRepo) Create(m *models.Migration) error {
- path := filepath.Join(
- mr.migrationsDirPath,
- fmt.Sprintf("%s_%s", m.Version, m.NormalizedMessage()),
- )
+func (mr *MigrationFsRepo) Create(migration *models.Migration) error {
+ path := filepath.Join(mr.migrationsDirPath, mr.migrationDirname(migration))
_, err := os.Create(filepath.Join(path, "upgrade.sql"))
if err != nil {
@@ -89,7 +86,7 @@ func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) {
func (mr *MigrationFsRepo) ReadUpgradeScript(migration *models.Migration) (string, error) {
upgradeScriptPath := filepath.Join(
mr.migrationsDirPath,
- fmt.Sprintf("%s_%s", migration.Version, migration.NormalizedMessage()),
+ mr.migrationDirname(migration),
"upgrade.sql",
)
return mr.readScriptContents(upgradeScriptPath)
@@ -98,7 +95,7 @@ func (mr *MigrationFsRepo) ReadUpgradeScript(migration *models.Migration) (strin
func (mr *MigrationFsRepo) ReadDowngradeScript(migration *models.Migration) (string, error) {
downgradeScriptPath := filepath.Join(
mr.migrationsDirPath,
- fmt.Sprintf("%s_%s", migration.Version, migration.NormalizedMessage()),
+ mr.migrationDirname(migration),
"downgrade.sql",
)
return mr.readScriptContents(downgradeScriptPath)
@@ -112,3 +109,18 @@ func (mr *MigrationFsRepo) readScriptContents(scriptPath string) (string, error)
return string(contents), nil
}
+
+// migrationDirname creates the directory name that
+// should be used for this migration.
+func (mr *MigrationFsRepo) migrationDirname(migration *models.Migration) string {
+ return fmt.Sprintf("%s_%s", migration.Version, mr.normalizeMessage(migration))
+}
+
+// normalizeMessage normalizes the Message of a migration
+// to be filesystem friendly.
+func (mr *MigrationFsRepo) normalizeMessage(migration *models.Migration) string {
+ message := strings.ToLower(migration.Message)
+ message = strings.TrimSpace(message)
+ message = strings.ReplaceAll(message, " ", "_")
+ return message
+}