Skip to content

Commit

Permalink
Merge branch 'dev' into chris/refactor-contract-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl authored Dec 16, 2024
2 parents e671991 + c5c89a7 commit 47faaff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Improve migration out after foreignkey check fails
24 changes: 21 additions & 3 deletions stores/sql/sqlite/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,29 @@ func applyMigration(ctx context.Context, db *sql.DB, fn func(tx sql.Tx) (bool, e
} else if !migrated {
return nil
}

// perform foreign key integrity check
if err := tx.QueryRow(ctx, "PRAGMA foreign_key_check").Scan(); !errors.Is(err, dsql.ErrNoRows) {
return fmt.Errorf("foreign key constraints are not satisfied")
rows, err := tx.Query(ctx, "PRAGMA foreign_key_check")
if err != nil {
return err
}
defer rows.Close()

// check if there are any foreign key constraint violations
var errs []error
var tableName, foreignKey string
var rowID int
for rows.Next() {
if err := rows.Scan(&tableName, &rowID, &foreignKey); err != nil {
return fmt.Errorf("failed to scan foreign key check result: %w", err)
}
errs = append(errs, fmt.Errorf("foreign key constraint violation in table '%s': row %d, foreign key %s", tableName, rowID, foreignKey))
}

if err := rows.Err(); err != nil {
return fmt.Errorf("error iterating foreign key check results: %w", err)
}
return nil
return errors.Join(errs...)
})
}

Expand Down

0 comments on commit 47faaff

Please sign in to comment.