Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit d4a0c3b

Browse files
committed
fix/graphql/database: fix repo permission sync count to ignore soft deleted repositories
1 parent 1ea3cc5 commit d4a0c3b

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

internal/database/permission_sync_jobs.go

+24-10
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,8 @@ FROM
722722
JOIN users ON users.id = latest_user_sync_jobs.user_id
723723
WHERE
724724
latest_user_sync_jobs.state = 'failed'
725-
AND users.deleted_at IS NULL; -- exclude jobs from soft-deleted users (hard deleted users are taken care of by the CASCADE foreign key constraint)
725+
AND users.deleted_at IS NULL;
726+
-- exclude jobs from soft-deleted users (hard deleted users are taken care of by the CASCADE foreign key constraint)
726727
`
727728

728729
// CountUsersWithFailingSyncJob returns count of users with LATEST sync job failing.
@@ -735,16 +736,29 @@ func (s *permissionSyncJobStore) CountUsersWithFailingSyncJob(ctx context.Contex
735736
}
736737

737738
const countReposWithFailingSyncJobsQuery = `
738-
SELECT COUNT(*)
739-
FROM (
740-
SELECT DISTINCT ON (repository_id) id, state
741-
FROM permission_sync_jobs
739+
WITH latest_repo_sync_jobs AS (
740+
SELECT
741+
DISTINCT ON (repository_id) repository_id,
742+
id,
743+
state
744+
FROM
745+
permission_sync_jobs
742746
WHERE
743-
repository_id is NOT NULL
744-
AND state IN ('completed', 'failed')
745-
ORDER BY repository_id, finished_at DESC
746-
) AS tmp
747-
WHERE state = 'failed';
747+
repository_id is NOT NULL
748+
AND state IN ('completed', 'failed')
749+
ORDER BY
750+
repository_id,
751+
finished_at DESC
752+
)
753+
SELECT
754+
COUNT(*)
755+
FROM
756+
latest_repo_sync_jobs
757+
JOIN repo ON repo.id = latest_repo_sync_jobs.repository_id
758+
WHERE
759+
latest_repo_sync_jobs.state = 'failed'
760+
AND repo.deleted_at IS NULL;
761+
-- exclude jobs from soft-deleted repos (hard deleted repos are taken care of by the CASCADE foreign key constraint)
748762
`
749763

750764
// CountReposWithFailingSyncJob returns count of repos with LATEST sync job failing.

internal/database/permission_sync_jobs_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,31 @@ func TestPermissionSyncJobs_CountReposWithFailingSyncJob(t *testing.T) {
10041004
require.NoError(t, err)
10051005
require.Equal(t, int32(2), count, "wrong count")
10061006
})
1007+
1008+
t.Run("Should not count jobs with a deleted repo", func(t *testing.T) {
1009+
cleanupSyncJobs(t, db, ctx)
1010+
tempRepo := types.Repo{Name: "test-repo-tmp", ID: 301}
1011+
err := reposStore.Create(ctx, &tempRepo)
1012+
require.NoError(t, err)
1013+
t.Cleanup(func() {
1014+
_ = reposStore.Delete(ctx, tempRepo.ID) // Ensure we clean up after ourselves.
1015+
})
1016+
1017+
createSyncJob(t, store, ctx, 0, tempRepo.ID) // id = 1
1018+
finishSyncJobWithFailure(t, db, ctx, 1, clock.Now().Add(-1*time.Hour))
1019+
1020+
count, err := store.CountReposWithFailingSyncJob(ctx)
1021+
require.NoError(t, err)
1022+
require.Equal(t, int32(1), count, "wrong count")
1023+
1024+
// Delete tempRepo
1025+
err = reposStore.Delete(ctx, tempRepo.ID)
1026+
require.NoError(t, err)
1027+
count, err = store.CountReposWithFailingSyncJob(ctx)
1028+
require.NoError(t, err)
1029+
1030+
require.Equal(t, int32(0), count, "wrong count")
1031+
})
10071032
}
10081033

10091034
// createSyncJobs creates 10 sync jobs, half with the ReasonManualUserSync reason

0 commit comments

Comments
 (0)