Fix policy membership migration for MariaDB (#3418)

* Fix policy membership migration for MariaDB

* Use constraintsForTable to get the proper foreign key names
This commit is contained in:
Lucas Manuel Rodriguez 2021-12-21 14:39:46 -03:00 committed by GitHub
parent 7f5b3cc15c
commit 93f4577c7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View file

@ -0,0 +1 @@
* Fix v4.7.0 migration issue with policy membership history on MariaDB.

View file

@ -2,6 +2,7 @@ package tables
import (
"database/sql"
"fmt"
"github.com/pkg/errors"
)
@ -16,6 +17,21 @@ func Up_20211202092042(tx *sql.Tx) error {
return errors.Wrap(err, "drop view policy_membership")
}
table := "policy_membership_history"
referencedTables := map[string]struct{}{
"policies": {},
"hosts": {},
}
constraints, err := constraintsForTable(tx, table, referencedTables)
if err != nil {
return errors.Wrap(err, "getting references to policies and hosts table")
}
for _, ct := range constraints {
if _, err := tx.Exec(fmt.Sprintf(`ALTER TABLE policy_membership_history DROP FOREIGN KEY %s;`, ct)); err != nil {
return errors.Wrapf(err, "dropping policy_membership_history foreign keys: %s", ct)
}
}
policyMembershipTable := `
CREATE TABLE IF NOT EXISTS policy_membership (
policy_id INT UNSIGNED NOT NULL,