Skip to content

Commit a7d2a08

Browse files
authored
Fixes 4735: snapshot repos even if last task was deleted (#824)
1 parent f9baa6a commit a7d2a08

7 files changed

+42
-5
lines changed

db/migrations.latest

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20240814145645
1+
20240917101025
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
BEGIN;
2+
3+
ALTER TABLE repository_configurations
4+
DROP CONSTRAINT IF EXISTS fk_last_snapshot_task;
5+
6+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
BEGIN;
2+
3+
update repository_configurations set last_snapshot_task_uuid = null where last_snapshot_task_uuid not in (select id from tasks);
4+
5+
ALTER TABLE repository_configurations
6+
DROP CONSTRAINT IF EXISTS fk_last_snapshot_task,
7+
ADD CONSTRAINT fk_last_snapshot_task
8+
FOREIGN KEY (last_snapshot_task_uuid)
9+
REFERENCES tasks(id)
10+
ON DELETE SET NULL;
11+
12+
COMMIT;

pkg/dao/repository_configs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (r repositoryConfigDaoImpl) extraReposToSnapshot(pdb *gorm.DB, notIn *gorm.
261261
Joins("LEFT JOIN tasks on last_snapshot_task_uuid = tasks.id").
262262
Where("repository_configurations.uuid not in (?)", notIn.Select("repository_configurations.uuid")).
263263
Where(pdb.Or("tasks.status NOT IN ?", []string{config.TaskStatusPending, config.TaskStatusRunning})).
264-
Order("tasks.finished_at ASC").Limit(count).Find(&extra)
264+
Order("tasks.finished_at ASC NULLS FIRST").Limit(count).Find(&extra)
265265
return extra, query.Error
266266
}
267267

pkg/dao/repository_configs_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,10 @@ func (suite *RepositoryConfigSuite) TestUpdateLastSnapshotTask() {
17821782
assert.NoError(t, err)
17831783
assert.Len(t, uuids, repoConfigCount)
17841784

1785-
taskUUID := uuid.NewString()
1785+
tasks, err := seeds.SeedTasks(suite.tx, 1, seeds.TaskSeedOptions{Status: "finished"})
1786+
require.NoError(t, err)
1787+
1788+
taskUUID := tasks[0].Id.String()
17861789

17871790
err = dao.UpdateLastSnapshotTask(context.Background(), taskUUID, orgID, uuids[0])
17881791
assert.Nil(t, err)

pkg/dao/task_info_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,19 @@ func (suite *TaskInfoSuite) createTemplateTask(orgID string) (models.TaskInfo, a
790790

791791
return task, template
792792
}
793+
794+
func (suite *TaskInfoSuite) TestDeleteTask() {
795+
t := suite.T()
796+
tasks, err := seeds.SeedTasks(suite.tx, 1, seeds.TaskSeedOptions{Status: "finished"})
797+
require.NoError(t, err)
798+
taskUUID := tasks[0].Id.String()
799+
800+
repos, err := seeds.SeedRepositoryConfigurations(suite.tx, 1, seeds.SeedOptions{OrgID: "someOrg", TaskID: taskUUID})
801+
assert.NoError(t, err)
802+
assert.NoError(t, suite.tx.Model(&models.TaskInfo{}).Where("id = ?", taskUUID).Delete(tasks[0]).Error)
803+
804+
rc := models.RepositoryConfiguration{}
805+
assert.NoError(t, suite.tx.Where("uuid = ?", repos[0].UUID).First(&rc).Error)
806+
assert.Equal(t, repos[0].UUID, rc.UUID)
807+
assert.Empty(t, rc.LastSnapshotTaskUUID)
808+
}

pkg/tasks/queue/pgqueue.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const (
118118
DELETE FROM task_heartbeats
119119
WHERE id = $1`
120120
sqlDeleteAllTasks = `
121-
TRUNCATE tasks, task_heartbeats, task_dependencies`
121+
TRUNCATE task_heartbeats, task_dependencies; DELETE FROM TASKS;`
122122
)
123123

124124
// These interfaces represent all the interactions with pgxpool that are needed for the pgqueue
@@ -821,7 +821,7 @@ func (p *PgQueue) IdFromToken(token uuid.UUID) (id uuid.UUID, isRunning bool, er
821821
func (p *PgQueue) RemoveAllTasks() error {
822822
_, err := p.Pool.Exec(context.Background(), sqlDeleteAllTasks)
823823
if err != nil {
824-
return fmt.Errorf("error removing all tasks")
824+
return fmt.Errorf("error removing all tasks: %w", err)
825825
}
826826
return nil
827827
}

0 commit comments

Comments
 (0)