Optimize policy_updated_at migration (#2362)

- Use `TRUNCATE TABLE` rather than `DELETE FROM` for improved performance.
- Move DDL statement after truncate to avoid issues with retries (due to
  column already being created).

#2360
This commit is contained in:
Zach Wasserman 2021-10-05 09:24:03 -07:00 committed by GitHub
parent 53f82b8e28
commit a2e9b539a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

1
changes/2360-updated-at Normal file
View file

@ -0,0 +1 @@
* Improve performance and reliability of Policy database migrations.

View file

@ -11,16 +11,16 @@ func init() {
}
func Up_20210927143115(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE hosts ADD COLUMN policy_updated_at timestamp NOT NULL DEFAULT '2000-01-01 00:00:00'")
if err != nil {
return errors.Wrap(err, "adding policy_updated_at column")
}
_, err = tx.Exec("delete from policy_membership_history")
_, err := tx.Exec("TRUNCATE TABLE policy_membership_history")
if err != nil {
return errors.Wrap(err, "clearing policy_membership_history")
}
_, err = tx.Exec("ALTER TABLE hosts ADD COLUMN policy_updated_at timestamp NOT NULL DEFAULT '2000-01-01 00:00:00'")
if err != nil {
return errors.Wrap(err, "adding policy_updated_at column")
}
return err
}