diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-02 11:07:30 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-02 11:07:30 -0500 |
| commit | 571bc9bf2efa841b29bf036f4d307664bfbe155d (patch) | |
| tree | 41fb3a459db99d1f0ec2ed2e00d4ca580a5d0c46 /internal/models | |
| parent | Update readme (diff) | |
| parent | Remove Latest() from migration fs repo in favor of using List on service (diff) | |
| download | cli-571bc9bf2efa841b29bf036f4d307664bfbe155d.zip | |
Merge pull request #1 from eugenetriguba/0.3.0
Diffstat (limited to 'internal/models')
| -rw-r--r-- | internal/models/migration.go | 40 | ||||
| -rw-r--r-- | internal/models/migration_test.go | 38 |
2 files changed, 40 insertions, 38 deletions
diff --git a/internal/models/migration.go b/internal/models/migration.go index f647a1b..b63fec9 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -11,30 +11,26 @@ type Migration struct { Applied bool } -func NewMigration(timestamp time.Time, message string) *Migration { - version := fmt.Sprintf( - "%d%02d%02d%02d%02d%02d", - timestamp.Year(), timestamp.Month(), timestamp.Day(), - timestamp.Hour(), timestamp.Minute(), timestamp.Second(), - ) - return &Migration{Version: version, Message: message, Applied: false} -} - -func (m *Migration) String() string { - checkmark := " " - if m.Applied { - checkmark = "x" +func NewTimestampMigration(version time.Time, message string) *Migration { + return &Migration{ + Version: fmt.Sprintf( + "%d%02d%02d%02d%02d%02d", + version.Year(), version.Month(), version.Day(), + version.Hour(), version.Minute(), version.Second(), + ), + Message: message, + Applied: false, } +} - message := m.Message - if len(message) > 0 { - message = fmt.Sprintf("- %s ", message) +func NewSequentialMigration(version uint64, message string) *Migration { + return &Migration{ + Version: fmt.Sprintf("%03d", version), + Message: message, + Applied: false, } +} - return fmt.Sprintf( - "%s %s- [%s]", - m.Version, - message, - checkmark, - ) +func (m *Migration) Name() string { + return fmt.Sprintf("%s_%s", m.Version, m.Message) } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index 8d1fb06..b6d3205 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -1,33 +1,39 @@ package models_test import ( + "fmt" "testing" "time" - "gotest.tools/v3/assert" - "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/checkmate/check" ) -var timestamp = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) +func TestNewTimestampMigration(t *testing.T) { + var timestamp = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) -func TestNewMigrationInit(t *testing.T) { - m := models.NewMigration(timestamp, "test message") + m := models.NewTimestampMigration(timestamp, "test message") - assert.Equal(t, m.Version, "20200101000000") - assert.Equal(t, m.Message, "test message") - assert.Equal(t, m.Applied, false) + check.Equal(t, m.Version, "20200101000000") + check.Equal(t, m.Message, "test message") + check.Equal(t, m.Applied, false) } -func TestAppliedNewMigration(t *testing.T) { - m := models.NewMigration(timestamp, "test_message") +func TestNewSequentialMigration(t *testing.T) { + m := models.NewSequentialMigration(1, "test message") + + check.Equal(t, m.Version, "001") + check.Equal(t, m.Message, "test message") + check.Equal(t, m.Applied, false) + + m = models.NewSequentialMigration(100, "test message") - assert.Equal(t, m.String(), "20200101000000 - test_message - [ ]") - m.Applied = true - assert.Equal(t, m.String(), "20200101000000 - test_message - [x]") + check.Equal(t, m.Version, "100") + check.Equal(t, m.Message, "test message") + check.Equal(t, m.Applied, false) } -func TestEmptyMessageMigration(t *testing.T) { - m := models.NewMigration(timestamp, "") - assert.Equal(t, "20200101000000 - [ ]", m.String()) +func TestName(t *testing.T) { + m := models.NewSequentialMigration(1, "test message") + check.Equal(t, m.Name(), fmt.Sprintf("%s_%s", m.Version, m.Message)) } |