-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrator.go
144 lines (112 loc) · 3.26 KB
/
migrator.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package frame
import (
"context"
"errors"
"fmt"
"gorm.io/gorm"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
type migrator struct {
service *Service
}
func (m *migrator) DB(ctx context.Context) *gorm.DB {
return m.service.DB(ctx, false)
}
func (m *migrator) scanForNewMigrations(ctx context.Context, migrationsDirPath string) error {
// Get a list of migration files
files, err := filepath.Glob(migrationsDirPath + "/*.sql")
if err != nil {
return err
}
sort.Strings(files)
for _, file := range files {
filename := filepath.Base(file)
filename = strings.Replace(filename, ".sql", "", 1)
migrationPatch, err := os.ReadFile(file)
if err != nil {
log.Printf("scanForNewMigrations -- Problem reading migration file content : %s", err)
continue
}
err = m.saveNewMigrations(ctx, filename, string(migrationPatch))
if err != nil {
log.Printf("scanForNewMigrations -- new migration :%s could not be processed because: %s", file, err)
return err
}
}
return nil
}
func (m *migrator) saveNewMigrations(ctx context.Context, filename string, migrationPatch string) error {
migration := Migration{}
err := m.DB(ctx).Model(&migration).First(&migration, "name = ?", filename).Error
if err != nil {
if !DBErrorIsRecordNotFound(err) {
return err
}
migration := Migration{
Name: filename,
Patch: migrationPatch,
}
err = m.DB(ctx).Create(&migration).Error
if err != nil {
return err
}
return nil
}
if !migration.AppliedAt.Valid && migration.Patch != migrationPatch {
err := m.DB(ctx).Model(&migration).Update("patch", migrationPatch).Error
if err != nil {
return err
}
}
return nil
}
func (m *migrator) applyNewMigrations(ctx context.Context) error {
var unAppliedMigrations []*Migration
err := m.DB(ctx).Where("applied_at IS NULL").Find(&unAppliedMigrations).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Printf("applyNewMigrations -- No migrations found to be applied ")
return nil
}
return err
}
for _, migration := range unAppliedMigrations {
if err := m.DB(ctx).Exec(migration.Patch).Error; err != nil {
return err
}
err := m.DB(ctx).Model(migration).Update("applied_at", time.Now()).Error
if err != nil {
return err
}
log.Printf("applyNewMigrations -- Successfully applied the file : %v", fmt.Sprintf("%s.sql", migration.Name))
}
return nil
}
// MigrateDatastore finds missing migrations and records them in the database
func (s *Service) MigrateDatastore(ctx context.Context, migrationsDirPath string, migrations ...any) error {
if migrationsDirPath == "" {
migrationsDirPath = "./migrations/0001"
}
migrations = append([]any{&Migration{}}, migrations...)
// Migrate the schema
err := s.DB(ctx, false).AutoMigrate(migrations...)
if err != nil {
s.L(ctx).WithError(err).Error("MigrateDatastore -- couldn't automigrate")
return err
}
migrationExecutor := migrator{service: s}
if err := migrationExecutor.scanForNewMigrations(ctx, migrationsDirPath); err != nil {
log.Printf("MigrateDatastore -- Error scanning for new migrations : %s ", err)
return err
}
if err := migrationExecutor.applyNewMigrations(ctx); err != nil {
log.Printf("MigrateDatastore -- There was an error applying migrations : %s ", err)
return err
}
return nil
}