aboutsummaryrefslogtreecommitdiffstats
path: root/internal/models/migration.go
blob: ed480737d54bfbf5fa2a7d37ee77fe615c3e2cca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package models

import (
	"fmt"
	"strings"
	"time"
)

type Migration struct {
	Version string
	Message string
	Applied bool
}

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,
	}
}

func NewSequentialMigration(version uint64, message string) *Migration {
	return &Migration{
		Version: fmt.Sprintf("%03d", version),
		Message: message,
		Applied: false,
	}
}

// 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.NormalizedMessage())
}