Skip to content

Commit

Permalink
fix: ensure engine is outside parameters (apache#14787)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored May 24, 2021
1 parent 6c59bf0 commit 19c4cf0
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions superset/databases/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class DatabaseParametersSchemaMixin:
When using this mixin make sure that `sqlalchemy_uri` is not required.
"""

engine = fields.String(allow_none=True, description="SQLAlchemy engine to use")
parameters = fields.Dict(
keys=fields.String(),
values=fields.Raw(),
Expand All @@ -245,15 +246,14 @@ def build_sqlalchemy_uri(
parameters (eg, username, password, host, etc.), instead of requiring
the constructed SQLAlchemy URI to be passed.
"""
parameters = data.pop("parameters", None)
serialized_encrypted_extra = data.get("encrypted_extra", "{}")
try:
encrypted_extra = json.loads(serialized_encrypted_extra)
except json.decoder.JSONDecodeError:
encrypted_extra = {}
parameters = data.pop("parameters", {})

# TODO (betodealmeida): remove second expression after making sure
# frontend is not passing engine inside parameters
engine = data.pop("engine", None) or parameters.pop("engine", None)

if parameters:
if "engine" not in parameters:
if not engine:
raise ValidationError(
[
_(
Expand All @@ -262,8 +262,6 @@ def build_sqlalchemy_uri(
)
]
)
engine = parameters["engine"]

engine_specs = get_engine_specs()
if engine not in engine_specs:
raise ValidationError(
Expand All @@ -272,6 +270,12 @@ def build_sqlalchemy_uri(
engine_spec = engine_specs[engine]

if hasattr(engine_spec, "build_sqlalchemy_uri"):
serialized_encrypted_extra = data.get("encrypted_extra", "{}")
try:
encrypted_extra = json.loads(serialized_encrypted_extra)
except json.decoder.JSONDecodeError:
encrypted_extra = {}

data[
"sqlalchemy_uri"
] = engine_spec.build_sqlalchemy_uri( # type: ignore
Expand Down

0 comments on commit 19c4cf0

Please sign in to comment.