-
Notifications
You must be signed in to change notification settings - Fork 14.6k
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
[sql] Correct SQL parameter formatting #5178
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""remove double percents | ||
|
||
Revision ID: 4451805bbaa1 | ||
Revises: afb7730f6a9c | ||
Create Date: 2018-06-13 10:20:35.846744 | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '4451805bbaa1' | ||
down_revision = 'bddc498dd179' | ||
|
||
|
||
from alembic import op | ||
import json | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy import Column, create_engine, ForeignKey, Integer, String, Text | ||
|
||
from superset import db | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class Slice(Base): | ||
__tablename__ = 'slices' | ||
|
||
id = Column(Integer, primary_key=True) | ||
datasource_id = Column(Integer, ForeignKey('tables.id')) | ||
datasource_type = Column(String(200)) | ||
params = Column(Text) | ||
|
||
|
||
class Table(Base): | ||
__tablename__ = 'tables' | ||
|
||
id = Column(Integer, primary_key=True) | ||
database_id = Column(Integer, ForeignKey('dbs.id')) | ||
|
||
|
||
class Database(Base): | ||
__tablename__ = 'dbs' | ||
|
||
id = Column(Integer, primary_key=True) | ||
sqlalchemy_uri = Column(String(1024)) | ||
|
||
|
||
def replace(source, target): | ||
bind = op.get_bind() | ||
session = db.Session(bind=bind) | ||
|
||
query = ( | ||
session.query(Slice, Database) | ||
.join(Table) | ||
.join(Database) | ||
.filter(Slice.datasource_type == 'table') | ||
.all() | ||
) | ||
|
||
for slc, database in query: | ||
try: | ||
engine = create_engine(database.sqlalchemy_uri) | ||
|
||
if engine.dialect.identifier_preparer._double_percents: | ||
params = json.loads(slc.params) | ||
|
||
if 'adhoc_filters' in params: | ||
for filt in params['adhoc_filters']: | ||
if 'sqlExpression' in filt: | ||
filt['sqlExpression'] = ( | ||
filt['sqlExpression'].replace(source, target) | ||
) | ||
|
||
slc.params = json.dumps(params, sort_keys=True) | ||
except Exception: | ||
pass | ||
|
||
session.commit() | ||
session.close() | ||
|
||
|
||
def upgrade(): | ||
replace('%%', '%') | ||
|
||
|
||
def downgrade(): | ||
replace('%', '%%') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ setenv = | |
SUPERSET_CONFIG = tests.superset_test_config | ||
SUPERSET_HOME = {envtmpdir} | ||
py27-mysql: SUPERSET__SQLALCHEMY_DATABASE_URI = mysql://mysqluser:mysqluserpassword@localhost/superset?charset=utf8 | ||
py34-mysql: SUPERSET__SQLALCHEMY_DATABASE_URI = mysql://mysqluser:mysqluserpassword@localhost/superset | ||
py{34,36}-mysql: SUPERSET__SQLALCHEMY_DATABASE_URI = mysql://mysqluser:mysqluserpassword@localhost/superset | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note, we may be able to drop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. This PR just adds the missing Python 3.6 version which I was using for testing locally. Note we've kind of already dropped 3.4 per here. I'll create a new PR to remove reference to it in Tox. |
||
py{27,34,36}-postgres: SUPERSET__SQLALCHEMY_DATABASE_URI = postgresql+psycopg2://postgresuser:pguserpassword@localhost/superset | ||
py{27,34,36}-sqlite: SUPERSET__SQLALCHEMY_DATABASE_URI = sqlite:////{envtmpdir}/superset.db | ||
whitelist_externals = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM. There's a bit of complexity/differences still left around the way explore vs sqllab get their data. I think the main difference is around Hive, the
async
param and whetherhandle_cursor
is called or not (called only in SQL Lab).This PR is a step in the right direction. Maybe eventually we want to make bring more into db_engine_spec, and have a
get_records
that receivesupdate_process=False
that internally would deal withasync
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mistercrunch do you want me to makes these changes here, or should these issues be addressed in a future PR?