From 80163663e64ca56f18b9c4d1625debef49fa9be6 Mon Sep 17 00:00:00 2001
From: "Yngve S. Kristiansen" <ynk@equinor.com>
Date: Fri, 25 Oct 2024 11:37:01 +0200
Subject: [PATCH] Backport gendata migration fix

---
 src/ert/storage/migration/to7.py              |  6 ++----
 .../test_that_storage_matches/gen_data        | 21 +++++++++++++++++++
 .../storage/test_storage_migration.py         | 12 +++++++++++
 3 files changed, 35 insertions(+), 4 deletions(-)
 create mode 100644 tests/unit_tests/storage/snapshots/test_storage_migration/test_that_storage_matches/gen_data

diff --git a/src/ert/storage/migration/to7.py b/src/ert/storage/migration/to7.py
index 122648c1b04..1263a76e878 100644
--- a/src/ert/storage/migration/to7.py
+++ b/src/ert/storage/migration/to7.py
@@ -96,9 +96,7 @@ def _migrate_response_datasets(path: Path) -> None:
             responses_obj is not None
         ), f"Failed to load responses.json @ {responses_file}"
 
-        gendata_keys = {
-            k for k, v in responses_obj.items() if v["_ert_kind"] == "GenDataConfig"
-        }
+        gendata_keys = responses_obj.get("gen_data", {}).get("keys", [])
 
         for ens in ensembles:
             with open(ens / "index.json", encoding="utf-8") as f:
@@ -134,5 +132,5 @@ def _migrate_response_datasets(path: Path) -> None:
 
 
 def migrate(path: Path) -> None:
-    _migrate_response_datasets(path)
     _migrate_response_configs(path)
+    _migrate_response_datasets(path)
diff --git a/tests/unit_tests/storage/snapshots/test_storage_migration/test_that_storage_matches/gen_data b/tests/unit_tests/storage/snapshots/test_storage_migration/test_that_storage_matches/gen_data
new file mode 100644
index 00000000000..420484ab0de
--- /dev/null
+++ b/tests/unit_tests/storage/snapshots/test_storage_migration/test_that_storage_matches/gen_data
@@ -0,0 +1,21 @@
+realization,name,index,report_step,values
+0,GEN,0,1,0.0
+0,GEN,1,1,0.0
+1,GEN,0,1,0.0
+1,GEN,1,1,0.0
+2,GEN,0,1,0.0
+2,GEN,1,1,0.0
+3,GEN,0,1,0.0
+3,GEN,1,1,0.0
+4,GEN,0,1,0.0
+4,GEN,1,1,0.0
+5,GEN,0,1,0.1
+5,GEN,1,1,0.1
+6,GEN,0,1,0.1
+6,GEN,1,1,0.1
+7,GEN,0,1,0.1
+7,GEN,1,1,0.1
+8,GEN,0,1,0.1
+8,GEN,1,1,0.1
+9,GEN,0,1,0.1
+9,GEN,1,1,0.1
diff --git a/tests/unit_tests/storage/test_storage_migration.py b/tests/unit_tests/storage/test_storage_migration.py
index 456164e6f7a..0d3bbc5ad7f 100644
--- a/tests/unit_tests/storage/test_storage_migration.py
+++ b/tests/unit_tests/storage/test_storage_migration.py
@@ -176,6 +176,18 @@ def test_that_storage_matches(
             "observations",
         )
 
+        gen_data = ensemble.load_responses(
+            "gen_data", tuple(range(ensemble.ensemble_size))
+        )
+        snapshot.assert_match(
+            gen_data.to_dataframe(
+                dim_order=["realization", "name", "index", "report_step"]
+            )
+            .transform(np.sort)
+            .to_csv(),
+            "gen_data",
+        )
+
 
 @pytest.mark.integration_test
 @pytest.mark.usefixtures("copy_shared")