-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added migration to update software_titles.
- Loading branch information
Showing
3 changed files
with
108 additions
and
11 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
server/datastore/mysql/migrations/tables/20240607133721_ReconcileSoftwareTitles.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tables | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
func init() { | ||
MigrationClient.AddMigration(Up_20240607133721, Down_20240607133721) | ||
} | ||
|
||
func Up_20240607133721(tx *sql.Tx) error { | ||
|
||
// For users that are not running vulnerabilities job, we need to ensure that software_titles are up-to-date | ||
|
||
upsertTitlesStmt := ` | ||
INSERT INTO software_titles (name, source, browser) | ||
SELECT DISTINCT | ||
name, | ||
source, | ||
browser | ||
FROM | ||
software s | ||
WHERE | ||
NOT EXISTS (SELECT 1 FROM software_titles st WHERE (s.name, s.source, s.browser) = (st.name, st.source, st.browser)) | ||
ON DUPLICATE KEY UPDATE software_titles.id = software_titles.id` | ||
|
||
_, err := tx.Exec(upsertTitlesStmt) | ||
if err != nil { | ||
return fmt.Errorf("failed to upsert software titles: %w", err) | ||
} | ||
|
||
// update title ids for software table entries | ||
updateSoftwareStmt := ` | ||
UPDATE | ||
software s, | ||
software_titles st | ||
SET | ||
s.title_id = st.id | ||
WHERE | ||
(s.name, s.source, s.browser) = (st.name, st.source, st.browser) | ||
AND (s.title_id IS NULL OR s.title_id != st.id)` | ||
|
||
_, err = tx.Exec(updateSoftwareStmt) | ||
if err != nil { | ||
return fmt.Errorf("failed to update software title_id: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func Down_20240607133721(tx *sql.Tx) error { | ||
return nil | ||
} |
53 changes: 53 additions & 0 deletions
53
server/datastore/mysql/migrations/tables/20240607133721_ReconcileSoftwareTitles_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tables | ||
|
||
import ( | ||
"context" | ||
"github.com/fleetdm/fleet/v4/server/fleet" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestUp_20240607133721(t *testing.T) { | ||
db := applyUpToPrev(t) | ||
|
||
// Insert data into software_titles | ||
title1 := execNoErrLastID(t, db, "INSERT INTO software_titles (name, source, browser) VALUES (?, ?, ?)", "sw1", "src1", "") | ||
|
||
// Insert software | ||
const insertStmt = `INSERT INTO software | ||
(name, version, source, browser, checksum, title_id) | ||
VALUES | ||
(?, ?, ?, ?, ?, ?)` | ||
|
||
execNoErr(t, db, insertStmt, "sw1", "1.0", "src1", "", "1", title1) | ||
execNoErr(t, db, insertStmt, "sw1", "1.0.1", "src1", "", "1a", nil) | ||
execNoErr(t, db, insertStmt, "sw2", "2.0", "src2", "", "2", nil) | ||
execNoErr(t, db, insertStmt, "sw3", "3.0", "src3", "browser3", "3", nil) | ||
|
||
applyNext(t, db) | ||
|
||
var softwareTitles []fleet.SoftwareTitle | ||
require.NoError(t, db.SelectContext(context.Background(), &softwareTitles, `SELECT * FROM software_titles`)) | ||
require.Len(t, softwareTitles, 3) | ||
|
||
var software []fleet.Software | ||
require.NoError(t, db.SelectContext(context.Background(), &software, `SELECT id, name, source, browser, title_id FROM software`)) | ||
require.Len(t, software, 4) | ||
|
||
for _, sw := range software { | ||
require.NotNil(t, sw.TitleID) | ||
var found bool | ||
for _, title := range softwareTitles { | ||
if *sw.TitleID == title.ID { | ||
assert.Equal(t, sw.Name, title.Name) | ||
assert.Equal(t, sw.Source, title.Source) | ||
assert.Equal(t, sw.Browser, title.Browser) | ||
found = true | ||
break | ||
} | ||
} | ||
assert.True(t, found) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters