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
=== modified file 'lib/lp/code/model/branch.py'
--- lib/lp/code/model/branch.py 2012-09-14 01:21:13 +0000
+++ lib/lp/code/model/branch.py 2012-09-18 04:16:18 +0000
@@ -65,6 +65,7 @@
65from lp.bugs.interfaces.bugtask import IBugTaskSet65from lp.bugs.interfaces.bugtask import IBugTaskSet
66from lp.bugs.interfaces.bugtaskfilter import filter_bugtasks_by_context66from lp.bugs.interfaces.bugtaskfilter import filter_bugtasks_by_context
67from lp.bugs.interfaces.bugtasksearch import BugTaskSearchParams67from lp.bugs.interfaces.bugtasksearch import BugTaskSearchParams
68from lp.buildmaster.model.buildfarmjob import BuildFarmJob
68from lp.buildmaster.model.buildqueue import BuildQueue69from lp.buildmaster.model.buildqueue import BuildQueue
69from lp.code.bzr import (70from lp.code.bzr import (
70 BranchFormat,71 BranchFormat,
@@ -1252,12 +1253,20 @@
1252 # the affected Jobs in the database otherwise.1253 # the affected Jobs in the database otherwise.
1253 store.find(BuildQueue, BuildQueue.jobID.is_in(affected_jobs)).remove()1254 store.find(BuildQueue, BuildQueue.jobID.is_in(affected_jobs)).remove()
12541255
1256 # Find BuildFarmJobs to delete.
1257 bfjs = store.find(
1258 (BuildFarmJob.id,),
1259 TranslationTemplatesBuild.build_farm_job_id == BuildFarmJob.id,
1260 TranslationTemplatesBuild.branch == self)
1261 bfj_ids = [bfj[0] for bfj in bfjs]
1262
1255 # Delete Jobs. Their BranchJobs cascade along in the database.1263 # Delete Jobs. Their BranchJobs cascade along in the database.
1256 store.find(Job, Job.id.is_in(affected_jobs)).remove()1264 store.find(Job, Job.id.is_in(affected_jobs)).remove()
12571265
1258 store.find(1266 store.find(
1259 TranslationTemplatesBuild,1267 TranslationTemplatesBuild,
1260 TranslationTemplatesBuild.branch == self).remove()1268 TranslationTemplatesBuild.branch == self).remove()
1269 store.find(BuildFarmJob, BuildFarmJob.id.is_in(bfj_ids)).remove()
12611270
1262 def destroySelf(self, break_references=False):1271 def destroySelf(self, break_references=False):
1263 """See `IBranch`."""1272 """See `IBranch`."""
12641273
=== modified file 'lib/lp/code/model/tests/test_branch.py'
--- lib/lp/code/model/tests/test_branch.py 2012-08-30 21:59:39 +0000
+++ lib/lp/code/model/tests/test_branch.py 2012-09-18 04:16:18 +0000
@@ -39,6 +39,7 @@
39 IBugSet,39 IBugSet,
40 )40 )
41from lp.bugs.model.bugbranch import BugBranch41from lp.bugs.model.bugbranch import BugBranch
42from lp.buildmaster.model.buildfarmjob import BuildFarmJob
42from lp.buildmaster.model.buildqueue import BuildQueue43from lp.buildmaster.model.buildqueue import BuildQueue
43from lp.code.bzr import (44from lp.code.bzr import (
44 BranchFormat,45 BranchFormat,
@@ -166,6 +167,9 @@
166 LaunchpadZopelessLayer,167 LaunchpadZopelessLayer,
167 ZopelessAppServerLayer,168 ZopelessAppServerLayer,
168 )169 )
170from lp.translations.model.translationtemplatesbuild import (
171 TranslationTemplatesBuild,
172 )
169from lp.translations.model.translationtemplatesbuildjob import (173from lp.translations.model.translationtemplatesbuildjob import (
170 ITranslationTemplatesBuildJobSource,174 ITranslationTemplatesBuildJobSource,
171 )175 )
@@ -1374,17 +1378,25 @@
1374 job = source.create(branch)1378 job = source.create(branch)
1375 other_job = source.create(other_branch)1379 other_job = source.create(other_branch)
1376 store = Store.of(branch)1380 store = Store.of(branch)
1381 bfj = store.find(
1382 BuildFarmJob,
1383 BuildFarmJob.id == TranslationTemplatesBuild.build_farm_job_id,
1384 TranslationTemplatesBuild.branch == branch).one().id
13771385
1378 branch.destroySelf(break_references=True)1386 branch.destroySelf(break_references=True)
13791387
1380 # The BuildQueue for the job whose branch we deleted is gone.1388 # The BuildQueue for the job whose branch we deleted is gone.
1381 buildqueue = store.find(BuildQueue, BuildQueue.job == job.job)1389 buildqueue = store.find(BuildQueue, BuildQueue.job == job.job)
1382 self.assertEqual([], list(buildqueue))1390 self.assertEqual(0, buildqueue.count())
1391
1392 # The BuildFarmJob for the TTB is gone.
1393 bfjs = store.find(BuildFarmJob, BuildFarmJob.id == bfj)
1394 self.assertEqual(0, bfjs.count())
13831395
1384 # The other job's BuildQueue entry is still there.1396 # The other job's BuildQueue entry is still there.
1385 other_buildqueue = store.find(1397 other_buildqueue = store.find(
1386 BuildQueue, BuildQueue.job == other_job.job)1398 BuildQueue, BuildQueue.job == other_job.job)
1387 self.assertNotEqual([], list(other_buildqueue))1399 self.assertEqual(1, other_buildqueue.count())
13881400
1389 def test_createsJobToReclaimSpace(self):1401 def test_createsJobToReclaimSpace(self):
1390 # When a branch is deleted from the database, a job to remove the1402 # When a branch is deleted from the database, a job to remove the