Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db): Adding DB_SQLA_URI_VALIDATOR #27847

Merged

Conversation

craig-rueda
Copy link
Member

@craig-rueda craig-rueda commented Apr 1, 2024

SUMMARY

Quick PR that adds a new callback hook, DB_SQLA_URI_VALIDATOR which can be optionally set in Superset configs. When set, it should point to a callable that is invoked in order to validate db_engine_spec's URIs.

Example:

def DB_SQLA_URI_VALIDATOR(sqlalchemy_uri: URL):
    if not <some condition>:
        raise Exception("URI invalid")

Copy link

codecov bot commented Apr 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.83%. Comparing base (ca47717) to head (8e5db72).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #27847      +/-   ##
==========================================
+ Coverage   67.41%   69.83%   +2.42%     
==========================================
  Files        1920     1920              
  Lines       75242    75245       +3     
  Branches     8423     8423              
==========================================
+ Hits        50724    52548    +1824     
+ Misses      22457    20636    -1821     
  Partials     2061     2061              
Flag Coverage Δ
hive 48.93% <66.66%> (?)
mysql 77.88% <66.66%> (-0.01%) ⬇️
postgres 77.99% <66.66%> (-0.01%) ⬇️
presto 53.63% <66.66%> (?)
python 83.15% <100.00%> (+5.01%) ⬆️
sqlite 77.43% <66.66%> (-0.01%) ⬇️
unit 56.78% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@mistercrunch mistercrunch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 thoughts:

  • about naming DB_SQLA_URI_VALIDATOR might be a slightly better name as it validates a sqlalchemy URI more than an "engine" uri
  • I thought we could have a more generic DB_VALIDATOR instead or in complement to this so that someone could look at other DatabaseConnection attributes too, as opposed to the uri only. Password complexity, extra attributes or anything else.

@craig-rueda
Copy link
Member Author

Aren't all of the connection attributes captured in the parsed url? Also, this is intended to be invoked pre-connect, so we likely wouldn't want to perform validations on a "connection".

@mistercrunch
Copy link
Member

I think the bigger exception is the way we do BigQuery for instance cramming many things into a blob, but unclear how common/uncommon it is. This probably a good starting point. Can add more hooks if/as needed.

@betodealmeida
Copy link
Member

betodealmeida commented Apr 2, 2024

Yeah, in general there are two parts:

engine = create_engine(uri, **params)

Both are needed for a full connection, since in some cases the params are too big to build a URI (like BigQuery credentials).

# if not <some condition>:
# raise Exception("URI invalid")
#
DB_ENGINE_URI_VALIDATOR = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: DB_ENGINE_URI_VALIDATOR: Callable[[URL], None] | None = None

@craig-rueda craig-rueda changed the title feat(db): Adding DB_ENGINE_URI_VALIDATOR feat(db): Adding DB_SQLA_URI_VALIDATOR Apr 2, 2024
@craig-rueda craig-rueda merged commit 8bdf457 into apache:master Apr 2, 2024
33 checks passed
@craig-rueda craig-rueda deleted the craig/adding-db-engine-validator branch April 2, 2024 16:00
@john-bodley
Copy link
Member

@craig-rueda out of interest why is this surfacing as an issue now? Did we not gracefully handle SQLAlchemy URI errors in the past?

jzhao62 pushed a commit to jzhao62/superset that referenced this pull request Apr 4, 2024
EandrewJones pushed a commit to UMD-ARLIS/superset that referenced this pull request Apr 5, 2024
EnxDev pushed a commit to EnxDev/superset that referenced this pull request Apr 12, 2024
betodealmeida pushed a commit that referenced this pull request Apr 25, 2024
qleroy pushed a commit to qleroy/superset that referenced this pull request Apr 28, 2024
eschutho pushed a commit that referenced this pull request Jun 5, 2024
vinothkumar66 pushed a commit to vinothkumar66/superset that referenced this pull request Nov 11, 2024
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 4.1.0 labels Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/M 🚢 4.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants