← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/launchpad:provide-oldest-first into launchpad:master

 

Jürgen Gmach has proposed merging ~jugmac00/launchpad:provide-oldest-first into launchpad:master.

Commit message:
Provide ascending sort order for bpph and spph

lp: #2086201


bpph: binarypackagepublishinghistory
spph: sourcepackagepublishinghistory

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #2086201 in Launchpad itself: "Provide oldest-first results for getPublished* methods"
  https://bugs.launchpad.net/launchpad/+bug/2086201

For more details, see:
https://code.launchpad.net/~jugmac00/launchpad/+git/launchpad/+merge/479370
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/launchpad:provide-oldest-first into launchpad:master.
diff --git a/lib/lp/soyuz/interfaces/archive.py b/lib/lp/soyuz/interfaces/archive.py
index 3641dd1..4d6b03e 100644
--- a/lib/lp/soyuz/interfaces/archive.py
+++ b/lib/lp/soyuz/interfaces/archive.py
@@ -612,6 +612,11 @@ class IArchiveSubscriberView(Interface):
             ),
             required=False,
         ),
+        order_by_date_ascending=Bool(
+            title=_("Order by ascending creation date"),
+            description=_("Return oldest results first."),
+            required=False,
+        ),
     )
     # Really ISourcePackagePublishingHistory, patched in
     # lp.soyuz.interfaces.webservice.
@@ -628,6 +633,7 @@ class IArchiveSubscriberView(Interface):
         created_since_date=None,
         component_name=None,
         order_by_date=False,
+        order_by_date_ascending=False,
     ):
         """All `ISourcePackagePublishingHistory` target to this archive."""
         # It loads additional related objects only needed in the API call
@@ -644,6 +650,7 @@ class IArchiveSubscriberView(Interface):
         eager_load=False,
         component_name=None,
         order_by_date=False,
+        order_by_date_ascending=False,
         include_removed=True,
         only_unpublished=False,
     ):
@@ -669,6 +676,8 @@ class IArchiveSubscriberView(Interface):
             If not specified, publications are ordered by source
             package name (lexicographically), then by descending version
             and then descending ID.
+        :param order_by_date_ascending: Order publications by descending
+            creation date and then by ascending ID.
         :param include_removed: If True, include publications that have been
             removed from disk as well as those that have not.
         :param only_unpublished: If True, only include publications that
@@ -749,6 +758,11 @@ class IArchiveSubscriberView(Interface):
             ),
             required=False,
         ),
+        order_by_date_ascending=Bool(
+            title=_("Order by ascending creation date"),
+            description=_("Return oldest results first."),
+            required=False,
+        ),
         component_name=TextLine(title=_("Component name"), required=False),
     )
     # Really IBinaryPackagePublishingHistory, patched in
@@ -767,6 +781,7 @@ class IArchiveSubscriberView(Interface):
         created_since_date=None,
         ordered=True,
         order_by_date=False,
+        order_by_date_ascending=False,
         include_removed=True,
         only_unpublished=False,
         eager_load=False,
diff --git a/lib/lp/soyuz/model/archive.py b/lib/lp/soyuz/model/archive.py
index 12fa3ac..11c9b17 100644
--- a/lib/lp/soyuz/model/archive.py
+++ b/lib/lp/soyuz/model/archive.py
@@ -26,6 +26,7 @@ from storm.databases.postgres import JSON as PgJSON
 from storm.expr import (
     Alias,
     And,
+    Asc,
     Cast,
     Count,
     Desc,
@@ -698,6 +699,7 @@ class Archive(StormBase):
         exact_match=False,
         created_since_date=None,
         order_by_date=False,
+        order_by_date_ascending=False,
         component_name=None,
     ):
         """See `IArchive`."""
@@ -714,6 +716,7 @@ class Archive(StormBase):
             eager_load=True,
             component_name=component_name,
             order_by_date=order_by_date,
+            order_by_date_ascending=order_by_date_ascending,
             include_removed=True,
         )
 
@@ -770,6 +773,7 @@ class Archive(StormBase):
         eager_load=False,
         component_name=None,
         order_by_date=False,
+        order_by_date_ascending=False,
         include_removed=True,
         only_unpublished=False,
     ):
@@ -781,13 +785,18 @@ class Archive(StormBase):
                 Desc(SourcePackagePublishingHistory.datecreated),
                 Desc(SourcePackagePublishingHistory.id),
             ]
+        elif order_by_date_ascending:
+            order_by = [
+                Asc(SourcePackagePublishingHistory.datecreated),
+                Asc(SourcePackagePublishingHistory.id),
+            ]
         else:
             order_by = [
                 SourcePackageName.name,
                 Desc(SourcePackagePublishingHistory.id),
             ]
 
-        if not order_by_date or name is not None:
+        if not (order_by_date or order_by_date_ascending) or name is not None:
             clauses.append(
                 SourcePackagePublishingHistory.sourcepackagename_id
                 == SourcePackageName.id
@@ -804,7 +813,10 @@ class Archive(StormBase):
             elif len(name) != 0:
                 clauses.append(SourcePackageName.name.is_in(name))
 
-        if not order_by_date or version is not None:
+        if (
+            not (order_by_date or order_by_date_ascending)
+            or version is not None
+        ):
             clauses.append(
                 SourcePackagePublishingHistory.sourcepackagerelease_id
                 == SourcePackageRelease.id
@@ -820,7 +832,7 @@ class Archive(StormBase):
                 Cast(SourcePackageRelease.version, "text")
                 == six.ensure_text(version)
             )
-        elif not order_by_date:
+        elif not (order_by_date or order_by_date_ascending):
             order_by.insert(1, Desc(SourcePackageRelease.version))
 
         if component_name is not None:
@@ -1001,6 +1013,7 @@ class Archive(StormBase):
         created_since_date=None,
         ordered=True,
         order_by_date=False,
+        order_by_date_ascending=False,
         include_removed=True,
         only_unpublished=False,
         need_bpr=False,
@@ -1012,7 +1025,7 @@ class Archive(StormBase):
         """
         clauses = [BinaryPackagePublishingHistory.archive == self]
 
-        if order_by_date:
+        if order_by_date or order_by_date_ascending:
             ordered = False
 
         if order_by_date:
@@ -1020,6 +1033,11 @@ class Archive(StormBase):
                 Desc(BinaryPackagePublishingHistory.datecreated),
                 Desc(BinaryPackagePublishingHistory.id),
             ]
+        elif order_by_date_ascending:
+            order_by = [
+                Asc(BinaryPackagePublishingHistory.datecreated),
+                Asc(BinaryPackagePublishingHistory.id),
+            ]
         elif ordered:
             order_by = [
                 BinaryPackageName.name,
@@ -1120,6 +1138,7 @@ class Archive(StormBase):
         created_since_date=None,
         ordered=True,
         order_by_date=False,
+        order_by_date_ascending=False,
         include_removed=True,
         only_unpublished=False,
         eager_load=False,
@@ -1140,6 +1159,7 @@ class Archive(StormBase):
             created_since_date=created_since_date,
             ordered=ordered,
             order_by_date=order_by_date,
+            order_by_date_ascending=order_by_date_ascending,
             include_removed=include_removed,
             only_unpublished=only_unpublished,
             component_name=component_name,
diff --git a/lib/lp/soyuz/tests/test_archive.py b/lib/lp/soyuz/tests/test_archive.py
index e5bb461..d0377de 100644
--- a/lib/lp/soyuz/tests/test_archive.py
+++ b/lib/lp/soyuz/tests/test_archive.py
@@ -3814,6 +3814,29 @@ class TestGetPublishedSources(TestCaseWithFactory):
             list(archive.getPublishedSources(order_by_date=True)),
         )
 
+    def test_xxxorder_by_date_ascending(self):
+        archive = self.factory.makeArchive()
+        middle_spph = self.factory.makeSourcePackagePublishingHistory(
+            archive=archive,
+            date_uploaded=datetime(2009, 1, 1, tzinfo=timezone.utc),
+        )
+        newest_spph = self.factory.makeSourcePackagePublishingHistory(
+            archive=archive,
+            date_uploaded=datetime(2025, 1, 1, tzinfo=timezone.utc),
+        )
+        oldest_spph = self.factory.makeSourcePackagePublishingHistory(
+            archive=archive,
+            date_uploaded=datetime(2000, 1, 1, tzinfo=timezone.utc),
+        )
+        expected_order = [oldest_spph, middle_spph, newest_spph]
+
+        self.assertEqual(
+            expected_order,
+            list(
+                archive.api_getPublishedSources(order_by_date_ascending=True)
+            ),
+        )
+
     def test_matches_version_as_text(self):
         # Versions such as 0.7-4 and 0.07-4 are equal according to the
         # "debversion" type, but for lookup purposes we compare the text of
@@ -5185,6 +5208,29 @@ class TestgetAllPublishedBinaries(TestCaseWithFactory):
             list(archive.getAllPublishedBinaries(order_by_date=True)),
         )
 
+    def test_order_by_date_ascending(self):
+        archive = self.factory.makeArchive()
+        middle_bpph = self.factory.makeBinaryPackagePublishingHistory(
+            archive=archive,
+            datecreated=datetime(2009, 1, 1, tzinfo=timezone.utc),
+        )
+        newest_bpph = self.factory.makeBinaryPackagePublishingHistory(
+            archive=archive,
+            datecreated=datetime(2025, 1, 1, tzinfo=timezone.utc),
+        )
+        oldest_bpph = self.factory.makeBinaryPackagePublishingHistory(
+            archive=archive,
+            datecreated=datetime(2000, 1, 1, tzinfo=timezone.utc),
+        )
+        expected_order = [oldest_bpph, middle_bpph, newest_bpph]
+
+        self.assertEqual(
+            expected_order,
+            list(
+                archive.getAllPublishedBinaries(order_by_date_ascending=True)
+            ),
+        )
+
     def test_matches_version_as_text(self):
         # Versions such as 0.7-4 and 0.07-4 are equal according to the
         # "debversion" type, but for lookup purposes we compare the text of