← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~pappacena/launchpad:optimizing-product-vcs-garbo into launchpad:master

 

Thiago F. Pappacena has proposed merging ~pappacena/launchpad:optimizing-product-vcs-garbo into launchpad:master.

Commit message:
Fetching less objects from the database on ProductVCSPopulator garbo job.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~pappacena/launchpad/+git/launchpad/+merge/386633
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~pappacena/launchpad:optimizing-product-vcs-garbo into launchpad:master.
diff --git a/lib/lp/scripts/garbo.py b/lib/lp/scripts/garbo.py
index cf0bc4f..70e168e 100644
--- a/lib/lp/scripts/garbo.py
+++ b/lib/lp/scripts/garbo.py
@@ -1,4 +1,4 @@
-# Copyright 2009-2019 Canonical Ltd.  This software is licensed under the
+# Copyright 2009-2020 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
 """Database garbage collection."""
@@ -1492,8 +1492,10 @@ class ProductVCSPopulator(TunableLoop):
         self.store = IMasterStore(Product)
 
     def findProducts(self):
-        return self.store.find(
-            Product, Product.id >= self.start_at).order_by(Product.id)
+        products = self.store.find(
+            Product,
+            Product.id >= self.start_at, Product.vcs == None)
+        return products.order_by(Product.id)
 
     def isDone(self):
         return self.findProducts().is_empty()
@@ -1501,8 +1503,7 @@ class ProductVCSPopulator(TunableLoop):
     def __call__(self, chunk_size):
         products = list(self.findProducts()[:chunk_size])
         for product in products:
-            if not product.vcs:
-                product.vcs = product.inferred_vcs
+            product.vcs = product.inferred_vcs
         self.start_at = products[-1].id + 1
         transaction.commit()
 
diff --git a/lib/lp/scripts/tests/test_garbo.py b/lib/lp/scripts/tests/test_garbo.py
index b74d0a8..2d6efd9 100644
--- a/lib/lp/scripts/tests/test_garbo.py
+++ b/lib/lp/scripts/tests/test_garbo.py
@@ -92,6 +92,7 @@ from lp.scripts.garbo import (
     load_garbo_job_state,
     LoginTokenPruner,
     OpenIDConsumerAssociationPruner,
+    ProductVCSPopulator,
     save_garbo_job_state,
     UnusedPOTMsgSetPruner,
     UnusedSessionPruner,
@@ -1344,6 +1345,24 @@ class TestGarbo(FakeAdapterMixin, TestCaseWithFactory):
 
         self.assertEqual(VCSType.GIT, product.vcs)
 
+    def test_ProductVCSPopulator_findProducts_filters_correctly(self):
+        switch_dbuser('testadmin')
+
+        # Create 2 products: one with VCS set, and another one without.
+        product = self.factory.makeProduct()
+        self.assertIsNone(product.vcs)
+
+        product_with_vcs = self.factory.makeProduct(vcs=VCSType.GIT)
+        self.assertIsNotNone(product_with_vcs.vcs)
+
+        populator = ProductVCSPopulator(None)
+        # Consider only products created by this test.
+        populator.start_at = product.id
+
+        rs = populator.findProducts()
+        self.assertEqual(1, rs.count())
+        self.assertEqual(product, rs.one())
+
     def test_PopulateDistributionSourcePackageCache(self):
         switch_dbuser('testadmin')
         # Make some test data.  We create source publications for different