Skip to content

fix(importexport): honor overwrite flag on /api/v1/assets/import#39502

Merged
rusackas merged 3 commits into
apache:masterfrom
mistercrunch:fix-assets-import-overwrite-flag
May 11, 2026
Merged

fix(importexport): honor overwrite flag on /api/v1/assets/import#39502
rusackas merged 3 commits into
apache:masterfrom
mistercrunch:fix-assets-import-overwrite-flag

Conversation

@mistercrunch
Copy link
Copy Markdown
Member

SUMMARY

The /api/v1/assets/import endpoint previously ignored the overwrite parameter and always overwrote existing assets. This PR threads an overwrite flag (defaulting to true for backwards compatibility) through ImportAssetsCommand to each of import_database, import_saved_query, import_dataset, import_chart, and import_dashboard — all of which were hard-coded to overwrite=True.

When overwrite=false and any asset in the bundle already exists, the import now fails with a clear validation error listing each conflicting asset (e.g. "Slice already exists and \overwrite=true` was not passed"), matching the behavior of the per-resource import endpoints (see ImportModelsCommand._prevent_overwrite_existing_model`).

Because the default remains True, existing clients that omit the flag will see no behavior change.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A — backend-only change.

TESTING INSTRUCTIONS

  1. Export assets from an instance: GET /api/v1/assets/export/.
  2. Re-import them with the default (or overwrite=true) — succeeds and overwrites as before.
  3. Re-import them with overwrite=false while the assets exist — fails with a 422 and clear per-asset error messages.
  4. Import a fresh bundle (no UUID conflicts) with overwrite=false — succeeds.

Automated tests added under:

  • tests/unit_tests/commands/importers/v1/assets_test.py — command-level behavior (default, flag threading, validation).
  • tests/unit_tests/importexport/api_test.py — API plumbing for the new form field.

Run with:

pytest tests/unit_tests/commands/importers/v1/ tests/unit_tests/importexport/

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@github-actions github-actions Bot added the api Related to the REST API label Apr 21, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

❌ Patch coverage is 35.89744% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.87%. Comparing base (4aa4415) to head (38d6293).
⚠️ Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
superset/commands/importers/v1/assets.py 35.13% 23 Missing and 1 partial ⚠️
superset/importexport/api.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #39502      +/-   ##
==========================================
- Coverage   63.88%   63.87%   -0.02%     
==========================================
  Files        2583     2583              
  Lines      136602   136657      +55     
  Branches    31501    31514      +13     
==========================================
+ Hits        87274    87284      +10     
- Misses      47812    47856      +44     
- Partials     1516     1517       +1     
Flag Coverage Δ
hive 39.36% <15.38%> (-0.03%) ⬇️
mysql 59.02% <35.89%> (-0.04%) ⬇️
postgres 59.10% <35.89%> (-0.04%) ⬇️
presto 41.05% <15.38%> (-0.03%) ⬇️
python 60.54% <35.89%> (-0.04%) ⬇️
sqlite 58.74% <35.89%> (-0.04%) ⬇️
unit 100.00% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mistercrunch mistercrunch marked this pull request as ready for review April 21, 2026 04:59
@dosubot dosubot Bot added the change:backend Requires changing the backend label Apr 21, 2026
Copy link
Copy Markdown
Contributor

@bito-code-review bito-code-review Bot left a comment

Choose a reason for hiding this comment

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

Code Review Agent Run #29c7ea

Actionable Suggestions - 1
  • superset/commands/importers/v1/assets.py - 1
Review Details
  • Files reviewed - 4 · Commit Range: 4c8ce19..74c5ade
    • superset/commands/importers/v1/assets.py
    • superset/importexport/api.py
    • tests/unit_tests/commands/importers/v1/assets_test.py
    • tests/unit_tests/importexport/api_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Comment on lines +237 to +239
existing_uuids = {
str(uuid) for (uuid,) in db.session.query(model_cls.uuid).all()
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Inefficient UUID query

The _prevent_overwrite_existing_assets method queries all UUIDs for each model, which can be inefficient with many existing assets. Optimize by filtering the query to only check UUIDs present in the import bundle.

Code Review Run #29c7ea


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

mistercrunch and others added 2 commits May 6, 2026 19:55
The assets import endpoint previously ignored the ``overwrite`` parameter
and always overwrote existing assets. This threads an ``overwrite`` flag
(default ``True`` for backwards compatibility) through ``ImportAssetsCommand``
to ``import_database``, ``import_saved_query``, ``import_dataset``,
``import_chart`` and ``import_dashboard``.

When ``overwrite=false`` and any asset in the bundle already exists, the
import now fails with a clear validation error listing the conflicting
assets, matching the behavior of the per-resource import endpoints.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… parse_boolean_string

Address review feedback:

- Add ``"queries/": SavedQuery`` to ``_MODEL_BY_PREFIX`` so existing saved
  queries trigger a validation error when ``overwrite=false`` — previously
  ``import_saved_query`` would silently return the existing row, letting
  the endpoint appear to succeed despite the conflict.
- Use ``parse_boolean_string`` in the API instead of an ad-hoc
  ``.lower() == "true"`` check.
- Add tests for the saved-query prefix and for partial conflicts (some
  assets already exist, others are new).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mistercrunch mistercrunch force-pushed the fix-assets-import-overwrite-flag branch from 74c5ade to 6c089ce Compare May 6, 2026 19:56
Comment on lines +237 to +239
existing_uuids = {
str(uuid) for (uuid,) in db.session.query(model_cls.uuid).all()
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: This validation does full-table UUID scans for every asset type (Database, SqlaTable, Slice, Dashboard, SavedQuery) on every import, even if the bundle contains only a few files. On large instances this can cause major latency and memory pressure. Build a per-prefix set of incoming UUIDs and query only matching rows with IN (...) instead of loading all UUIDs from each table. [possible bug]

Severity Level: Major ⚠️
- ⚠️ /api/v1/assets/import overwrite=false always scans all asset tables.
- ⚠️ Asset import latency grows linearly with total stored assets.
- ⚠️ Additional queries add memory pressure on metadata database.
Steps of Reproduction ✅
1. Call the bulk import API by POSTing to `/api/v1/assets/import/` (implemented in
`superset/importexport/api.py:95-201`) with a valid ZIP bundle and form field
`overwrite=false`, so that `ImportExportRestApi.import_` constructs
`ImportAssetsCommand(..., overwrite=False)` (`importexport/api.py:195-237`) and calls
`command.run()`.

2. Inside `ImportAssetsCommand.run` (`superset/commands/importers/v1/assets.py:205-215`),
`self.validate()` is invoked. `validate()` loads the bundle configs via `load_configs`
(`assets.py:59-21`) into `self._configs` and then calls
`_prevent_overwrite_existing_assets(exceptions)` (`assets.py:22`).

3. `_prevent_overwrite_existing_assets` (`assets.py:17-35`) first checks `if
self.overwrite: return` and, since `overwrite=False`, iterates over `_MODEL_BY_PREFIX`
(`assets.py:7-15`), which maps `"databases/"``Database`, `"datasets/"``SqlaTable`,
`"charts/"``Slice`, `"dashboards/"``Dashboard`, and `"queries/"``SavedQuery`.

4. For each prefix/model pair, it executes `db.session.query(model_cls.uuid).all()` and
builds `existing_uuids = {str(uuid) for (uuid,) in ...}` (`assets.py:27-30`), pulling
every UUID from each of the five tables into Python sets, regardless of how many files of
that type are actually present in `self._configs`. These full-table UUID scans run on
every import with `overwrite=false`, giving O(total stored assets) database and memory
work per import, rather than O(assets in the bundle).

Fix in Cursor | Fix in VSCode Claude

(Use Cmd/Ctrl + Click for best experience)

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/commands/importers/v1/assets.py
**Line:** 237:239
**Comment:**
	*Possible Bug: This validation does full-table UUID scans for every asset type (`Database`, `SqlaTable`, `Slice`, `Dashboard`, `SavedQuery`) on every import, even if the bundle contains only a few files. On large instances this can cause major latency and memory pressure. Build a per-prefix set of incoming UUIDs and query only matching rows with `IN (...)` instead of loading all UUIDs from each table.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch — fixed in 38d6293. The validation now groups the bundle's (file_name, uuid) pairs by prefix in one pass over self._configs, and only issues one WHERE uuid IN (...) query per prefix that has entries. Prefixes with nothing in the bundle skip the database entirely, so cost scales with bundle size instead of the size of each asset table. Added test_prevent_overwrite_queries_only_bundle_uuids to lock in the behavior (asserts only the relevant model is queried for a single-prefix bundle).

@bito-code-review
Copy link
Copy Markdown
Contributor

The flagged issue is correct—the validation performs full-table UUID scans for all asset types on every import with overwrite=false, causing O(total stored assets) work instead of O(assets in bundle). To resolve, collect incoming UUIDs per prefix and query only matching rows with IN(...). I've implemented the concise fix below. No other comments found in this PR.

superset/commands/importers/v1/assets.py

def _prevent_overwrite_existing_assets(
        self, exceptions: list[ValidationError]
    ) -> None:
        """
        When ``overwrite`` is ``False``, raise a clear validation error for any
        asset in the bundle whose UUID already exists in the database.
        """
        if self.overwrite:
            return

        for prefix, model_cls in self._MODEL_BY_PREFIX.items():
            incoming_uuids = [
                config.get("uuid") for file_name, config in self._configs.items()
                if file_name.startswith(prefix) and config.get("uuid")
            ]
            if incoming_uuids:
                existing_uuids = {
                    str(uuid) for (uuid,) in db.session.query(model_cls.uuid)
                    .filter(model_cls.uuid.in_(incoming_uuids)).all()
                }
                for file_name, config in self._configs.items():
                    if (
                        file_name.startswith(prefix)
                        and config.get("uuid") in existing_uuids
                    ):
                        model_name = model_cls.__name__
                        exceptions.append(
                            ValidationError(
                                {
                                    file_name: (
                                        f"{model_name} already exists "
                                        "and `overwrite=true` was not passed"
                                    ),
                                }
                            )
                        )

@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review Bot commented May 7, 2026

Code Review Agent Run #3fc73c

Actionable Suggestions - 0
Review Details
  • Files reviewed - 4 · Commit Range: baad84b..6c089ce
    • superset/commands/importers/v1/assets.py
    • superset/importexport/api.py
    • tests/unit_tests/commands/importers/v1/assets_test.py
    • tests/unit_tests/importexport/api_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Before: ``_prevent_overwrite_existing_assets`` ran a full-table UUID
scan against every asset model (``Database``, ``SqlaTable``, ``Slice``,
``Dashboard``, ``SavedQuery``) on every import with ``overwrite=false``,
giving ``O(total stored assets)`` work per import regardless of how many
files the bundle actually contains.

Fix: collect the incoming UUIDs from ``self._configs`` per prefix, then
issue one ``WHERE uuid IN (...)`` query per prefix that has entries —
prefixes with no entries skip the database entirely. The cost now scales
with the bundle size rather than with the size of the asset tables.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented May 7, 2026

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 38d6293
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/69fccd2db829d7000826fccf
😎 Deploy Preview https://deploy-preview-39502--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review Bot commented May 8, 2026

Code Review Agent Run #4d77f5

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 6c089ce..38d6293
    • superset/commands/importers/v1/assets.py
    • tests/unit_tests/commands/importers/v1/assets_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Copy link
Copy Markdown
Member

@rusackas rusackas left a comment

Choose a reason for hiding this comment

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

LGTM!

@rusackas rusackas merged commit d90d3a2 into apache:master May 11, 2026
65 checks passed
qfcwell pushed a commit to qfcwell/superset that referenced this pull request May 12, 2026
…che#39502)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Related to the REST API change:backend Requires changing the backend size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants