Merge ~jugmac00/launchpad:fix-charm-recipes-cannot-be-deleted into launchpad:master

Proposed by Jürgen Gmach
Status: Merged
Approved by: Jürgen Gmach
Approved revision: 28242324c9e5ef02fcde2c540b7d58c5a42133aa
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~jugmac00/launchpad:fix-charm-recipes-cannot-be-deleted
Merge into: launchpad:master
Diff against target: 82 lines (+21/-3)
2 files modified
lib/lp/charms/model/charmrecipe.py (+7/-0)
lib/lp/charms/tests/test_charmrecipe.py (+14/-3)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+418934@code.launchpad.net

Commit message

Fix charm recipes cannot be deleted

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/charms/model/charmrecipe.py b/lib/lp/charms/model/charmrecipe.py
2index 7c8c59a..f1bc919 100644
3--- a/lib/lp/charms/model/charmrecipe.py
4+++ b/lib/lp/charms/model/charmrecipe.py
5@@ -782,6 +782,13 @@ class CharmRecipe(StormBase, WebhookTargetMixin):
6 CharmFile.build = CharmRecipeBuild.id AND
7 CharmRecipeBuild.recipe = ?
8 """, (self.id,))
9+ store.execute("""
10+ DELETE FROM CharmRecipeBuildJob
11+ USING CharmRecipeBuild
12+ WHERE
13+ CharmRecipeBuildJob.build = CharmRecipeBuild.id AND
14+ CharmRecipeBuild.recipe = ?
15+ """, (self.id,))
16 store.find(CharmRecipeBuild, CharmRecipeBuild.recipe == self).remove()
17 affected_jobs = Select(
18 [CharmRecipeJob.job_id],
19diff --git a/lib/lp/charms/tests/test_charmrecipe.py b/lib/lp/charms/tests/test_charmrecipe.py
20index a5aac5c..b67d542 100644
21--- a/lib/lp/charms/tests/test_charmrecipe.py
22+++ b/lib/lp/charms/tests/test_charmrecipe.py
23@@ -73,11 +73,13 @@ from lp.charms.interfaces.charmrecipebuild import (
24 ICharmRecipeBuild,
25 ICharmRecipeBuildSet,
26 )
27+from lp.charms.interfaces.charmrecipebuildjob import ICharmhubUploadJobSource
28 from lp.charms.interfaces.charmrecipejob import (
29 ICharmRecipeRequestBuildsJobSource,
30 )
31 from lp.charms.model.charmrecipe import CharmRecipeSet
32 from lp.charms.model.charmrecipebuild import CharmFile
33+from lp.charms.model.charmrecipebuildjob import CharmRecipeBuildJob
34 from lp.charms.model.charmrecipejob import CharmRecipeJob
35 from lp.code.errors import GitRepositoryBlobNotFound
36 from lp.code.tests.helpers import GitHostingFixture
37@@ -955,7 +957,7 @@ class TestCharmRecipeDeleteWithBuilds(TestCaseWithFactory):
38 def test_delete_with_builds(self):
39 # A charm recipe with build requests and builds can be deleted.
40 # Doing so deletes all its build requests, their builds, and their
41- # files.
42+ # files, and any associated build jobs too.
43 owner = self.factory.makePerson()
44 project = self.factory.makeProduct()
45 distroseries = self.factory.makeDistroSeries()
46@@ -997,17 +999,23 @@ class TestCharmRecipeDeleteWithBuilds(TestCaseWithFactory):
47 [other_build] = requests[1].builds
48 charm_file = self.factory.makeCharmFile(build=build)
49 other_charm_file = self.factory.makeCharmFile(build=other_build)
50+ charm_build_job = getUtility(
51+ ICharmhubUploadJobSource).create(build)
52+ other_build_job = getUtility(
53+ ICharmhubUploadJobSource).create(other_build)
54 store = Store.of(condemned_recipe)
55 store.flush()
56 job_ids = [job.job_id for job in jobs]
57 build_id = build.id
58 build_queue_id = build.buildqueue_record.id
59 build_farm_job_id = removeSecurityProxy(build).build_farm_job_id
60+ charm_build_job_id = charm_build_job.job_id
61 charm_file_id = removeSecurityProxy(charm_file).id
62 with person_logged_in(condemned_recipe.owner):
63 condemned_recipe.destroySelf()
64 flush_database_caches()
65- # The deleted recipe, its build requests, and its builds are gone.
66+ # The deleted recipe, its build requests, its builds and the build job
67+ # are gone.
68 self.assertFalse(
69 getUtility(ICharmRecipeSet).exists(owner, project, "condemned"))
70 self.assertIsNone(store.get(CharmRecipeJob, job_ids[0]))
71@@ -1015,7 +1023,10 @@ class TestCharmRecipeDeleteWithBuilds(TestCaseWithFactory):
72 self.assertIsNone(store.get(BuildQueue, build_queue_id))
73 self.assertIsNone(store.get(BuildFarmJob, build_farm_job_id))
74 self.assertIsNone(store.get(CharmFile, charm_file_id))
75- # Unrelated build requests and builds are still present.
76+ self.assertIsNone(store.get(CharmRecipeBuildJob, charm_build_job_id))
77+ # Unrelated build requests, build jobs and builds are still present.
78+ self.assertIsNotNone(
79+ store.get(CharmRecipeBuildJob, other_build_job.job_id))
80 self.assertEqual(
81 removeSecurityProxy(jobs[1]).context,
82 store.get(CharmRecipeJob, job_ids[1]))

Subscribers

People subscribed via source and target branches

to status/vote changes: