fix: verify server snapshots and recover whole migration sessions (#3)
This commit is contained in:
1 parent
3d4101ecc0
commit
8569871a17
29 files changed
+1923
-133
No files matched your search
@@ -34,35 +34,43 @@ func applySQLMigrationFile(db *gorm.DB, filename, content string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse migration %s: %w", filename, err)
|
||||
}
|
||||
statements, err := splitSQLStatements(content)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse migration %s SQL: %w", filename, err)
|
||||
}
|
||||
return db.Connection(func(conn *gorm.DB) error {
|
||||
if err := executeMigrationStatements(conn, filename, splitSQLStatements(content)); err != nil {
|
||||
return err
|
||||
}
|
||||
// Each query gets a fresh statement on the pinned connection: chaining
|
||||
// Raw/Create on conn itself leaks the previous query's state (with two
|
||||
// verify checks the history insert targeted a table named "rows").
|
||||
fresh := func() *gorm.DB { return conn.Session(&gorm.Session{NewDB: true}) }
|
||||
for _, check := range checks {
|
||||
rows, err := fresh().Raw(check.query).Rows()
|
||||
if err != nil {
|
||||
return fmt.Errorf("verify migration %s (%s): %w", filename, check.description, err)
|
||||
}
|
||||
failed := rows.Next()
|
||||
rowErr := rows.Err()
|
||||
closeErr := rows.Close()
|
||||
if rowErr != nil {
|
||||
return fmt.Errorf("read migration verification %s (%s): %w", filename, check.description, rowErr)
|
||||
}
|
||||
if closeErr != nil {
|
||||
return fmt.Errorf("close migration verification %s (%s): %w", filename, check.description, closeErr)
|
||||
}
|
||||
if failed {
|
||||
return fmt.Errorf("verify migration %s failed: %s", filename, check.description)
|
||||
}
|
||||
}
|
||||
if err := fresh().Create(&SQLSchemaMigration{Filename: filename}).Error; err != nil {
|
||||
return fmt.Errorf("record migration %s: %w", filename, err)
|
||||
}
|
||||
return nil
|
||||
return applySQLMigrationOnSession(conn, filename, statements, checks)
|
||||
})
|
||||
}
|
||||
|
||||
func applySQLMigrationOnSession(conn *gorm.DB, filename string, statements []string, checks []migrationVerification) error {
|
||||
if err := executeMigrationStatements(conn, filename, statements); err != nil {
|
||||
return err
|
||||
}
|
||||
// Each query gets a fresh statement on the pinned connection: chaining
|
||||
// Raw/Create on conn itself leaks the previous query's state (with two
|
||||
// verify checks the history insert targeted a table named "rows").
|
||||
fresh := func() *gorm.DB { return conn.Session(&gorm.Session{NewDB: true}) }
|
||||
for _, check := range checks {
|
||||
rows, err := fresh().Raw(check.query).Rows()
|
||||
if err != nil {
|
||||
return fmt.Errorf("verify migration %s (%s): %w", filename, check.description, err)
|
||||
}
|
||||
failed := rows.Next()
|
||||
rowErr := rows.Err()
|
||||
closeErr := rows.Close()
|
||||
if rowErr != nil {
|
||||
return fmt.Errorf("read migration verification %s (%s): %w", filename, check.description, rowErr)
|
||||
}
|
||||
if closeErr != nil {
|
||||
return fmt.Errorf("close migration verification %s (%s): %w", filename, check.description, closeErr)
|
||||
}
|
||||
if failed {
|
||||
return fmt.Errorf("verify migration %s failed: %s", filename, check.description)
|
||||
}
|
||||
}
|
||||
if err := fresh().Create(&SQLSchemaMigration{Filename: filename}).Error; err != nil {
|
||||
return fmt.Errorf("record migration %s: %w", filename, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user