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
feat: add endpoint to export saved queries using new format #11447
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5c970c3
Add UUID to saved_query
betodealmeida 1e10d5e
Merge branch 'master' into SO-1130
betodealmeida eea5e1f
Merge branch 'master' into SO-1130
betodealmeida e864c9b
Merge branch 'master' into SO-1130
betodealmeida a8d261f
Reuse function from previous migration
betodealmeida 7c6f8ce
Point to new head
betodealmeida c9f8872
Merge branch 'master' into SO-1130
betodealmeida d2d2450
Merge branch 'master' into SO-1130
betodealmeida 96d6856
feat: add backend to export saved queries using new format
betodealmeida 88fd334
Merge branch 'master' into SO-1130
betodealmeida 7f8174a
Merge branch 'SO-1130' into SO-1095
betodealmeida 9f79795
Merge branch 'master' into SO-1095
betodealmeida 1e455bf
Merge branch 'master' into SO-1095
betodealmeida d8f6659
Merge branch 'master' into SO-1095
betodealmeida 0549ed2
Merge branch 'master' into SO-1095
betodealmeida File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# isort:skip_file | ||
|
||
import json | ||
import logging | ||
from typing import Iterator, List, Tuple | ||
|
||
import yaml | ||
|
||
from superset.commands.base import BaseCommand | ||
from superset.queries.saved_queries.commands.exceptions import SavedQueryNotFoundError | ||
from superset.queries.saved_queries.dao import SavedQueryDAO | ||
from superset.utils.dict_import_export import IMPORT_EXPORT_VERSION, sanitize | ||
from superset.models.sql_lab import SavedQuery | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ExportSavedQueriesCommand(BaseCommand): | ||
def __init__(self, query_ids: List[int]): | ||
self.query_ids = query_ids | ||
|
||
# this will be set when calling validate() | ||
self._models: List[SavedQuery] = [] | ||
|
||
@staticmethod | ||
def export_saved_query(query: SavedQuery) -> Iterator[Tuple[str, str]]: | ||
# build filename based on database, optional schema, and label | ||
database_slug = sanitize(query.database.database_name) | ||
schema_slug = sanitize(query.schema) | ||
query_slug = sanitize(query.label) or str(query.uuid) | ||
file_name = f"queries/{database_slug}/{schema_slug}/{query_slug}.yaml" | ||
|
||
payload = query.export_to_dict( | ||
recursive=False, | ||
include_parent_ref=False, | ||
include_defaults=True, | ||
export_uuids=True, | ||
) | ||
payload["version"] = IMPORT_EXPORT_VERSION | ||
payload["database_uuid"] = str(query.database.uuid) | ||
|
||
file_content = yaml.safe_dump(payload, sort_keys=False) | ||
yield file_name, file_content | ||
|
||
# include database as well | ||
file_name = f"databases/{database_slug}.yaml" | ||
|
||
payload = query.database.export_to_dict( | ||
recursive=False, | ||
include_parent_ref=False, | ||
include_defaults=True, | ||
export_uuids=True, | ||
) | ||
# TODO (betodealmeida): move this logic to export_to_dict once this | ||
# becomes the default export endpoint | ||
if "extra" in payload: | ||
try: | ||
payload["extra"] = json.loads(payload["extra"]) | ||
except json.decoder.JSONDecodeError: | ||
logger.info("Unable to decode `extra` field: %s", payload["extra"]) | ||
|
||
payload["version"] = IMPORT_EXPORT_VERSION | ||
|
||
file_content = yaml.safe_dump(payload, sort_keys=False) | ||
yield file_name, file_content | ||
|
||
def run(self) -> Iterator[Tuple[str, str]]: | ||
self.validate() | ||
|
||
for query in self._models: | ||
yield from self.export_saved_query(query) | ||
|
||
def validate(self) -> None: | ||
self._models = SavedQueryDAO.find_by_ids(self.query_ids) | ||
if len(self._models) != len(self.query_ids): | ||
raise SavedQueryNotFoundError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not related with this PR, sort of a note to self, this is asking to be DRY'ed