Improve performance of cascade host software migration (#2163)

This commit is contained in:
Tomas Touceda 2021-09-21 16:37:13 -03:00 committed by GitHub
parent f57b92ac2d
commit 6497e0ba2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 4 deletions

View file

@ -0,0 +1 @@
* Improve the performance of certain database migrations that were preventing users from updating.

View file

@ -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
}

View file

@ -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)