Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Delete any orphaned entries before creating foreignkey (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Jan 8, 2021
1 parent a0ee8fe commit f2b0194
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,17 @@ func (db *Database) Migrations(ctx context.Context) []*gormigrate.Migration {
return err
}

// We are about to add foreign key references for these fields. However,
// in the past, it was possible for de-association to occur. We need to
// delete any orphaned user_realm and admin_realm associations so the
// foreign key constraint can be properly applied in the next step.
if err := tx.Exec(`DELETE FROM user_realms ur WHERE NOT EXISTS (SELECT FROM users u WHERE u.id = ur.user_id)`).Error; err != nil {
return err
}
if err := tx.Exec(`DELETE FROM admin_realms ar WHERE NOT EXISTS (SELECT FROM users u WHERE u.id = ar.user_id)`).Error; err != nil {
return err
}

// Update existing columns defaults.
if err := tx.Exec(`ALTER TABLE user_realms ALTER COLUMN realm_id SET NOT NULL`).Error; err != nil {
return err
Expand Down

0 comments on commit f2b0194

Please sign in to comment.