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

Commit

Permalink
Drop FK constraints if they already exist before creating (#1534)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Jan 8, 2021
1 parent f2b0194 commit 1b0a580
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1764,9 +1764,15 @@ func (db *Database) Migrations(ctx context.Context) []*gormigrate.Migration {

// Convert realm and user into real foreign keys. If the parent record
// is deleted, also delete the membership record.
if err := tx.Exec(`ALTER TABLE user_realms DROP CONSTRAINT IF EXISTS fk_realm`).Error; err != nil {
return err
}
if err := tx.Exec(`ALTER TABLE user_realms ADD CONSTRAINT fk_realm FOREIGN KEY (realm_id) REFERENCES realms(id) ON DELETE CASCADE`).Error; err != nil {
return err
}
if err := tx.Exec(`ALTER TABLE user_realms DROP CONSTRAINT IF EXISTS fk_user`).Error; err != nil {
return err
}
if err := tx.Exec(`ALTER TABLE user_realms ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE`).Error; err != nil {
return err
}
Expand Down

0 comments on commit 1b0a580

Please sign in to comment.