From d26fe23f19f6f4836d690aee5afd357d4d9ae117 Mon Sep 17 00:00:00 2001 From: Konstantin Sykulev Date: Sat, 8 Aug 2026 01:13:51 -0500 Subject: [PATCH] Fix DEP sync re-opening completed MDM migrations --- server/datastore/mysql/apple_mdm.go | 5 +++++ server/datastore/mysql/apple_mdm_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/server/datastore/mysql/apple_mdm.go b/server/datastore/mysql/apple_mdm.go index a8aee928d4e..5bc0d5f1aa3 100644 --- a/server/datastore/mysql/apple_mdm.go +++ b/server/datastore/mysql/apple_mdm.go @@ -2028,6 +2028,11 @@ func upsertHostDEPAssignmentsDB(ctx context.Context, tx sqlx.ExtContext, hosts [ added_at = CURRENT_TIMESTAMP, deleted_at = NULL, abm_token_id = VALUES(abm_token_id), + mdm_migration_completed = IF( + mdm_migration_completed IS NOT NULL AND mdm_migration_completed >= mdm_migration_deadline, + VALUES(mdm_migration_deadline), + mdm_migration_completed + ), mdm_migration_deadline = VALUES(mdm_migration_deadline), hardware_serial = VALUES(hardware_serial)` diff --git a/server/datastore/mysql/apple_mdm_test.go b/server/datastore/mysql/apple_mdm_test.go index 029d17c994d..c30f85781b5 100644 --- a/server/datastore/mysql/apple_mdm_test.go +++ b/server/datastore/mysql/apple_mdm_test.go @@ -5678,6 +5678,16 @@ func TestHostDEPAssignments(t *testing.T) { require.Equal(t, depHostID, hdepa.HostID) require.Nil(t, hdepa.DeletedAt) require.Equal(t, depAssignment.AddedAt, hdepa.AddedAt) + + // Regression: re-syncing with a new migration deadline after migration was + // completed should preserve the completed state (not re-open the migration). + newDeadline := migrationDeadline.Add(48 * time.Hour).Truncate(time.Millisecond) + n, err = ds.IngestMDMAppleDevicesFromDEPSync(ctx, []godep.Device{{SerialNumber: depSerial, MDMMigrationDeadline: &newDeadline}}, abmToken.ID, nil, nil, nil) + require.NoError(t, err) + + checkinInfo, err = ds.GetHostMDMCheckinInfo(ctx, depUUID) + require.NoError(t, err) + require.False(t, checkinInfo.MigrationInProgress, "re-sync with new deadline should not re-open a completed migration") }) t.Run("manual enrollment", func(t *testing.T) {