fix: DB exported with incorrect type (#16037)

* fix: DB exported with incorrect type

* Fix export as well
This commit is contained in:
Beto Dealmeida 2021-08-03 08:35:48 -07:00 committed by GitHub
parent 4cb79e5017
commit 7b15b7690d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 5 deletions

View File

@ -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
@ -32,6 +32,23 @@ from superset.utils.dict_import_export import EXPORT_VERSION
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
@ -51,10 +68,7 @@ class ExportDatabasesCommand(ExportModelsCommand):
# 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

View File

@ -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())