Blame SOURCES/0001-history-Fix-dnf-history-rollback-when-a-package-was-removed-RhBug1683134.patch

288843
From a96d701f7f55ff475e11ac9cda63b81c31c54e7a Mon Sep 17 00:00:00 2001
288843
From: Daniel Mach <dmach@redhat.com>
288843
Date: Wed, 6 May 2020 08:34:46 +0200
288843
Subject: [PATCH] history: Fix dnf history rollback when a package was removed
288843
 (RhBug:1683134)
288843
288843
---
288843
 libdnf/transaction/MergedTransaction.cpp      |  25 +++-
288843
 .../transaction/MergedTransactionTest.cpp     | 120 ++++++++++++++++--
288843
 .../transaction/MergedTransactionTest.hpp     |   6 +-
288843
 3 files changed, 137 insertions(+), 14 deletions(-)
288843
288843
diff --git a/libdnf/transaction/MergedTransaction.cpp b/libdnf/transaction/MergedTransaction.cpp
288843
index a7c06ffa9..a8d878cb5 100644
288843
--- a/libdnf/transaction/MergedTransaction.cpp
288843
+++ b/libdnf/transaction/MergedTransaction.cpp
288843
@@ -19,6 +19,7 @@
288843
  */
288843
 
288843
 #include "MergedTransaction.hpp"
288843
+#include <algorithm>
288843
 #include <vector>
288843
 
288843
 namespace libdnf {
288843
@@ -171,6 +172,21 @@ MergedTransaction::getConsoleOutput()
288843
     return output;
288843
 }
288843
 
288843
+
288843
+static bool transaction_item_sort_function(const std::shared_ptr<TransactionItemBase> lhs, const std::shared_ptr<TransactionItemBase> rhs) {
288843
+    if (lhs->isForwardAction() && rhs->isForwardAction()) {
288843
+        return false;
288843
+    }
288843
+    if (lhs->isBackwardAction() && rhs->isBackwardAction()) {
288843
+        return false;
288843
+    }
288843
+    if (lhs->isBackwardAction()) {
288843
+        return true;
288843
+    }
288843
+    return false;
288843
+}
288843
+
288843
+
288843
 /**
288843
  * Get list of transaction items involved in the merged transaction
288843
  * Actions are merged using following rules:
288843
@@ -203,6 +219,9 @@ MergedTransaction::getItems()
288843
     // iterate over transaction
288843
     for (auto t : transactions) {
288843
         auto transItems = t->getItems();
288843
+        // sort transaction items by their action type - forward/backward
288843
+        // this fixes behavior of the merging algorithm in several edge cases
288843
+        std::sort(transItems.begin(), transItems.end(), transaction_item_sort_function);
288843
         // iterate over transaction items
288843
         for (auto transItem : transItems) {
288843
             // get item and its type
288843
@@ -383,10 +402,6 @@ MergedTransaction::mergeItem(ItemPairMap &itemPairMap, TransactionItemBasePtr mT
288843
     auto firstState = previousItemPair.first->getAction();
288843
     auto newState = mTransItem->getAction();
288843
 
288843
-    if (firstState == TransactionItemAction::INSTALL && mTransItem->isBackwardAction()) {
288843
-        return;
288843
-    }
288843
-
288843
     switch (firstState) {
288843
         case TransactionItemAction::REMOVE:
288843
         case TransactionItemAction::OBSOLETED:
288843
@@ -399,6 +414,8 @@ MergedTransaction::mergeItem(ItemPairMap &itemPairMap, TransactionItemBasePtr mT
288843
                 // Install -> Remove = (nothing)
288843
                 itemPairMap.erase(name);
288843
                 break;
288843
+            } else if (mTransItem->isBackwardAction()) {
288843
+                break;
288843
             }
288843
             // altered -> transfer install to the altered package
288843
             mTransItem->setAction(TransactionItemAction::INSTALL);
288843
diff --git a/tests/libdnf/transaction/MergedTransactionTest.cpp b/tests/libdnf/transaction/MergedTransactionTest.cpp
288843
index 90ad182cf..52507700b 100644
288843
--- a/tests/libdnf/transaction/MergedTransactionTest.cpp
288843
+++ b/tests/libdnf/transaction/MergedTransactionTest.cpp
288843
@@ -700,7 +700,7 @@ MergedTransactionTest::test_downgrade()
288843
 }
288843
 
288843
 void
288843
-MergedTransactionTest::test_install_downgrade()
288843
+MergedTransactionTest::test_install_downgrade_upgrade_remove()
288843
 {
288843
     auto trans1 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
288843
     trans1->addItem(
288843
@@ -724,19 +724,123 @@ MergedTransactionTest::test_install_downgrade()
288843
         TransactionItemReason::USER
288843
     );
288843
 
288843
+    auto trans3 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
288843
+    trans3->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.6-1.noarch"),
288843
+        "repo2",
288843
+        TransactionItemAction::UPGRADED,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+    trans3->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
288843
+        "repo1",
288843
+        TransactionItemAction::UPGRADE,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+
288843
+    auto trans4 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
288843
+    trans4->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
288843
+        "repo1",
288843
+        TransactionItemAction::REMOVE,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+
288843
     MergedTransaction merged(trans1);
288843
+
288843
+    // test merging trans1, trans2
288843
     merged.merge(trans2);
288843
+    auto items2 = merged.getItems();
288843
+    CPPUNIT_ASSERT_EQUAL(1, (int)items2.size());
288843
+    auto item2 = items2.at(0);
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.6-1.noarch"), item2->getItem()->toStr());
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("repo2"), item2->getRepoid());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::INSTALL, item2->getAction());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item2->getReason());
288843
 
288843
-    auto items = merged.getItems();
288843
-    CPPUNIT_ASSERT_EQUAL(1, (int)items.size());
288843
+    // test merging trans1, trans2, trans3
288843
+    merged.merge(trans3);
288843
+    auto items3 = merged.getItems();
288843
+    CPPUNIT_ASSERT_EQUAL(1, (int)items3.size());
288843
+    auto item3 = items3.at(0);
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.8-1.noarch"), item3->getItem()->toStr());
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("repo1"), item3->getRepoid());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::INSTALL, item3->getAction());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item3->getReason());
288843
 
288843
-    auto item = items.at(0);
288843
-    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.6-1.noarch"), item->getItem()->toStr());
288843
-    CPPUNIT_ASSERT_EQUAL(std::string("repo2"), item->getRepoid());
288843
-    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::INSTALL, item->getAction());
288843
-    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item->getReason());
288843
+    // test merging trans1, trans2, trans3, trans4
288843
+    merged.merge(trans4);
288843
+    auto items4 = merged.getItems();
288843
+    CPPUNIT_ASSERT_EQUAL(0, (int)items4.size());
288843
+    // trans4 removes the package, empty output is expected
288843
 }
288843
 
288843
+
288843
+void
288843
+MergedTransactionTest::test_downgrade_upgrade_remove()
288843
+{
288843
+    auto trans1 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
288843
+    trans1->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.6-1.noarch"),
288843
+        "repo2",
288843
+        TransactionItemAction::DOWNGRADE,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+    trans1->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
288843
+        "repo1",
288843
+        TransactionItemAction::DOWNGRADED,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+
288843
+    // items are in reversed order than in test_install_downgrade_upgrade_remove()
288843
+    // fixing this required ordering transaction items by forward/backward action
288843
+    auto trans2 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
288843
+    trans2->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
288843
+        "repo1",
288843
+        TransactionItemAction::UPGRADE,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+    trans2->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.6-1.noarch"),
288843
+        "repo2",
288843
+        TransactionItemAction::UPGRADED,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+
288843
+    auto trans3 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
288843
+    trans3->addItem(
288843
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
288843
+        "repo1",
288843
+        TransactionItemAction::REMOVE,
288843
+        TransactionItemReason::USER
288843
+    );
288843
+
288843
+    MergedTransaction merged(trans1);
288843
+
288843
+    // test merging trans1, trans2
288843
+    merged.merge(trans2);
288843
+    auto items2 = merged.getItems();
288843
+    CPPUNIT_ASSERT_EQUAL(1, (int)items2.size());
288843
+    auto item2 = items2.at(0);
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.8-1.noarch"), item2->getItem()->toStr());
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("repo1"), item2->getRepoid());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::REINSTALL, item2->getAction());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item2->getReason());
288843
+
288843
+    // test merging trans1, trans2, trans3
288843
+    merged.merge(trans3);
288843
+    auto items3 = merged.getItems();
288843
+    CPPUNIT_ASSERT_EQUAL(1, (int)items3.size());
288843
+    auto item3 = items3.at(0);
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.8-1.noarch"), item3->getItem()->toStr());
288843
+    CPPUNIT_ASSERT_EQUAL(std::string("repo1"), item3->getRepoid());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::REMOVE, item3->getAction());
288843
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item3->getReason());
288843
+}
288843
+
288843
+
288843
 void
288843
 MergedTransactionTest::test_multilib_identity()
288843
 {
288843
diff --git a/tests/libdnf/transaction/MergedTransactionTest.hpp b/tests/libdnf/transaction/MergedTransactionTest.hpp
288843
index 9f1ed660a..77585e865 100644
288843
--- a/tests/libdnf/transaction/MergedTransactionTest.hpp
288843
+++ b/tests/libdnf/transaction/MergedTransactionTest.hpp
288843
@@ -26,7 +26,8 @@ class MergedTransactionTest : public CppUnit::TestCase {
288843
     CPPUNIT_TEST(test_add_obsoleted_obsoleted);
288843
 
288843
     CPPUNIT_TEST(test_downgrade);
288843
-    CPPUNIT_TEST(test_install_downgrade);
288843
+    CPPUNIT_TEST(test_install_downgrade_upgrade_remove);
288843
+    CPPUNIT_TEST(test_downgrade_upgrade_remove);
288843
 
288843
     CPPUNIT_TEST(test_multilib_identity);
288843
 
288843
@@ -56,7 +57,8 @@ class MergedTransactionTest : public CppUnit::TestCase {
288843
     // END: tests ported from DNF unit tests
288843
 
288843
     void test_downgrade();
288843
-    void test_install_downgrade();
288843
+    void test_install_downgrade_upgrade_remove();
288843
+    void test_downgrade_upgrade_remove();
288843
 
288843
     void test_multilib_identity();
288843
 private: