Skip to content

Commit 962876f

Browse files
authored
Merge pull request #24 from eure/topic/check-valid-timestamp
fix: check timestamp in sync
2 parents d1a3ada + 3f65d2f commit 962876f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

core/migration.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package core
33
import (
44
"io/ioutil"
55
"os"
6+
"strconv"
7+
"time"
68

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

70+
func (m *Migration) IsValidTimestamp() bool {
71+
_, err := time.Parse("20060102150405", strconv.FormatUint(m.version, 10))
72+
return err == nil
73+
}
74+
6875
//////////////////////////////
6976
// Migrations
7077
//////////////////////////////

core/service.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,18 @@ func (s *Service) Sync() error {
256256
if err := s.apply(); err != nil {
257257
return err
258258
}
259+
if len(s.data) == 0 {
260+
return nil
261+
}
259262

260263
version := s.driver.Version()
261-
264+
// 一番目のマイグレーションファイルが Timestamp 形式だったらそれ以降は Timestamp 扱いにする
265+
isTimeStampVer := s.data[0].IsValidTimestamp()
262266
for _, mig := range s.data {
267+
if isTimeStampVer != mig.IsValidTimestamp() {
268+
return errors.New("invalid version format")
269+
}
270+
263271
if count := version.Count(mig.version); count == 0 {
264272
// gets current index of migrations
265273
idx := s.data.index(*mig)

0 commit comments

Comments
 (0)