forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create backend routes and API for importing saved queries (apac…
…he#13893) * initial commit * revisions * started tests * added unit tests * revisions * tests passing * fixed api test * Update superset/queries/saved_queries/commands/importers/v1/utils.py Co-authored-by: Hugh A. Miles II <hughmil3s@gmail.com> * Revert "Update superset/queries/saved_queries/commands/importers/v1/utils.py" This reverts commit 18580aa. Co-authored-by: Hugh A. Miles II <hughmil3s@gmail.com>
- Loading branch information
1 parent
12eda74
commit 4fb4b93
Showing
10 changed files
with
468 additions
and
5 deletions.
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
67 changes: 67 additions & 0 deletions
67
superset/queries/saved_queries/commands/importers/dispatcher.py
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,67 @@ | ||
# 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. | ||
|
||
import logging | ||
from typing import Any, Dict | ||
|
||
from marshmallow.exceptions import ValidationError | ||
|
||
from superset.commands.base import BaseCommand | ||
from superset.commands.exceptions import CommandInvalidError | ||
from superset.commands.importers.exceptions import IncorrectVersionError | ||
from superset.queries.saved_queries.commands.importers import v1 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
command_versions = [ | ||
v1.ImportSavedQueriesCommand, | ||
] | ||
|
||
|
||
class ImportSavedQueriesCommand(BaseCommand): | ||
""" | ||
Import Saved Queries | ||
This command dispatches the import to different versions of the command | ||
until it finds one that matches. | ||
""" | ||
|
||
# pylint: disable=unused-argument | ||
def __init__(self, contents: Dict[str, str], *args: Any, **kwargs: Any): | ||
self.contents = contents | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def run(self) -> None: | ||
# iterate over all commands until we find a version that can | ||
# handle the contents | ||
for version in command_versions: | ||
command = version(self.contents, *self.args, **self.kwargs) | ||
try: | ||
command.run() | ||
return | ||
except IncorrectVersionError: | ||
logger.debug("File not handled by command, skipping") | ||
except (CommandInvalidError, ValidationError) as exc: | ||
# found right version, but file is invalid | ||
logger.exception("Error running import command") | ||
raise exc | ||
|
||
raise CommandInvalidError("Could not find a valid command to import file") | ||
|
||
def validate(self) -> None: | ||
pass |
71 changes: 71 additions & 0 deletions
71
superset/queries/saved_queries/commands/importers/v1/__init__.py
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,71 @@ | ||
# 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. | ||
|
||
from typing import Any, Dict, Set | ||
|
||
from marshmallow import Schema | ||
from sqlalchemy.orm import Session | ||
|
||
from superset.commands.importers.v1 import ImportModelsCommand | ||
from superset.connectors.sqla.models import SqlaTable | ||
from superset.databases.commands.importers.v1.utils import import_database | ||
from superset.databases.schemas import ImportV1DatabaseSchema | ||
from superset.queries.saved_queries.commands.exceptions import SavedQueryImportError | ||
from superset.queries.saved_queries.commands.importers.v1.utils import ( | ||
import_saved_query, | ||
) | ||
from superset.queries.saved_queries.dao import SavedQueryDAO | ||
from superset.queries.saved_queries.schemas import ImportV1SavedQuerySchema | ||
|
||
|
||
class ImportSavedQueriesCommand(ImportModelsCommand): | ||
"""Import Saved Queries""" | ||
|
||
dao = SavedQueryDAO | ||
model_name = "saved_queries" | ||
prefix = "queries/" | ||
schemas: Dict[str, Schema] = { | ||
"databases/": ImportV1DatabaseSchema(), | ||
"queries/": ImportV1SavedQuerySchema(), | ||
} | ||
import_error = SavedQueryImportError | ||
|
||
@staticmethod | ||
def _import( | ||
session: Session, configs: Dict[str, Any], overwrite: bool = False | ||
) -> None: | ||
# discover databases associated with saved queries | ||
database_uuids: Set[str] = set() | ||
for file_name, config in configs.items(): | ||
if file_name.startswith("queries/"): | ||
database_uuids.add(config["database_uuid"]) | ||
|
||
# import related databases | ||
database_ids: Dict[str, int] = {} | ||
for file_name, config in configs.items(): | ||
if file_name.startswith("databases/") and config["uuid"] in database_uuids: | ||
database = import_database(session, config, overwrite=False) | ||
database_ids[str(database.uuid)] = database.id | ||
|
||
# import saved queries with the correct parent ref | ||
for file_name, config in configs.items(): | ||
if ( | ||
file_name.startswith("queries/") | ||
and config["database_uuid"] in database_ids | ||
): | ||
config["db_id"] = database_ids[config["database_uuid"]] | ||
import_saved_query(session, config, overwrite=overwrite) |
38 changes: 38 additions & 0 deletions
38
superset/queries/saved_queries/commands/importers/v1/utils.py
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,38 @@ | ||
# 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. | ||
|
||
from typing import Any, Dict | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
from superset.models.sql_lab import SavedQuery | ||
|
||
|
||
def import_saved_query( | ||
session: Session, config: Dict[str, Any], overwrite: bool = False | ||
) -> SavedQuery: | ||
existing = session.query(SavedQuery).filter_by(uuid=config["uuid"]).first() | ||
if existing: | ||
if not overwrite: | ||
return existing | ||
config["id"] = existing.id | ||
|
||
saved_query = SavedQuery.import_from_dict(session, config, recursive=False) | ||
if saved_query.id is None: | ||
session.flush() | ||
|
||
return saved_query |
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
Oops, something went wrong.