-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
189 lines (158 loc) · 5.06 KB
/
version.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package clpgmigrate
import (
"context"
"fmt"
"io"
"strings"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/postgres"
"github.com/crewlinker/clgo/clconfig"
"github.com/crewlinker/clgo/clpostgres"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/samber/lo"
"go.uber.org/fx"
"go.uber.org/zap"
)
// alwaysValidateMigrateDir can be provided as a migration dir to disable checksum validation. This
// is useful for unit tests that don't need this feature for quick iteration.
type alwaysValidateMigrateDir struct{ migrate.Dir }
// Checksum implements the logic for re-calculate the directories checksum. But instead we return
// the checksum in the checksum file to make the validation logic always pass.
func (dir alwaysValidateMigrateDir) Checksum() (migrate.HashFile, error) {
file, err := dir.Open(migrate.HashFileName)
if err != nil {
return nil, fmt.Errorf("failed to open hash file: %w", err)
}
defer file.Close()
byt, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("failed to read all from hash file: %w", err)
}
var fh migrate.HashFile
if err := fh.UnmarshalText(byt); err != nil {
return nil, fmt.Errorf("failed to unmarshal hash file: %w", err)
}
return fh, nil
}
// VersionMigrated configures the di for testing with a temporary database and auto-migration of a directory.
func VersionMigrated(noChecksumValidate bool) fx.Option {
return fx.Options(
// provide the environment configuration
clconfig.Provide[Config](strings.ToUpper(moduleName)+"_"),
// Provide migrater which will now always run before connecting using versioned steps
fx.Provide(fx.Annotate(
NewVersionMigrater,
fx.As(new(clpostgres.Migrater)),
fx.OnStart(func(ctx context.Context, m clpostgres.Migrater) error { return m.Migrate(ctx) }),
fx.OnStop(func(ctx context.Context, m clpostgres.Migrater) error { return m.Reset(ctx) }),
fx.ParamTags(``, ``, `name:"rw"`, `name:"ro"`)),
),
// For tests we want temporary database and auto-migratino
fx.Decorate(func(c Config) Config {
c.TemporaryDatabase = true
c.AutoMigration = true
return c
}),
// decorate dir to disable checksum
fx.Decorate(func(dir migrate.Dir) (migrate.Dir, error) {
if noChecksumValidate {
return alwaysValidateMigrateDir{Dir: dir}, nil
}
return dir, nil
}),
)
}
// VersionMigrater allows programmatic migration of a database schema using versioned sql steps. Mostly used
// in testing and local development to provide fully isolated databases.
type VersionMigrater struct {
baseMigrater
dir migrate.Dir
}
// NewVersionMigrater inits the migrater.
func NewVersionMigrater(
cfg Config,
logs *zap.Logger,
rwcfg *pgxpool.Config,
rocfg *pgxpool.Config,
dir migrate.Dir,
) (*VersionMigrater, error) {
mig := &VersionMigrater{
baseMigrater: baseMigrater{
cfg: cfg,
dbcfg: rwcfg,
logs: logs.Named("version_migrater"),
}, dir: dir,
}
return mig, mig.baseMigrater.init(rocfg)
}
// Migrate initializes the schema.
func (m VersionMigrater) Migrate(ctx context.Context) error {
if err := m.baseMigrater.setup(ctx); err != nil {
return err
}
if !m.cfg.AutoMigration {
m.logs.Info("auto-migration disabled, expect database to be provisioned already")
return nil // without auto-migration enabled, there is nothing left to do
}
checksum, err := m.dir.Checksum()
if err != nil {
return fmt.Errorf("failed to determine local dir checksum: %w", err)
}
m.logs.Info("auto-migrating from migrate dir",
zap.String("checksum", checksum.Sum()))
sqldb := stdlib.OpenDB(*m.dbcfg.ConnConfig)
defer sqldb.Close()
if err := sqldb.PingContext(ctx); err != nil {
return fmt.Errorf("failed to ping connection: %w", err)
}
drv, err := postgres.Open(sqldb)
if err != nil {
return fmt.Errorf("failed to init atlas driver: %w", err)
}
exec, err := migrate.NewExecutor(drv, m.dir, migrate.NopRevisionReadWriter{},
migrate.WithLogger(migrateLogger{logs: m.logs.Named("executer")}))
if err != nil {
return fmt.Errorf("failed to init executor: %w", err)
}
if err := exec.ExecuteN(ctx, -1); err != nil {
return fmt.Errorf("failed to execute migrations: %w", err)
}
return nil
}
// Reset drops the migrated state.
func (m VersionMigrater) Reset(ctx context.Context) error {
return m.baseMigrater.reset(ctx)
}
// migrateLogger logger.
type migrateLogger struct {
logs *zap.Logger
}
func (l migrateLogger) Log(e migrate.LogEntry) {
switch entry := e.(type) {
case migrate.LogExecution:
l.logs.Info("execution",
zap.String("from", entry.From),
zap.String("to", entry.To),
zap.Strings("files", lo.Map(entry.Files, func(f migrate.File, _ int) string {
return f.Name()
})))
case migrate.LogFile:
l.logs.Info("file",
zap.String("desc", entry.File.Desc()),
zap.String("name", entry.File.Name()),
zap.Int("skip", entry.Skip),
zap.String("version", entry.Version),
)
case migrate.LogStmt:
l.logs.Info("statement",
zap.String("sql", entry.SQL),
)
case migrate.LogDone:
l.logs.Info("done")
case migrate.LogError:
l.logs.Error("error",
zap.Error(entry.Error),
zap.String("sql", entry.SQL))
}
}