This is an automated email from the ASF dual-hosted git repository.

michaelsmolina pushed a commit to branch 5.0
in repository https://gitbox.apache.org/repos/asf/superset.git

commit e67089b5ce2e133e7bc4d239d74816047076cf18
Author: Antonio Rivero <[email protected]>
AuthorDate: Wed Feb 5 23:00:22 2025 +0100

    fix(migrations): Handle no params in time comparison migration (#32155)
    
    (cherry picked from commit 6ed9dae2f7c32602b152f476a89f82afe72eebc2)
---
 ...02_f84fde59123a_update_charts_with_old_time_comparison.py |  8 ++++++++
 ...fde59123a_update_charts_with_old_time_comparison__test.py | 12 ++++++++++++
 2 files changed, 20 insertions(+)

diff --git 
a/superset/migrations/versions/2024-05-10_18-02_f84fde59123a_update_charts_with_old_time_comparison.py
 
b/superset/migrations/versions/2024-05-10_18-02_f84fde59123a_update_charts_with_old_time_comparison.py
index 57acaee9ef..b6f975dd02 100644
--- 
a/superset/migrations/versions/2024-05-10_18-02_f84fde59123a_update_charts_with_old_time_comparison.py
+++ 
b/superset/migrations/versions/2024-05-10_18-02_f84fde59123a_update_charts_with_old_time_comparison.py
@@ -63,6 +63,8 @@ time_map = {
 
 
 def upgrade_comparison_params(slice_params: dict[str, Any]) -> dict[str, Any]:
+    if not slice_params or not isinstance(slice_params, dict):
+        return {}
     params = deepcopy(slice_params)
 
     # Update time_comparison to time_compare
@@ -103,6 +105,8 @@ def upgrade():
         )
     ):
         try:
+            if not slc.params:  # Noop if there's no params on the slice
+                continue
             params = json.loads(slc.params)
             updated_slice_params = upgrade_comparison_params(params)
             slc.params = json.dumps(updated_slice_params)
@@ -119,6 +123,8 @@ def upgrade():
 
 
 def downgrade_comparison_params(slice_params: dict[str, Any]) -> dict[str, 
Any]:
+    if not slice_params or not isinstance(slice_params, dict):
+        return {}
     params = deepcopy(slice_params)
     params["enable_time_comparison"] = False
 
@@ -199,6 +205,8 @@ def downgrade():
         )
     ):
         try:
+            if not slc.params:  # Noop if there's no params on the slice
+                continue
             params = json.loads(slc.params)
             updated_slice_params = downgrade_comparison_params(params)
             slc.params = json.dumps(updated_slice_params)
diff --git 
a/tests/integration_tests/migrations/f84fde59123a_update_charts_with_old_time_comparison__test.py
 
b/tests/integration_tests/migrations/f84fde59123a_update_charts_with_old_time_comparison__test.py
index cec6a636c5..a9f343b950 100644
--- 
a/tests/integration_tests/migrations/f84fde59123a_update_charts_with_old_time_comparison__test.py
+++ 
b/tests/integration_tests/migrations/f84fde59123a_update_charts_with_old_time_comparison__test.py
@@ -229,3 +229,15 @@ def test_downgrade_chart_params_other_than_custom_false():
     original_params = deepcopy(params_v2_other_than_custom_false)
     downgraded_params = downgrade_comparison_params(original_params)
     assert downgraded_params == params_v1_other_than_custom_false
+
+
+def test_upgrade_chart_params_empty():
+    """
+    Ensure that the migration does not fail when params is None or empty.
+    """
+    assert upgrade_comparison_params(None) == {}
+    assert upgrade_comparison_params({}) == {}
+    assert upgrade_comparison_params("") == {}
+    assert downgrade_comparison_params(None) == {}
+    assert downgrade_comparison_params({}) == {}
+    assert downgrade_comparison_params("") == {}

Reply via email to