Repository: conan-io/conan
Pull request #16417 (2024-06-06 11:44:52)

Fix back migration of default compatibility.py from a clean install
Changelog: Fix: Fix back migration of default compatibility.py from a clean install
Docs: omit

I think it should solve this case: https://github.com/conan-io/conan/pull/16405#issuecomment-2151120296

Clean installation for 2.4.0, then downgrade to 2.3.2 it should work



Patch:
diff --git a/conans/__init__.py b/conans/__init__.py
index df40eb197..a6137010a 100644
--- a/conans/__init__.py
+++ b/conans/__init__.py
@@ -2,4 +2,4 @@ CHECKSUM_DEPLOY = "checksum_deploy"  # Only when v2
 REVISIONS = "revisions"  # Only when enabled in config, not by default look at server_launcher.py
 OAUTH_TOKEN = "oauth_token"
 
-__version__ = '2.4.0'
+__version__ = '2.4.1'
diff --git a/conans/client/migrations.py b/conans/client/migrations.py
index 6c56d7035..74267a5b3 100644
--- a/conans/client/migrations.py
+++ b/conans/client/migrations.py
@@ -56,7 +56,9 @@ class ClientMigrator(Migrator):
         if old_version and old_version < "2.0.14-":
             _migrate_pkg_db_lru(self.cache_folder, old_version)
 
-        if old_version and old_version < "2.4":
+        # let the back migration files be stored
+        # if there was not a previous install (old_version==None)
+        if old_version is None or old_version < "2.4":
             _migrate_default_compatibility(self.cache_folder)
 
 


Test patch:
diff --git a/test/integration/test_migrations.py b/test/integration/test_migrations.py
index 73fa759e0..61b9761db 100644
--- a/test/integration/test_migrations.py
+++ b/test/integration/test_migrations.py
@@ -1,6 +1,7 @@
 import os
 import sqlite3
 import textwrap
+from unittest.mock import patch
 
 import pytest
 
@@ -8,6 +9,8 @@ from conan import conan_version
 from conan.test.assets.genconanfile import GenConanfile
 from conan.test.utils.test_files import temp_folder
 from conan.test.utils.tools import TestClient
+from conans.client.migrations import ClientMigrator
+from conans.model.version import Version
 from conans.util.files import save, load
 
 
@@ -131,3 +134,24 @@ def test_back_migrations():
         assert not os.path.exists(os.path.join(t.cache_folder, f"file{number}.txt"))
         migration_file = os.path.join(t.cache_folder, "migrations", f"2.100.0_{number}-migrate.py")
         assert not os.path.exists(migration_file)
+
+
+def test_back_default_compatibility_migration():
+    t = TestClient()
+
+    version_txt_file_path = os.path.join(t.cache_folder, "version.txt")
+
+    # emulate a clean install, no previous version
+    os.remove(version_txt_file_path)
+
+    t.run("-v")  # Fire the backward migration
+    migration_file = os.path.join(t.cache_folder, "migrations", "2.4_1-migrate.py")
+    assert os.path.exists(migration_file)
+
+    # downgrade from a clean latest conan_version to 2.3.2
+    # simulate that we are in 2.3.2 and the old one is latest conan_version
+    migrator = ClientMigrator(t.cache_folder, Version("2.3.2"))
+
+    with patch('conan.api.conan_api.ClientMigrator', new=lambda *args, **kwargs: migrator):
+        t.run("-v")  # Fire the backward migration
+        assert f"WARN: Downgrading cache from Conan {conan_version} to 2.3.2" in t.out