Skip to content

Commit

Permalink
fix: DB exported with incorrect type (apache#16037)
Browse files Browse the repository at this point in the history
* fix: DB exported with incorrect type

* Fix export as well
  • Loading branch information
betodealmeida authored and cccs-RyanS committed Dec 17, 2021
1 parent 0f0d269 commit 07a031e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
24 changes: 19 additions & 5 deletions superset/databases/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import json
import logging
from typing import Iterator, Tuple
from typing import Any, Dict, Iterator, Tuple

import yaml
from werkzeug.utils import secure_filename
Expand All @@ -32,6 +32,23 @@
logger = logging.getLogger(__name__)


def parse_extra(extra_payload: str) -> Dict[str, Any]:
try:
extra = json.loads(extra_payload)
except json.decoder.JSONDecodeError:
logger.info("Unable to decode `extra` field: %s", extra_payload)
return {}

# Fix for DBs saved with an invalid ``schemas_allowed_for_csv_upload``
schemas_allowed_for_csv_upload = extra.get("schemas_allowed_for_csv_upload")
if isinstance(schemas_allowed_for_csv_upload, str):
extra["schemas_allowed_for_csv_upload"] = json.loads(
schemas_allowed_for_csv_upload
)

return extra


class ExportDatabasesCommand(ExportModelsCommand):

dao = DatabaseDAO
Expand All @@ -51,10 +68,7 @@ def _export(model: Database) -> Iterator[Tuple[str, str]]:
# TODO (betodealmeida): move this logic to export_to_dict once this
# becomes the default export endpoint
if payload.get("extra"):
try:
payload["extra"] = json.loads(payload["extra"])
except json.decoder.JSONDecodeError:
logger.info("Unable to decode `extra` field: %s", payload["extra"])
payload["extra"] = parse_extra(payload["extra"])

payload["version"] = EXPORT_VERSION

Expand Down
19 changes: 19 additions & 0 deletions superset/databases/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,25 @@ class DatabaseFunctionNamesResponse(Schema):


class ImportV1DatabaseExtraSchema(Schema):
# pylint: disable=no-self-use, unused-argument
@pre_load
def fix_schemas_allowed_for_csv_upload(
self, data: Dict[str, Any], **kwargs: Any
) -> Dict[str, Any]:
"""
Fix ``schemas_allowed_for_csv_upload`` being a string.
Due to a bug in the database modal, some databases might have been
saved and exported with a string for ``schemas_allowed_for_csv_upload``.
"""
schemas_allowed_for_csv_upload = data.get("schemas_allowed_for_csv_upload")
if isinstance(schemas_allowed_for_csv_upload, str):
data["schemas_allowed_for_csv_upload"] = json.loads(
schemas_allowed_for_csv_upload
)

return data

metadata_params = fields.Dict(keys=fields.Str(), values=fields.Raw())
engine_params = fields.Dict(keys=fields.Str(), values=fields.Raw())
metadata_cache_timeout = fields.Dict(keys=fields.Str(), values=fields.Integer())
Expand Down

0 comments on commit 07a031e

Please sign in to comment.