-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_migration.go
More file actions
485 lines (414 loc) · 13.5 KB
/
db_migration.go
File metadata and controls
485 lines (414 loc) · 13.5 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package portal
import (
"database/sql"
"errors"
"fmt"
"io/fs"
"reflect"
"strings"
"unicode"
"github.com/go-sql-driver/mysql"
"github.com/mattn/go-sqlite3"
"github.com/pressly/goose/v3"
"go.lumeweb.com/portal/core"
"go.lumeweb.com/portal/db"
"go.uber.org/zap"
"gorm.io/gorm"
)
// GOOSE_TABLE_NAME_FORMAT defines the per-plugin goose version table name.
// %s is the (sanitized) plugin identifier.
const GOOSE_TABLE_NAME_FORMAT = "goose_%s_version"
// GOOSE_CORE_TABLE_NAME is the goose version table for core migrations.
const GOOSE_CORE_TABLE_NAME = "goose_db_version"
// logMySQLError extracts and logs MySQL-specific error details
func logMySQLError(logger *core.Logger, plugin string, err error) bool {
var mysqlErr *mysql.MySQLError
if !errors.As(err, &mysqlErr) {
return false
}
logger.Error("MySQL driver error detected",
zap.String("plugin", plugin),
zap.Uint16("mysql_error_number", mysqlErr.Number),
zap.String("mysql_sql_state", string(mysqlErr.SQLState[:])),
zap.String("mysql_error_message", mysqlErr.Message),
)
// Provide a summary of common MySQL error codes
var errorSummary string
switch mysqlErr.Number {
case 1062:
errorSummary = "Duplicate entry - unique constraint violation"
case 1045:
errorSummary = "Access denied - authentication failed"
case 1049:
errorSummary = "Unknown database"
case 1050:
errorSummary = "Table already exists"
case 1054:
errorSummary = "Unknown column"
case 1146:
errorSummary = "Table doesn't exist"
case 1213:
errorSummary = "Deadlock found"
case 1205:
errorSummary = "Lock wait timeout exceeded"
case 1366:
errorSummary = "Incorrect string value"
case 1292:
errorSummary = "Truncated incorrect value"
case 1064:
errorSummary = "Syntax error or unrecognized SQL token"
default:
errorSummary = fmt.Sprintf("Error code %d", mysqlErr.Number)
}
logger.Error("MySQL error summary",
zap.String("plugin", plugin),
zap.Uint16("mysql_error_number", mysqlErr.Number),
zap.String("error_summary", errorSummary),
)
return true
}
// logSQLiteError extracts and logs SQLite-specific error details
func logSQLiteError(logger *core.Logger, plugin string, err error) bool {
var sqliteErr *sqlite3.Error
if !errors.As(err, &sqliteErr) {
return false
}
fields := []zap.Field{
zap.String("plugin", plugin),
zap.Int("sqlite_error_code", int(sqliteErr.Code)),
zap.String("sqlite_error_message", sqliteErr.Error()),
}
// Include extended error code if available
if sqliteErr.ExtendedCode != 0 {
fields = append(fields, zap.Int("sqlite_extended_error_code", int(sqliteErr.ExtendedCode)))
}
// Include system errno if available
if sqliteErr.SystemErrno != 0 {
fields = append(fields, zap.Int("system_errno", int(sqliteErr.SystemErrno)))
}
logger.Error("SQLite driver error detected", fields...)
// Provide a summary of common SQLite error codes
var errorSummary string
switch sqliteErr.Code {
case 1: // SQLITE_ERROR
errorSummary = "SQL error or missing database"
case 5: // SQLITE_BUSY
errorSummary = "Database file is locked"
case 6: // SQLITE_LOCKED
errorSummary = "Table in the database is locked"
case 7: // SQLITE_NOMEM
errorSummary = "malloc() failed"
case 14: // SQLITE_CANTOPEN
errorSummary = "Unable to open database file"
case 19: // SQLITE_CONSTRAINT
errorSummary = "Constraint violation"
case 20: // SQLITE_MISMATCH
errorSummary = "Data type mismatch"
case 21: // SQLITE_MISUSE
errorSummary = "Library used incorrectly"
default:
errorSummary = fmt.Sprintf("Error code %d", sqliteErr.Code)
}
logger.Error("SQLite error summary",
zap.String("plugin", plugin),
zap.Int("sqlite_error_code", int(sqliteErr.Code)),
zap.String("error_summary", errorSummary),
)
return true
}
// extractSQLQuery attempts to extract the SQL query from goose error messages
func extractSQLQuery(errorMsg string) string {
// Goose error format typically: "failed to execute SQL query \"...QUERY...\" : ERROR"
// or similar variations with escape sequences
start := strings.Index(errorMsg, "\"")
if start == -1 {
return ""
}
// Find matching closing quote, counting escaped quotes
queryStart := start + 1
quoteCount := 0
for i := queryStart; i < len(errorMsg); i++ {
if errorMsg[i] == '\\' && i+1 < len(errorMsg) {
// Skip escaped characters
i++
} else if errorMsg[i] == '"' {
if quoteCount == 0 {
// Found closing quote
sqlQuery := errorMsg[queryStart:i]
// Truncate very long queries for display
const maxQueryLength = 500
if len(sqlQuery) > maxQueryLength {
sqlQuery = sqlQuery[:maxQueryLength] + fmt.Sprintf("... (truncated, total length: %d)", len(sqlQuery))
}
return sqlQuery
}
quoteCount--
}
}
return ""
}
// logMigrationError unwraps and inspects migration errors to extract and display
// full database-specific error details including error codes, messages, and SQL queries.
// It supports both MySQL and SQLite databases.
func logMigrationError(logger *core.Logger, dbType, plugin string, err error) {
// Log the basic error first
logger.Error("Migration failed",
zap.String("plugin", plugin),
zap.String("db_type", dbType),
zap.Error(err),
)
// Try to extract database-specific error information
detected := false
switch {
case dbType == "mysql":
detected = logMySQLError(logger, plugin, err)
case dbType == "sqlite" || dbType == "sqlitememory":
detected = logSQLiteError(logger, plugin, err)
}
// If no specific driver error found, walk the error chain
if !detected {
unwrapped := errors.Unwrap(err)
depth := 0
for unwrapped != nil && depth < 5 {
logger.Debug("Underlying error in chain",
zap.String("plugin", plugin),
zap.Int("depth", depth),
zap.String("error_type", fmt.Sprintf("%T", unwrapped)),
zap.String("error_message", unwrapped.Error()),
)
// Try to extract database-specific errors from the chain
switch {
case dbType == "mysql":
if logMySQLError(logger, plugin, unwrapped) {
detected = true
break
}
case dbType == "sqlite" || dbType == "sqlitememory":
if logSQLiteError(logger, plugin, unwrapped) {
detected = true
break
}
}
unwrapped = errors.Unwrap(unwrapped)
if detected {
break
}
depth++
}
}
// Try to extract SQL query from the error message
errorMsg := err.Error()
if strings.Contains(errorMsg, "failed to execute SQL query") {
sqlQuery := extractSQLQuery(errorMsg)
if sqlQuery != "" {
logger.Error("Failed SQL query",
zap.String("plugin", plugin),
zap.String("sql_query", sqlQuery),
)
}
}
}
type MigrationManager struct {
ctx core.Context
logger *core.Logger
}
func NewMigrationManager(ctx core.Context) (*MigrationManager, error) {
return &MigrationManager{
ctx: ctx,
logger: ctx.Logger(),
}, nil
}
func normalizeDbDialect(dbType string) string {
switch dbType {
case "sqlite", "sqlitememory":
return "sqlite3"
case "mysql":
return "mysql"
default:
return dbType
}
}
func (m *MigrationManager) RunMigrations(db *gorm.DB) error {
return m.ExecuteMigrations(db)
}
func (m *MigrationManager) ExecuteMigrations(_db *gorm.DB) error {
m.logger.Info("Starting database migrations")
cfg := m.ctx.Config()
dbType := cfg.Config().Core.DB.Type
sqlDb, err := _db.DB()
if err != nil {
return fmt.Errorf("failed to get db: %w", err)
}
// Run core migrations first
m.logger.Info("Running core migrations", zap.String("db_type", dbType))
if err := m.runCoreMigrations(sqlDb, dbType); err != nil {
m.logger.Error("Failed to run core migrations", zap.Error(err))
return err
}
m.logger.Info("Core migrations completed successfully")
// Run each plugin's migrations individually
m.logger.Info("Running plugin migrations", zap.String("db_type", dbType))
if err := m.runPluginMigrations(sqlDb, dbType); err != nil {
m.logger.Error("Failed to run plugin migrations", zap.Error(err))
return err
}
m.logger.Info("Plugin migrations completed successfully")
m.logger.Info("Database migrations completed successfully")
return nil
}
func (m *MigrationManager) runCoreMigrations(sqlDb *sql.DB, dbType string) error {
// Set the default table name for core migrations
goose.SetTableName(GOOSE_CORE_TABLE_NAME)
coreMigrations := db.GetCoreMigrations()
coreFS := getMigrationsByType(dbType, coreMigrations)
if coreFS == nil {
m.logger.Warn("No core migrations found for database type", zap.String("db_type", dbType))
return nil
}
m.logger.Debug("Setting up core migrations", zap.String("db_type", dbType))
goose.SetBaseFS(coreFS)
dialect := normalizeDbDialect(dbType)
if err := goose.SetDialect(dialect); err != nil {
m.logger.Error("Failed to select goose db dialect for core migrations",
zap.String("db_type", dbType),
zap.String("dialect", dialect),
zap.Error(err))
return fmt.Errorf("failed to select goose db dialect: %w", err)
}
m.logger.Debug("Running core migrations")
if err := goose.RunContext(m.ctx.GetContext(), "up", sqlDb, "."); err != nil {
m.logger.Error("Failed to run core migrations", zap.String("db_type", dbType), zap.Error(err))
return fmt.Errorf("failed to run core migrations: %w", err)
}
return nil
}
func (m *MigrationManager) runPluginMigrations(sqlDb *sql.DB, dbType string) error {
pluginMigrations, migrationOrder, err := getMigrations()
if err != nil {
m.logger.Error("Failed to get plugin migrations", zap.Error(err))
return err
}
m.logger.Info("Found plugins with migrations", zap.Int("count", len(migrationOrder)))
for _, plugin := range migrationOrder {
tableName := pluginTableName(plugin)
m.logger.Debug("Running migrations for plugin", zap.String("plugin", plugin), zap.String("table", tableName))
// Set custom table name for this plugin
goose.SetTableName(tableName)
migrations := getMigrationsByType(dbType, pluginMigrations[plugin])
if migrations == nil {
m.logger.Debug("No migrations found for plugin", zap.String("plugin", plugin), zap.String("db_type", dbType))
continue
}
m.logger.Debug("Setting up plugin migrations", zap.String("plugin", plugin), zap.String("db_type", dbType))
goose.SetBaseFS(migrations)
dialect := normalizeDbDialect(dbType)
if err := goose.SetDialect(dialect); err != nil {
m.logger.Error("Failed to select goose db dialect for plugin",
zap.String("plugin", plugin),
zap.String("db_type", dbType),
zap.String("dialect", dialect),
zap.Error(err))
return fmt.Errorf("failed to select goose db dialect for plugin %s: %w", plugin, err)
}
m.logger.Debug("Running plugin migrations", zap.String("plugin", plugin))
if err := goose.RunContext(m.ctx.GetContext(), "up", sqlDb, "."); err != nil {
logMigrationError(m.logger, dbType, plugin, err)
return fmt.Errorf("failed to run migrations for plugin %s: %w", plugin, err)
}
m.logger.Debug("Plugin migrations completed", zap.String("plugin", plugin))
// Hygiene: clear per-plugin state to avoid leaking Goose globals.
goose.SetBaseFS(nil)
// Table name will be set again on next iteration; no need to restore here.
}
return nil
}
// Helper to get all models that need migration
func getModels(ctx core.Context) ([]interface{}, error) {
plugins := core.GetPlugins()
models := make([]interface{}, 0)
for _, plugin := range plugins {
if plugin.Models != nil && len(plugin.Models) > 0 {
for _, model := range plugin.Models {
typ := reflect.TypeOf(model)
if typ.Kind() != reflect.Ptr {
ctx.Logger().Error("Model must be a pointer", zap.String("model", typ.Name()))
return nil, core.ErrInvalidModel
}
}
models = append(models, plugin.Models...)
}
}
return models, nil
}
func getMigrations() (map[string]core.DBMigration, []string, error) {
plugins := core.GetPlugins()
migrations := make(map[string]core.DBMigration)
order := make([]string, 0)
for _, plugin := range plugins {
if plugin.Migrations != nil && len(plugin.Migrations) > 0 {
migrations[plugin.ID] = plugin.Migrations
order = append(order, plugin.ID)
}
}
return migrations, order, nil
}
func getMigrationsByType(typ string, migrations core.DBMigration) fs.FS {
switch typ {
case "sqlite", "sqlitememory":
return migrations[core.DB_TYPE_SQLITE]
case "mysql":
return migrations[core.DB_TYPE_MYSQL]
default:
return nil
}
}
// sanitizeIdent converts a string to a valid SQL identifier by:
// 1. Lowercasing all characters
// 2. Replacing any character that is not [a-z0-9_] with '_'
// 3. Ensuring it doesn't start with a digit
// 4. Truncating to 64 characters (MySQL identifier limit)
func sanitizeIdent(ident string) string {
s := strings.Map(func(r rune) rune {
switch {
case r >= 'a' && r <= 'z':
return r
case r >= 'A' && r <= 'Z':
return unicode.ToLower(r)
case r >= '0' && r <= '9':
return r
case r == '_':
return r
default:
return '_'
}
}, ident)
if s == "" {
s = "plugin"
}
if len(s) > 0 && s[0] >= '0' && s[0] <= '9' {
s = "_" + s
}
// Truncate to 64 characters if needed
if len(s) > 64 {
s = s[:64]
}
return s
}
// pluginTableName generates a safe table name for goose migrations.
func pluginTableName(plugin string) string {
const prefix = "goose_"
const suffix = "_version"
const maxLen = 64
// Sanitize the plugin identifier
sanitized := sanitizeIdent(plugin)
// Calculate reserved length for prefix and suffix
reservedLen := len(prefix) + len(suffix)
// If the total length fits within the limit, return as-is
if len(sanitized)+reservedLen <= maxLen {
return fmt.Sprintf("%s%s%s", prefix, sanitized, suffix)
}
// Truncate the sanitized plugin name to fit within limits
availableLen := maxLen - reservedLen
truncated := sanitized[:availableLen]
return fmt.Sprintf("%s%s%s", prefix, truncated, suffix)
}