← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~stevenk/launchpad/branch-information_type-garbo into lp:launchpad

 

Steve Kowalik has proposed merging lp:~stevenk/launchpad/branch-information_type-garbo into lp:launchpad with lp:~stevenk/launchpad/branch-information_type-model as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #933768 in Launchpad itself: "Update branch to use information_visibility_policy"
  https://bugs.launchpad.net/launchpad/+bug/933768

For more details, see:
https://code.launchpad.net/~stevenk/launchpad/branch-information_type-garbo/+merge/104683

Add a garbo job to populate Branch.information_type. Be very sneaky and use an SQL CASE statement to set it for purely speed reasons.
-- 
https://code.launchpad.net/~stevenk/launchpad/branch-information_type-garbo/+merge/104683
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~stevenk/launchpad/branch-information_type-garbo into lp:launchpad.
=== modified file 'lib/lp/scripts/garbo.py'
--- lib/lp/scripts/garbo.py	2012-04-25 14:10:24 +0000
+++ lib/lp/scripts/garbo.py	2012-05-04 06:08:20 +0000
@@ -51,6 +51,7 @@
     MAX_SAMPLE_SIZE,
     )
 from lp.code.interfaces.revision import IRevisionSet
+from lp.code.model.branch import Branch
 from lp.code.model.codeimportevent import CodeImportEvent
 from lp.code.model.codeimportresult import CodeImportResult
 from lp.code.model.revision import (
@@ -58,6 +59,7 @@
     RevisionCache,
     )
 from lp.hardwaredb.model.hwdb import HWSubmission
+from lp.registry.enums import InformationType
 from lp.registry.model.person import Person
 from lp.services.config import config
 from lp.services.database import postgresql
@@ -835,6 +837,30 @@
         transaction.commit()
 
 
+class BranchInformationTypeMigrator(TunableLoop):
+    """A `TunableLoop` to populate information_type for all branches."""
+
+    maximum_chunk_size = 5000
+
+    def __init__(self, log, abort_time=None):
+        super(BranchInformationTypeMigrator, self).__init__(log, abort_time)
+        self.transaction = transaction
+        self.store = IMasterStore(Branch)
+
+    def findBranches(self):
+        return self.store.find(Branch, Branch.information_type == None)
+
+    def isDone(self):
+        return self.findBranches().is_empty()
+
+    def __call__(self, chunk_size):
+        self.findBranches()[:chunk_size].set(
+            information_type=SQL("CASE WHEN transitively_private THEN %s "
+                "ELSE %s END" % sqlvalues(
+                    InformationType.USERDATA, InformationType.PUBLIC)))
+        self.transaction.commit()
+
+
 class BugWatchActivityPruner(BulkPruner):
     """A TunableLoop to prune BugWatchActivity entries."""
     target_table_class = BugWatchActivity
@@ -1382,6 +1408,7 @@
         DuplicateSessionPruner,
         BugHeatUpdater,
         BugTaskFlattener,
+        BranchInformationTypeMigrator,
         ]
     experimental_tunable_loops = []
 

=== modified file 'lib/lp/scripts/tests/test_garbo.py'
--- lib/lp/scripts/tests/test_garbo.py	2012-04-02 07:10:56 +0000
+++ lib/lp/scripts/tests/test_garbo.py	2012-05-04 06:08:20 +0000
@@ -55,6 +55,7 @@
     )
 from lp.code.model.codeimportevent import CodeImportEvent
 from lp.code.model.codeimportresult import CodeImportResult
+from lp.registry.enums import InformationType
 from lp.registry.interfaces.distribution import IDistributionSet
 from lp.registry.interfaces.person import IPersonSet
 from lp.scripts.garbo import (
@@ -1134,6 +1135,16 @@
                 'SELECT bugtask FROM BugTaskFlat WHERE bugtask = ?',
                 (task.id,)).get_one())
 
+    def test_BranchInformationTypeMigrator(self):
+        # A non-migrated branch will have information_type set correctly.
+        switch_dbuser('testadmin')
+        branch = self.factory.makeBranch(private=True)
+        # Since creating a branch will set information_type, unset it.
+        removeSecurityProxy(branch).information_type = None
+        transaction.commit()
+        self.runHourly()
+        self.assertEqual(InformationType.USERDATA, branch.information_type)
+
 
 class TestGarboTasks(TestCaseWithFactory):
     layer = LaunchpadZopelessLayer


Follow ups