-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix VPP migration edge case (#22460)
#22415 # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files) for more information. - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [x] Added/updated tests - [x] If database migrations are included, checked table schema to confirm autoupdate - For database migrations: - [x] Checked schema for all modified table for columns that will auto-update timestamps during migration. - [x] Confirmed that updating the timestamps is acceptable, and will not cause unwanted side effects. - [x] Ensured the correct collation is explicitly set for character columns (`COLLATE utf8mb4_unicode_ci`). - [x] Manual QA for all new/changed functionality
- Loading branch information
1 parent
d000776
commit 69d07b3
Showing
3 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
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 @@ | ||
* Fixed an issue with the migration adding support for multiple VPP tokens that would happen if a token is removed prior to upgrading Fleet. |
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
70 changes: 70 additions & 0 deletions
70
server/datastore/mysql/migrations/tables/20240829170033_AddVPPTokenIDToVppAppsTeams_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,70 @@ | ||
package tables | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUp_20240829170033_Existing(t *testing.T) { | ||
db := applyUpToPrev(t) | ||
|
||
// insert a vpp token | ||
vppTokenID := execNoErrLastID(t, db, "INSERT INTO vpp_tokens (organization_name, location, renew_at, token) VALUES (?, ?, ?, ?)", "org", "location", time.Now(), "token") | ||
|
||
// create a couple teams | ||
tm1 := execNoErrLastID(t, db, "INSERT INTO teams (name) VALUES ('team1')") | ||
tm2 := execNoErrLastID(t, db, "INSERT INTO teams (name) VALUES ('team2')") | ||
|
||
// create a couple of vpp apps | ||
adamID1 := "123" | ||
execNoErr(t, db, `INSERT INTO vpp_apps (adam_id, platform) VALUES (?, "iOS")`, adamID1) | ||
adamID2 := "456" | ||
execNoErr(t, db, `INSERT INTO vpp_apps (adam_id, platform) VALUES (?, "iOS")`, adamID2) | ||
|
||
// insert some teams with vpp apps | ||
execNoErr(t, db, `INSERT INTO vpp_apps_teams (adam_id, team_id, global_or_team_id, platform, self_service) VALUES (?, ?, ?, ?, ?)`, adamID1, tm1, 0, "iOS", 0) | ||
execNoErr(t, db, `INSERT INTO vpp_apps_teams (adam_id, team_id, global_or_team_id, platform, self_service) VALUES (?, ?, ?, ?, ?)`, adamID2, tm2, 0, "iOS", 0) | ||
|
||
// apply current migration | ||
applyNext(t, db) | ||
|
||
// ensure vpp_token_id is set for all teams | ||
var vppTokenIDs []int | ||
err := sqlx.Select(db, &vppTokenIDs, `SELECT vpp_token_id FROM vpp_apps_teams`) | ||
require.NoError(t, err) | ||
require.Len(t, vppTokenIDs, 2) | ||
for _, tokenID := range vppTokenIDs { | ||
require.Equal(t, int(vppTokenID), tokenID) | ||
} | ||
} | ||
|
||
func TestUp_20240829170033_NoTokens(t *testing.T) { | ||
db := applyUpToPrev(t) | ||
|
||
// create a couple teams | ||
tm1 := execNoErrLastID(t, db, "INSERT INTO teams (name) VALUES ('team1')") | ||
tm2 := execNoErrLastID(t, db, "INSERT INTO teams (name) VALUES ('team2')") | ||
|
||
// create a couple of vpp apps | ||
adamID1 := "123" | ||
execNoErr(t, db, `INSERT INTO vpp_apps (adam_id, platform) VALUES (?, "iOS")`, adamID1) | ||
adamID2 := "456" | ||
execNoErr(t, db, `INSERT INTO vpp_apps (adam_id, platform) VALUES (?, "iOS")`, adamID2) | ||
|
||
// insert some teams with vpp apps | ||
execNoErr(t, db, `INSERT INTO vpp_apps_teams (adam_id, team_id, global_or_team_id, platform, self_service) VALUES (?, ?, ?, ?, ?)`, adamID1, tm1, 0, "iOS", 0) | ||
execNoErr(t, db, `INSERT INTO vpp_apps_teams (adam_id, team_id, global_or_team_id, platform, self_service) VALUES (?, ?, ?, ?, ?)`, adamID2, tm2, 0, "iOS", 0) | ||
|
||
// apply current migration | ||
applyNext(t, db) | ||
|
||
// ensure no rows are left in vpp_apps_teams (since there are no tokens) | ||
var count int | ||
err := sqlx.Get(db, &count, `SELECT COUNT(*) FROM vpp_apps_teams`) | ||
require.NoError(t, err) | ||
require.Zero(t, count) | ||
|
||
} |