Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow chart import to update the dataset an existing chart points to #24821

Conversation

jfrag1
Copy link
Member

@jfrag1 jfrag1 commented Jul 27, 2023

SUMMARY

Currently, attempting to overwrite a chart via import fails when the chart in the import file points to a dataset other than the one the chart currently points at.

This is because of the following filter which gets added to the query that checks if the chart being imported already exists:

filters.extend([getattr(cls, k) == dict_rep.get(k) for k in parent_refs.keys()])

which for charts evalulates to

filters.extend([Slice.datasource_id == dict_rep.get("datasource_id")])

When the chart's dataset has been changed, the query fails to find the chart even if a chart with the same UUID exists. It then attempts to create the chart since it thinks it doesn't exist, which obviously fails the UUID uniqueness constraint.

This PR allows for charts to be "re-parented" to another dataset on import by removing this filter for chart imports, which can be useful when importing and exporting assets between environments after making changes, or for managing assets as code.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

  1. Have a database connection with a dataset
  2. Create a virtual dataset identical to the dataset (SELECT * FROM <physical_dataset>)
  3. Create a chart for each dataset
  4. Do a bulk export with both of these charts
  5. Extract the export file
  6. Navigate to the charts folder and swap the dataset_uuid values for the charts
  7. Compress the file
  8. Import it back into the Superset instance
  9. See the charts have swapped datasets

ADDITIONAL INFORMATION

@codecov
Copy link

codecov bot commented Jul 27, 2023

Codecov Report

Merging #24821 (3ef4ddf) into master (ba508a7) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 3ef4ddf differs from pull request most recent head 0d14ddf. Consider uploading reports for the commit 0d14ddf to get more accurate results

@@           Coverage Diff           @@
##           master   #24821   +/-   ##
=======================================
  Coverage   68.98%   68.98%           
=======================================
  Files        1903     1903           
  Lines       74007    74008    +1     
  Branches     8193     8193           
=======================================
+ Hits        51052    51053    +1     
  Misses      20834    20834           
  Partials     2121     2121           
Flag Coverage Δ
hive 54.16% <20.00%> (-0.01%) ⬇️
mysql 79.19% <100.00%> (+<0.01%) ⬆️
postgres 79.29% <100.00%> (+<0.01%) ⬆️
presto 54.06% <20.00%> (-0.01%) ⬇️
python 83.35% <100.00%> (+<0.01%) ⬆️
sqlite 77.86% <100.00%> (+<0.01%) ⬆️
unit 54.95% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
superset/charts/commands/importers/v1/utils.py 91.66% <100.00%> (ø)
superset/models/helpers.py 70.78% <100.00%> (+0.03%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Vitor-Avila
Copy link
Contributor

This is awesome! Thanks!!!

@michael-s-molina
Copy link
Member

@jfrag1 I saw yesterday that someone publish a SIP about this topic. Should we wait for the SIP before making this change? In the SIP, they are proposing to require explicit confirmation from the user before overriding associated assets. Is this PR going to conflict with the SIP?

@rusackas
Copy link
Member

The SIP not only proposes the UI to allow choice in the cascade of overrides, but also having a checkbox or similar introduces a place to check for or add ownership/RBAC constraints. I'll add this comment (and others) to the sip as well.

@jfrag1
Copy link
Member Author

jfrag1 commented Jul 27, 2023

@jfrag1 I saw yesterday that someone publish a SIP about this topic. Should we wait for the SIP before making this change? In the SIP, they are proposing to require explicit confirmation from the user before overriding associated assets. Is this PR going to conflict with the SIP?

I don't believe this PR is in conflict with that SIP, or that this functionality is relevant to that SIP. This isn't doing any cascading updates to child objects, it simply allows pointing a chart at a different dataset than it currently does. Also note that there is already an explicit confirmation that has to be performed when overwriting an asset via import

@Vitor-Avila
Copy link
Contributor

I think this would only conflict with the SIP in case the chart dataset update also happens when importing a dashboard. Currently the overwrite check is only "applied" to the actual asset being imported -- for example, if you import a dashboard with charts, the dashboard will be overwritten but charts wouldn't. So if this change is only applied when importing a chart directly (or via the /api/v1/assets/import/ endpoint) I don't think it conflicts.

@eschutho eschutho added the 2.1.2 label Jul 27, 2023
Copy link
Member

@betodealmeida betodealmeida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@michael-s-molina this is not directly related to the SIP you mentioned; it's not doing any cascade imports but just point the chart to a different dataset (which could be new or existing).

@michael-s-molina
Copy link
Member

Got it. Thanks @jfrag1 @Vitor-Avila and @betodealmeida for the additional context.

@lilykuang lilykuang merged commit 77889b2 into apache:master Jul 28, 2023
32 checks passed
@lilykuang lilykuang deleted the jack/allow-updating-chart-dataset-via-import branch July 28, 2023 23:08
@michael-s-molina michael-s-molina added the v3.0 Label added by the release manager to track PRs to be included in the 3.0 branch label Jul 31, 2023
@SharonCastel
Copy link

@jfrag1 finally someone who understands the issue and know how to fix it! You are the best! Can't wait for it...

michael-s-molina pushed a commit that referenced this pull request Aug 4, 2023
@eschutho eschutho added the v2.1 label Oct 20, 2023
@mistercrunch mistercrunch added 🍒 2.1.2 🍒 2.1.3 🍒 3.0.0 🍒 3.0.1 🍒 3.0.2 🍒 3.0.3 🍒 3.0.4 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 3.1.0 labels Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.1.2 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/S v2.1 v3.0 Label added by the release manager to track PRs to be included in the 3.0 branch 🍒 2.1.2 🍒 2.1.3 🍒 3.0.0 🍒 3.0.1 🍒 3.0.2 🍒 3.0.3 🍒 3.0.4 🚢 3.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants