Skip to content

Commit

Permalink
fix: get or create db with an existing invalid URL (apache#23737)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar authored and sebastianliebscher committed Apr 28, 2023
1 parent 85ffb5d commit e1b9bbf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from sqlalchemy.engine import Connection, Dialect, Engine
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.engine.url import URL
from sqlalchemy.exc import ArgumentError, NoSuchModuleError
from sqlalchemy.exc import NoSuchModuleError
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
from sqlalchemy.pool import NullPool
Expand All @@ -56,6 +56,7 @@

from superset import app, db_engine_specs
from superset.constants import LRU_CACHE_MAX_SIZE, PASSWORD_MASK
from superset.databases.commands.exceptions import DatabaseInvalidError
from superset.databases.utils import make_url_safe
from superset.db_engine_specs.base import MetricType, TimeGrain
from superset.extensions import (
Expand Down Expand Up @@ -888,7 +889,7 @@ def get_schema_access_for_file_upload( # pylint: disable=invalid-name
def sqlalchemy_uri_decrypted(self) -> str:
try:
conn = make_url_safe(self.sqlalchemy_uri)
except (ArgumentError, ValueError):
except DatabaseInvalidError:
# if the URI is invalid, ignore and return a placeholder url
# (so users see 500 less often)
return "dialect://invalid_uri"
Expand Down
7 changes: 7 additions & 0 deletions tests/integration_tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,13 @@ def test_get_or_create_db_invalid_uri(self):
with self.assertRaises(DatabaseInvalidError):
get_or_create_db("test_db", "yoursql:superset.db/()")

def test_get_or_create_db_existing_invalid_uri(self):
database = get_or_create_db("test_db", "sqlite:///superset.db")
database.sqlalchemy_uri = "None"
db.session.commit()
database = get_or_create_db("test_db", "sqlite:///superset.db")
assert database.sqlalchemy_uri == "sqlite:///superset.db"

def test_get_iterable(self):
self.assertListEqual(get_iterable(123), [123])
self.assertListEqual(get_iterable([123]), [123])
Expand Down

0 comments on commit e1b9bbf

Please sign in to comment.