Skip to content

Commit

Permalink
fix: check timestamp in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
emahiro committed Dec 6, 2023
1 parent d1a3ada commit 3f65d2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions core/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package core
import (
"io/ioutil"
"os"
"strconv"
"time"

"github.com/eure/kamimai/internal/cast"
)
Expand Down Expand Up @@ -65,6 +67,11 @@ func (m *Migration) IsValid() bool {
return m.version > 0 && m.name != ""
}

func (m *Migration) IsValidTimestamp() bool {
_, err := time.Parse("20060102150405", strconv.FormatUint(m.version, 10))
return err == nil
}

//////////////////////////////
// Migrations
//////////////////////////////
Expand Down
10 changes: 9 additions & 1 deletion core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,18 @@ func (s *Service) Sync() error {
if err := s.apply(); err != nil {
return err
}
if len(s.data) == 0 {
return nil
}

version := s.driver.Version()

// 一番目のマイグレーションファイルが Timestamp 形式だったらそれ以降は Timestamp 扱いにする
isTimeStampVer := s.data[0].IsValidTimestamp()
for _, mig := range s.data {
if isTimeStampVer != mig.IsValidTimestamp() {
return errors.New("invalid version format")
}

if count := version.Count(mig.version); count == 0 {
// gets current index of migrations
idx := s.data.index(*mig)
Expand Down

0 comments on commit 3f65d2f

Please sign in to comment.