Skip to content

Commit

Permalink
remove snaps from trc table before deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
xbhouse committed Oct 18, 2024
1 parent 90348d2 commit 8798f4d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
1 change: 1 addition & 0 deletions pkg/dao/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,5 @@ type TemplateDao interface {
UpdateLastUpdateTask(ctx context.Context, taskUUID string, orgID string, templateUUID string) error
UpdateLastError(ctx context.Context, orgID string, templateUUID string, lastUpdateSnapshotError string) error
UpdateSnapshots(ctx context.Context, templateUUID string, repoUUIDs []string, snapshots []models.Snapshot) error
DeleteTemplateSnapshot(ctx context.Context, snapshotUUID string) error
}
10 changes: 10 additions & 0 deletions pkg/dao/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,16 @@ func (t templateDaoImpl) UpdateSnapshots(ctx context.Context, templateUUID strin
return nil
}

func (t templateDaoImpl) DeleteTemplateSnapshot(ctx context.Context, snapshotUUID string) error {
err := t.db.WithContext(ctx).Unscoped().Where("snapshot_uuid = ?", UuidifyString(snapshotUUID)).
Delete(models.TemplateRepositoryConfiguration{}).Error

if err != nil {
return t.DBToApiError(err)
}
return nil
}

func templatesCreateApiToModel(api api.TemplateRequest, model *models.Template) {
if api.Name != nil {
model.Name = *api.Name
Expand Down
18 changes: 18 additions & 0 deletions pkg/dao/templates_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/tasks/delete_repository_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (d *DeleteRepositorySnapshots) Run() error {
if err != nil {
return err
}
err = d.deleteTemplateSnapshot(snap.UUID)
if err != nil {
return err
}
err = d.deleteSnapshot(snap.UUID)
if err != nil {
return err
Expand Down Expand Up @@ -221,3 +225,11 @@ func (d *DeleteRepositorySnapshots) deleteCandlepinContent() error {

return nil
}

func (d *DeleteRepositorySnapshots) deleteTemplateSnapshot(snapshotUUID string) error {
err := d.daoReg.Template.DeleteTemplateSnapshot(d.ctx, snapshotUUID)
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions pkg/tasks/delete_repository_snapshots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func (s *DeleteRepositorySnapshotsSuite) TestDeleteSnapshotFull() {
s.mockDaoRegistry.Snapshot.On("FetchForRepoConfigUUID", ctx, repoConfig.UUID).Return([]models.Snapshot{expectedSnap}, nil).Once()
s.mockDaoRegistry.Snapshot.On("Delete", ctx, expectedSnap.UUID).Return(nil).Once()
s.mockDaoRegistry.RepositoryConfig.On("Delete", ctx, repoConfig.OrgID, repoConfig.UUID).Return(nil).Once()
s.mockDaoRegistry.Template.On("DeleteTemplateSnapshot", ctx, expectedSnap.UUID).Return(nil).Once()

s.MockPulpClient.On("PollTask", ctx, "taskHref").Return(&taskResp, nil).Times(3)
s.MockPulpClient.On("DeleteRpmRepositoryVersion", ctx, expectedSnap.VersionHref).Return(nil).Once()
Expand Down
21 changes: 5 additions & 16 deletions test/integration/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,7 @@ func (s *SnapshotSuite) TestSnapshot() {

// Create template and add repository to template
cpClient := candlepin_client.NewCandlepinClient()
environmentID, templateUUID := s.createTemplate(cpClient, s.ctx, repo, domainName)

// Delete the template
deleteTemplateTaskUuid, err := taskClient.Enqueue(queue.Task{
Typename: config.DeleteTemplatesTask,
Payload: tasks.DeleteTemplatesPayload{TemplateUUID: templateUUID, RepoConfigUUIDs: []string{repo.UUID}},
OrgId: repo.OrgID,
ObjectUUID: utils.Ptr(templateUUID),
ObjectType: utils.Ptr(config.ObjectTypeTemplate),
})
assert.NoError(s.T(), err)

s.WaitOnTask(deleteTemplateTaskUuid)
environmentID := s.createTemplate(cpClient, s.ctx, repo, domainName)

// Delete the repository
taskUuid, err := taskClient.Enqueue(queue.Task{
Expand Down Expand Up @@ -191,7 +179,8 @@ func (s *SnapshotSuite) TestSnapshot() {

environment, err := cpClient.FetchEnvironment(s.ctx, environmentID)
assert.Nil(s.T(), err)
require.Nil(s.T(), environment)
require.NotNil(s.T(), environment)
assert.Empty(s.T(), environment.GetEnvironmentContent())
}

type loggingTransport struct{}
Expand Down Expand Up @@ -324,7 +313,7 @@ func (s *SnapshotSuite) waitOnTask(taskUUID uuid2.UUID) *models.TaskInfo {
return taskInfo
}

func (s *SnapshotSuite) createTemplate(cpClient candlepin_client.CandlepinClient, ctx context.Context, repo api.RepositoryResponse, domainName string) (environmentID string, templateUUID string) {
func (s *SnapshotSuite) createTemplate(cpClient candlepin_client.CandlepinClient, ctx context.Context, repo api.RepositoryResponse, domainName string) (environmentID string) {
reqTemplate := api.TemplateRequest{
Name: utils.Ptr(fmt.Sprintf("test template %v", rand.Int())),
Description: utils.Ptr("includes rpm unsigned"),
Expand Down Expand Up @@ -352,7 +341,7 @@ func (s *SnapshotSuite) createTemplate(cpClient candlepin_client.CandlepinClient
environmentContent := environment.GetEnvironmentContent()
require.NotEmpty(s.T(), environmentContent)

return environmentID, payload.TemplateUUID
return environmentID
}

func (s *SnapshotSuite) updateTemplateContentAndWait(orgId string, tempUUID string, repoConfigUUIDS []string) payloads.UpdateTemplateContentPayload {
Expand Down

0 comments on commit 8798f4d

Please sign in to comment.