From a2e9b539a129db01c4acb637db9f152efe217a3f Mon Sep 17 00:00:00 2001 From: Zach Wasserman Date: Tue, 5 Oct 2021 09:24:03 -0700 Subject: [PATCH] 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 --- changes/2360-updated-at | 1 + .../20210927143115_AddPolicyUpdatedAtColumn.go | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 changes/2360-updated-at diff --git a/changes/2360-updated-at b/changes/2360-updated-at new file mode 100644 index 0000000000..d034536d62 --- /dev/null +++ b/changes/2360-updated-at @@ -0,0 +1 @@ +* Improve performance and reliability of Policy database migrations. diff --git a/server/datastore/mysql/migrations/tables/20210927143115_AddPolicyUpdatedAtColumn.go b/server/datastore/mysql/migrations/tables/20210927143115_AddPolicyUpdatedAtColumn.go index 43385178ee..a0681d5b57 100644 --- a/server/datastore/mysql/migrations/tables/20210927143115_AddPolicyUpdatedAtColumn.go +++ b/server/datastore/mysql/migrations/tables/20210927143115_AddPolicyUpdatedAtColumn.go @@ -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 }