From a19ab48521e40549c12e36c509ce45b6f49b854f Mon Sep 17 00:00:00 2001
From: Valentin Rigal <rigal@teklia.com>
Date: Wed, 22 May 2024 08:53:27 +0000
Subject: [PATCH] Support exports version 8 in the load_export command

---
 .../documents/management/commands/load_export.py   | 14 +++++++++-----
 .../documents/tests/commands/test_load_export.py   | 10 ++++++++--
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/arkindex/documents/management/commands/load_export.py b/arkindex/documents/management/commands/load_export.py
index bc86530c4c..aff006607e 100644
--- a/arkindex/documents/management/commands/load_export.py
+++ b/arkindex/documents/management/commands/load_export.py
@@ -38,7 +38,8 @@ from arkindex.process.models import (
 from arkindex.training.models import Dataset, DatasetElement, DatasetSet, Model
 from arkindex.users.models import Role, User
 
-EXPORT_VERSION = 9
+EXPORT_VERSION_MIN = 8
+EXPORT_VERSION_MAX = 9
 
 TABLE_NAMES = {
     "export_version",
@@ -519,13 +520,16 @@ class Command(BaseCommand):
 
         # Check database tables
         db_results = self.db.execute(SQL_TABLES_QUERY).fetchall()
-        if not set([table["name"] for table in db_results]) == TABLE_NAMES:
-            raise CommandError(f"The SQLite database {db_path} is not a correct Arkindex export")
+        # Database's tables must be a superset of TABLE_NAMES, so we keep compatibility when removing things
+        if (missing := TABLE_NAMES - set([table["name"] for table in db_results])):
+            raise CommandError(f"The SQLite database {db_path} is missing some expected tables: {sorted(missing)}")
 
         # Check export version
         db_results = self.db.execute(SQL_VERSION_QUERY).fetchall()
-        if len(db_results) != 1 or db_results[0]["version"] != EXPORT_VERSION:
-            raise CommandError(f"The SQLite database {db_path} does not have the correct export version")
+        if len(db_results) != 1 or not (
+            EXPORT_VERSION_MIN <= db_results[0]["version"] <= EXPORT_VERSION_MAX
+        ):
+            raise CommandError(f"The SQLite database {db_path} does not have a supported export version")
 
         # Retrieve corpus name
         date = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M")
diff --git a/arkindex/documents/tests/commands/test_load_export.py b/arkindex/documents/tests/commands/test_load_export.py
index 3bf8e45290..7344e5f720 100644
--- a/arkindex/documents/tests/commands/test_load_export.py
+++ b/arkindex/documents/tests/commands/test_load_export.py
@@ -100,7 +100,13 @@ class TestLoadExport(FixtureTestCase):
         _, temp_file = tempfile.mkstemp(suffix=".db")
         with self.assertRaises(CommandError) as context:
             call_command("load_export", temp_file, "--email", self.user.email)
-        self.assertEqual(str(context.exception), f"The SQLite database {temp_file} is not a correct Arkindex export")
+        self.assertEqual(str(context.exception), (
+            f"The SQLite database {temp_file} is missing some expected tables: "
+            "['classification', 'dataset', 'dataset_element', 'element', "
+            "'element_path', 'entity', 'entity_type', 'export_version', "
+            "'image', 'image_server', 'metadata', 'transcription', "
+            "'transcription_entity', 'worker_run', 'worker_version']"
+        ))
 
     def test_invalid_version(self):
         _, temp_file = tempfile.mkstemp(suffix=".db")
@@ -113,7 +119,7 @@ class TestLoadExport(FixtureTestCase):
 
         with self.assertRaises(CommandError) as context:
             call_command("load_export", temp_file, "--email", self.user.email, "--corpus-name", "My corpus")
-        self.assertEqual(str(context.exception), f"The SQLite database {temp_file} does not have the correct export version")
+        self.assertEqual(str(context.exception), f"The SQLite database {temp_file} does not have a supported export version")
 
     @patch("arkindex.documents.export.os.unlink")
     @patch("arkindex.project.aws.s3.Object")
-- 
GitLab