From 6497e0ba2e1ea79c82ec5b00a3f86bb606c7c479 Mon Sep 17 00:00:00 2001 From: Tomas Touceda Date: Tue, 21 Sep 2021 16:37:13 -0300 Subject: [PATCH] Improve performance of cascade host software migration (#2163) --- changes/improve-speed-of-migration | 1 + .../20210819131107_AddCascadeToHostSoftware.go | 18 ++++++++++++++---- server/datastore/mysql/migrations_test.go | 3 +++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 changes/improve-speed-of-migration diff --git a/changes/improve-speed-of-migration b/changes/improve-speed-of-migration new file mode 100644 index 0000000000..203c2faa3d --- /dev/null +++ b/changes/improve-speed-of-migration @@ -0,0 +1 @@ +* Improve the performance of certain database migrations that were preventing users from updating. diff --git a/server/datastore/mysql/migrations/tables/20210819131107_AddCascadeToHostSoftware.go b/server/datastore/mysql/migrations/tables/20210819131107_AddCascadeToHostSoftware.go index f1721cf57b..4de803112b 100644 --- a/server/datastore/mysql/migrations/tables/20210819131107_AddCascadeToHostSoftware.go +++ b/server/datastore/mysql/migrations/tables/20210819131107_AddCascadeToHostSoftware.go @@ -31,13 +31,13 @@ func Up_20210819131107(tx *sql.Tx) error { } // Clear any orphan software and host_software - _, err = tx.Exec(`DELETE FROM host_software WHERE NOT EXISTS (select 1 from hosts h where h.id=host_software.host_id)`) + _, err = tx.Exec(`CREATE TEMPORARY TABLE temp_host_software AS SELECT * FROM host_software;`) if err != nil { - return errors.Wrap(err, "clearing orphan host_software") + return errors.Wrap(err, "save current host software to a temp table") } - _, err = tx.Exec(`DELETE FROM software WHERE NOT EXISTS (select 1 from host_software hs where hs.software_id=software.id)`) + _, err = tx.Exec(`DELETE FROM host_software;`) if err != nil { - return errors.Wrap(err, "clearing orphan software") + return errors.Wrap(err, "clear all host software") } if _, err := tx.Exec(` @@ -48,6 +48,16 @@ func Up_20210819131107(tx *sql.Tx) error { return errors.Wrap(err, "add fk on host_software hosts & software") } + _, err = tx.Exec(`INSERT IGNORE INTO host_software SELECT * FROM temp_host_software;`) + if err != nil { + return errors.Wrap(err, "reinserting host software") + } + + _, err = tx.Exec(`DROP TABLE temp_host_software;`) + if err != nil { + return errors.Wrap(err, "dropping temp table") + } + return nil } diff --git a/server/datastore/mysql/migrations_test.go b/server/datastore/mysql/migrations_test.go index f30833f41c..2398c5d8ac 100644 --- a/server/datastore/mysql/migrations_test.go +++ b/server/datastore/mysql/migrations_test.go @@ -111,7 +111,10 @@ func Test20210819131107_AddCascadeToHostSoftware(t *testing.T) { require.NoError(t, ds.DeleteHost(context.Background(), host1.ID)) + t.Log("Done adding software...") + startTime := time.Now() require.NoError(t, tables.MigrationClient.UpByOne(ds.writer.DB, "")) + t.Log("took", time.Since(startTime)) // Make sure we don't delete more than we need hostCheck, err := ds.Host(context.Background(), host2.ID)