/
migrations.go
78 lines (67 loc) · 1.88 KB
/
migrations.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package repository
import (
"github.com/MashinIvan/go-migrator/internal/models"
"gorm.io/gorm"
"hash/fnv"
"time"
)
type Order string
const (
OrderASC Order = "ASC"
OrderDESC Order = "DESC"
)
func GetMigrationsSorted(db *gorm.DB, order Order) ([]models.MigrationModel, error) {
var migrations []models.MigrationModel
err := db.Order("rank " + string(order)).Find(&migrations).Error
return migrations, err
}
func UpdateMigrationState(db *gorm.DB, model *models.MigrationModel, state models.MigrationState) error {
return db.Model(model).Update("state", state).Error
}
func UpdateMigrationStateExecuted(db *gorm.DB, model *models.MigrationModel, state models.MigrationState, checksum string) error {
now := time.Now().UTC()
return db.Model(model).Updates(models.MigrationModel{
ExecutedOn: &now,
State: state,
Checksum: checksum,
}).Error
}
type SaveMigrationRequest struct {
Rank int
Type string
Version string
Description string
State models.MigrationState
}
func SaveMigration(db *gorm.DB, request SaveMigrationRequest) (models.MigrationModel, error) {
h := fnv.New32a()
_, _ = h.Write([]byte(request.Type + request.Version))
migration := models.MigrationModel{
Id: h.Sum32(),
Rank: request.Rank,
Type: request.Type,
Version: request.Version,
Description: request.Description,
RegisteredOn: time.Now().UTC(),
State: request.State,
}
return migration, db.Save(&migration).Error
}
func HasMigrationsTable(db *gorm.DB) bool {
return db.Migrator().HasTable(models.MigrationModel{}.TableName())
}
func CreateMigrationsTable(db *gorm.DB) error {
return db.Exec(`
CREATE TABLE IF NOT EXISTS migrations (
id NUMERIC PRIMARY KEY,
rank BIGINT,
type TEXT,
version TEXT,
description TEXT,
registered_on TIMESTAMPTZ,
executed_on TIMESTAMPTZ,
checksum TEXT,
state TEXT
)
`).Error
}