/
change_history_manager.go
334 lines (273 loc) · 10.5 KB
/
change_history_manager.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package db_repo
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/jinzhu/gorm"
"github.com/justtrackio/gosoline/pkg/appctx"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/coffin"
"github.com/justtrackio/gosoline/pkg/funk"
"github.com/justtrackio/gosoline/pkg/log"
"github.com/justtrackio/gosoline/pkg/mdl"
)
type changeHistoryManagerSettings struct {
ChangeAuthorField string `cfg:"change_author_column"`
TableSuffix string `cfg:"table_suffix" default:"history"`
}
type ChangeHistoryManager struct {
orm *gorm.DB
logger log.Logger
settings *changeHistoryManagerSettings
models []ModelBased
}
type changeHistoryManagerAppctxKey int
func ProvideChangeHistoryManager(ctx context.Context, config cfg.Config, logger log.Logger) (*ChangeHistoryManager, error) {
return appctx.Provide(ctx, changeHistoryManagerAppctxKey(0), func() (*ChangeHistoryManager, error) {
return NewChangeHistoryManager(config, logger)
})
}
func NewChangeHistoryManager(config cfg.Config, logger log.Logger) (*ChangeHistoryManager, error) {
orm, err := NewOrm(config, logger)
if err != nil {
return nil, fmt.Errorf("can not create orm: %w", err)
}
settings := &changeHistoryManagerSettings{}
config.UnmarshalKey("change_history", settings)
return &ChangeHistoryManager{
logger: logger.WithChannel("change_history_manager"),
orm: orm,
settings: settings,
}, nil
}
func (c *ChangeHistoryManager) addModels(models ...ModelBased) {
c.models = append(c.models, models...)
}
func (c *ChangeHistoryManager) RunMigrations() error {
cfn := coffin.New()
cfn.Go(func() error {
for _, model := range funk.UniqByType(c.models) {
model := model
cfn.Go(func() error {
if err := c.RunMigration(model); err != nil {
return fmt.Errorf("can not run migration for model %T: %w", model, err)
}
return nil
})
}
return nil
})
return cfn.Wait()
}
func (c *ChangeHistoryManager) RunMigration(model ModelBased) error {
originalTable := c.buildOriginalTableMetadata(model)
historyTable := c.buildHistoryTableMetadata(model, originalTable)
if err := c.executeMigration(originalTable, historyTable); err != nil {
return fmt.Errorf("cannot execute change history migration: %w", err)
}
if err := c.validateSchema(originalTable.tableName, historyTable.tableName); err != nil {
return fmt.Errorf("error during schema validation: %w", err)
}
return nil
}
func (c *ChangeHistoryManager) validateSchema(originalTable, historyTable string) error {
qry := `
select original.TABLE_NAME, original.COLUMN_NAME, original.DATA_TYPE, history_entries.DATA_TYPE
from (select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from information_schema.columns
where table_schema = '%s' and TABLE_NAME = '%s') original
left join (select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from information_schema.columns
where table_schema = '%s' and TABLE_NAME = '%s') as history_entries
on original.COLUMN_NAME = history_entries.COLUMN_NAME
where original.DATA_TYPE != coalesce(history_entries.DATA_TYPE, '');`
db := c.orm.Raw("select database();")
if db.Error != nil {
return fmt.Errorf("unable to fetch database name: %w", db.Error)
}
var dbName string
err := db.Row().Scan(&dbName)
if err != nil {
return fmt.Errorf("unable to scan database name: %w", db.Error)
}
parsed := fmt.Sprintf(qry, dbName, originalTable, dbName, historyTable)
db = c.orm.Raw(parsed)
if db.Error != nil {
return db.Error
}
invalidColumnsErr := &multierror.Error{}
rows, err := db.Rows()
if err != nil {
return fmt.Errorf("unable to fetch rows: %w", err)
}
defer rows.Close()
for rows.Next() {
var tableName, columnName, originalType string
var historyType *string
err = rows.Scan(&tableName, &columnName, &originalType, &historyType)
if err != nil {
return fmt.Errorf("unable to scan result row: %w", err)
}
if historyType == nil {
err = fmt.Errorf("missing column %s of type %s on history table %s", columnName, originalType, tableName)
} else {
err = fmt.Errorf("type mismatch for table %s and column %s: expected %s, got %s", tableName, columnName, originalType, mdl.EmptyIfNil(historyType))
}
invalidColumnsErr = multierror.Append(invalidColumnsErr, err)
}
return invalidColumnsErr.ErrorOrNil()
}
func (c *ChangeHistoryManager) executeMigration(originalTable, historyTable *tableMetadata) error {
statements := make([]string, 0)
if !historyTable.exists {
statements = append(statements, c.createHistoryTable(historyTable))
statements = append(statements, c.dropHistoryTriggers(originalTable, historyTable)...)
statements = append(statements, c.createHistoryTriggers(originalTable, historyTable)...)
c.logger.Info("creating change history setup")
return c.execute(statements)
}
updated, statement := c.updateHistoryTable(historyTable)
if updated {
statements = append(statements, statement)
statements = append(statements, c.dropHistoryTriggers(originalTable, historyTable)...)
statements = append(statements, c.createHistoryTriggers(originalTable, historyTable)...)
c.logger.Info("updating change history setup")
return c.execute(statements)
}
c.logger.Info("change history setup was already up to date")
return nil
}
func (c *ChangeHistoryManager) buildOriginalTableMetadata(model ModelBased) *tableMetadata {
scope := c.orm.NewScope(model)
fields := scope.GetModelStruct().StructFields
tableName := scope.TableName()
return newTableMetadata(scope, tableName, fields)
}
func (c *ChangeHistoryManager) buildHistoryTableMetadata(model ModelBased, originalTable *tableMetadata) *tableMetadata {
historyScope := c.orm.NewScope(ChangeHistoryModel{})
tableName := fmt.Sprintf("%s_%s", originalTable.tableName, c.settings.TableSuffix)
modelFields := c.orm.NewScope(model).GetModelStruct().StructFields
fields := append(historyScope.GetModelStruct().StructFields, modelFields...)
return newTableMetadata(historyScope, tableName, fields)
}
func (c *ChangeHistoryManager) createHistoryTable(historyTable *tableMetadata) string {
return fmt.Sprintf("CREATE TABLE %v (%v, PRIMARY KEY (%v))",
historyTable.tableNameQuoted,
strings.Join(historyTable.columnDefinitions(), ","),
strings.Join(historyTable.primaryKeyNamesQuoted(), ","),
)
}
func (c *ChangeHistoryManager) dropHistoryTriggers(originalTable *tableMetadata, historyTable *tableMetadata) []string {
statements := make([]string, 0)
triggers := []string{
originalTable.tableName + "_ai",
originalTable.tableName + "_au",
originalTable.tableName + "_bd",
historyTable.tableName + "_revai",
}
for _, trigger := range triggers {
statements = append(statements, fmt.Sprintf(`DROP TRIGGER IF EXISTS %s`, trigger))
}
return statements
}
func (c *ChangeHistoryManager) createHistoryTriggers(originalTable *tableMetadata, historyTable *tableMetadata) []string {
const NewRecord = "NEW"
const OldRecord = "OLD"
statements := []string{
fmt.Sprintf(`CREATE TRIGGER %s_ai AFTER INSERT ON %s FOR EACH ROW %s WHERE %s`,
originalTable.tableName,
originalTable.tableNameQuoted,
c.insertHistoryEntry(originalTable, historyTable, "insert", true),
c.primaryKeysMatchCondition(originalTable, NewRecord),
),
fmt.Sprintf(`CREATE TRIGGER %s_au AFTER UPDATE ON %s FOR EACH ROW %s WHERE %s AND (%s)`,
originalTable.tableName,
originalTable.tableNameQuoted,
c.insertHistoryEntry(originalTable, historyTable, "update", true),
c.primaryKeysMatchCondition(originalTable, NewRecord),
c.rowUpdatedCondition(originalTable),
),
fmt.Sprintf(`CREATE TRIGGER %s_bd BEFORE DELETE ON %s FOR EACH ROW %s WHERE %s`,
originalTable.tableName,
originalTable.tableNameQuoted,
c.insertHistoryEntry(originalTable, historyTable, "delete", false),
c.primaryKeysMatchCondition(originalTable, OldRecord),
),
fmt.Sprintf(`CREATE TRIGGER %s_revai BEFORE INSERT ON %s FOR EACH ROW %s`,
historyTable.tableName,
historyTable.tableNameQuoted,
c.incrementRevision(originalTable, historyTable),
),
}
return statements
}
func (c *ChangeHistoryManager) insertHistoryEntry(originalTable *tableMetadata, historyTable *tableMetadata, action string, includeAuthorEmail bool) string {
columnNames := originalTable.columnNamesQuoted()
if !includeAuthorEmail {
columnNames = originalTable.columnNamesQuotedExcludingValue(c.settings.ChangeAuthorField)
}
columns := strings.Join(columnNames, ",")
values := "d." + strings.Join(columnNames, ", d.")
return fmt.Sprintf(`
INSERT INTO %s (change_history_action,change_history_revision,change_history_action_at,%s)
SELECT '%s', NULL, NOW(), %s
FROM %s AS d`,
historyTable.tableNameQuoted,
columns,
action,
values,
originalTable.tableNameQuoted)
}
func (c *ChangeHistoryManager) incrementRevision(originalTable *tableMetadata, historyTable *tableMetadata) string {
return fmt.Sprintf(`
BEGIN
SET NEW.change_history_revision = (SELECT IFNULL(MAX(d.change_history_revision), 0) + 1 FROM %s as d WHERE %s);
END`,
historyTable.tableNameQuoted,
c.primaryKeysMatchCondition(originalTable, "NEW"),
)
}
func (c *ChangeHistoryManager) primaryKeysMatchCondition(originalTable *tableMetadata, record string) string {
var conditions []string
for _, columnName := range originalTable.primaryKeyNamesQuoted() {
condition := fmt.Sprintf("d.%s = %s.%s", columnName, record, columnName)
conditions = append(conditions, condition)
}
return strings.Join(conditions, " AND ")
}
func (c *ChangeHistoryManager) rowUpdatedCondition(originalTable *tableMetadata) string {
columnNames := originalTable.columnNamesQuotedExcludingValue(c.settings.ChangeAuthorField, ColumnUpdatedAt)
var conditions []string
for _, columnName := range columnNames {
condition := fmt.Sprintf("NOT (OLD.%s <=> NEW.%s)", columnName, columnName)
conditions = append(conditions, condition)
}
return strings.Join(conditions, " OR ")
}
func (c *ChangeHistoryManager) updateHistoryTable(historyTable *tableMetadata) (bool, string) {
for _, column := range historyTable.columns {
if column.exists {
continue
}
return true, fmt.Sprintf("ALTER TABLE %s ADD %s",
historyTable.tableNameQuoted,
column.definition,
)
}
return false, ""
}
func (c *ChangeHistoryManager) execute(statements []string) error {
for _, statement := range statements {
c.logger.Debug(statement)
_, err := c.orm.CommonDB().Exec(statement)
if err != nil {
c.logger.WithFields(log.Fields{
"sql": statement,
}).Error("could not migrate change history: %w", err)
return fmt.Errorf("could not migrate change history: %w", err)
}
}
c.logger.Info("change history setup is now up to date")
return nil
}