Merge lp:~stevenk/launchpad/destroy-bfj-on-branch-delete into lp:launchpad

Proposed by Steve Kowalik
Status: Merged
Approved by: Steve Kowalik
Approved revision: no longer in the source branch.
Merged at revision: 15971
Proposed branch: lp:~stevenk/launchpad/destroy-bfj-on-branch-delete
Merge into: lp:launchpad
Diff against target: 82 lines (+23/-2)
2 files modified
lib/lp/code/model/branch.py (+9/-0)
lib/lp/code/model/tests/test_branch.py (+14/-2)
To merge this branch: bzr merge lp:~stevenk/launchpad/destroy-bfj-on-branch-delete
Reviewer Review Type Date Requested Status
Ian Booth (community) Approve
Review via email: mp+124828@code.launchpad.net

Commit message

Also delete BuildFarmJobs in Branch._deleteJobs() to avoid InconsistentBuildFarmJobError on Builder:+history.

Description of the change

Branch._deleteJobs() made the assumption that ON DELETE CASCADE would remove all references to the TTB that was being deleted. Which will remove everything but the BuildFarmJob. So delete that, and add a test that makes sure Builder:+history deals with it.

To post a comment you must log in.
Revision history for this message
Ian Booth (wallyworld) wrote :

Looks ok, but bear in mind I am not an expert in this area.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/model/branch.py'
2--- lib/lp/code/model/branch.py 2012-09-14 01:21:13 +0000
3+++ lib/lp/code/model/branch.py 2012-09-18 04:16:18 +0000
4@@ -65,6 +65,7 @@
5 from lp.bugs.interfaces.bugtask import IBugTaskSet
6 from lp.bugs.interfaces.bugtaskfilter import filter_bugtasks_by_context
7 from lp.bugs.interfaces.bugtasksearch import BugTaskSearchParams
8+from lp.buildmaster.model.buildfarmjob import BuildFarmJob
9 from lp.buildmaster.model.buildqueue import BuildQueue
10 from lp.code.bzr import (
11 BranchFormat,
12@@ -1252,12 +1253,20 @@
13 # the affected Jobs in the database otherwise.
14 store.find(BuildQueue, BuildQueue.jobID.is_in(affected_jobs)).remove()
15
16+ # Find BuildFarmJobs to delete.
17+ bfjs = store.find(
18+ (BuildFarmJob.id,),
19+ TranslationTemplatesBuild.build_farm_job_id == BuildFarmJob.id,
20+ TranslationTemplatesBuild.branch == self)
21+ bfj_ids = [bfj[0] for bfj in bfjs]
22+
23 # Delete Jobs. Their BranchJobs cascade along in the database.
24 store.find(Job, Job.id.is_in(affected_jobs)).remove()
25
26 store.find(
27 TranslationTemplatesBuild,
28 TranslationTemplatesBuild.branch == self).remove()
29+ store.find(BuildFarmJob, BuildFarmJob.id.is_in(bfj_ids)).remove()
30
31 def destroySelf(self, break_references=False):
32 """See `IBranch`."""
33
34=== modified file 'lib/lp/code/model/tests/test_branch.py'
35--- lib/lp/code/model/tests/test_branch.py 2012-08-30 21:59:39 +0000
36+++ lib/lp/code/model/tests/test_branch.py 2012-09-18 04:16:18 +0000
37@@ -39,6 +39,7 @@
38 IBugSet,
39 )
40 from lp.bugs.model.bugbranch import BugBranch
41+from lp.buildmaster.model.buildfarmjob import BuildFarmJob
42 from lp.buildmaster.model.buildqueue import BuildQueue
43 from lp.code.bzr import (
44 BranchFormat,
45@@ -166,6 +167,9 @@
46 LaunchpadZopelessLayer,
47 ZopelessAppServerLayer,
48 )
49+from lp.translations.model.translationtemplatesbuild import (
50+ TranslationTemplatesBuild,
51+ )
52 from lp.translations.model.translationtemplatesbuildjob import (
53 ITranslationTemplatesBuildJobSource,
54 )
55@@ -1374,17 +1378,25 @@
56 job = source.create(branch)
57 other_job = source.create(other_branch)
58 store = Store.of(branch)
59+ bfj = store.find(
60+ BuildFarmJob,
61+ BuildFarmJob.id == TranslationTemplatesBuild.build_farm_job_id,
62+ TranslationTemplatesBuild.branch == branch).one().id
63
64 branch.destroySelf(break_references=True)
65
66 # The BuildQueue for the job whose branch we deleted is gone.
67 buildqueue = store.find(BuildQueue, BuildQueue.job == job.job)
68- self.assertEqual([], list(buildqueue))
69+ self.assertEqual(0, buildqueue.count())
70+
71+ # The BuildFarmJob for the TTB is gone.
72+ bfjs = store.find(BuildFarmJob, BuildFarmJob.id == bfj)
73+ self.assertEqual(0, bfjs.count())
74
75 # The other job's BuildQueue entry is still there.
76 other_buildqueue = store.find(
77 BuildQueue, BuildQueue.job == other_job.job)
78- self.assertNotEqual([], list(other_buildqueue))
79+ self.assertEqual(1, other_buildqueue.count())
80
81 def test_createsJobToReclaimSpace(self):
82 # When a branch is deleted from the database, a job to remove the