Comments (4)
This is how I patched it to work, but it seems far from best practices and I haven't much context to provide a good fix.
Should there be some helper extracted?
"""Make case insensitive hash of query text
Revision ID: 1038c2174f5d
Revises: fd4fc850d7ea
Create Date: 2023-07-16 23:10:12.885949
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table
from redash.utils import gen_query_hash
from redash.query_runner import BaseSQLQueryRunner
import json
# revision identifiers, used by Alembic.
revision = '1038c2174f5d'
down_revision = 'fd4fc850d7ea'
branch_labels = None
depends_on = None
def query_hash(query_text, options):
options_json = json.loads(options)
apply_auto_limit = options_json.get('apply_auto_limit', False)
runner = BaseSQLQueryRunner({})
limit_aware_query = runner.apply_auto_limit(query_text, apply_auto_limit)
return gen_query_hash(limit_aware_query)
def change_query_hash(conn, table, query_text_to):
for record in conn.execute(table.select()):
query_text = query_text_to(record.query)
options = record.options
conn.execute(
table
.update()
.where(table.c.id == record.id)
.values(query_hash=query_hash(query_text, options)))
def upgrade():
queries = table(
'queries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('query', sa.Text),
sa.Column('query_hash', sa.String(length=10)),
sa.Column('options', sa.Text))
conn = op.get_bind()
change_query_hash(conn, queries, query_text_to=str)
def downgrade():
queries = table(
'queries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('query', sa.Text),
sa.Column('query_hash', sa.String(length=10)),
sa.Column('options', sa.Text))
conn = op.get_bind()
change_query_hash(conn, queries, query_text_to=str.lower)
from redash.
NOTE: my migration makes different hashes if upgrade->revert. Looking into.
from redash.
I have tight time so ended up with just making the migration irreversible 😬
Turns out there is a hassle with Snowflake queries and I don't want to mess with that at the moment.
def downgrade():
# This is irreversible mainly because of Snowflake queries which have a grayed out "auto-limit" option in 10.1.0.
# But in a "preview" version it is available and Snowflake queries suddenly become "auto-limited"
# and their hashes becomes different.
# Also it would take a hash correction because BaseSQLQueryRunner's apply_auto_limit in "upgrade branch" has
# own opinion on semicolons.
# To sum up - it is easier to just restore a dump than mess with it.
raise Exception("Irreversible migration")
from redash.
I've encountered issues while migrating real production data - it was failing on queries which had no statements (e.g. empty or everything commented out), so hacked it that way:
"""Make case insensitive hash of query text
Revision ID: 1038c2174f5d
Revises: fd4fc850d7ea
Create Date: 2023-07-16 23:10:12.885949
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table
from redash.utils import gen_query_hash
from redash.query_runner import BaseSQLQueryRunner
import json
import sqlparse
# revision identifiers, used by Alembic.
revision = '1038c2174f5d'
down_revision = 'fd4fc850d7ea'
branch_labels = None
depends_on = None
def is_empty_statement(stmt):
# copy statement object. `copy.deepcopy` fails to do this, so just re-parse it
st = sqlparse.engine.FilterStack()
st.stmtprocess.append(sqlparse.filters.StripCommentsFilter())
stmt = next(st.run(str(stmt)), None)
if stmt is None:
return True
return str(stmt).strip() == ""
def query_hash(query_text, options):
options_json = json.loads(options)
apply_auto_limit = options_json.get('apply_auto_limit', False)
runner = BaseSQLQueryRunner({})
limit_aware_query = runner.apply_auto_limit(query_text, apply_auto_limit)
return gen_query_hash(limit_aware_query)
def change_query_hash(conn, table, query_text_to):
for record in conn.execute(table.select()):
if is_empty_statement(record.query):
continue
query_text = query_text_to(record.query)
options = record.options
conn.execute(
table
.update()
.where(table.c.id == record.id)
.values(query_hash=query_hash(query_text, options)))
def upgrade():
queries = table(
'queries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('query', sa.Text),
sa.Column('query_hash', sa.String(length=10)),
sa.Column('options', sa.Text))
conn = op.get_bind()
change_query_hash(conn, queries, query_text_to=str)
def downgrade():
# This is irreversible mainly because of Snowflake queries which have a grayed out "auto-limit" option in 10.1.0.
# But in a "preview" version it is available and Snowflake queries suddenly become "auto-limited"
# and their hashes becomes different.
# Also it would take a hash correction because BaseSQLQueryRunner's apply_auto_limit in "upgrade branch" has
# own opinion on semicolons.
# To sum up - it is easier to just restore a dump than mess with it.
raise Exception("Irreversible migration")
😬
from redash.
Related Issues (20)
- How to add counter visualization clickable its is possible ? HOT 2
- Feature request: an installation script for Fedora! HOT 1
- Front end data type display exception HOT 7
- puppeteer issue with Apple Silicon chips HOT 19
- Data Source "Redshift (with IAM User/Role)" does not support Redshift Serverless HOT 2
- Redash adds `/redash` prefix if a string starts with `/api` HOT 2
- Boolean field in elasticsearch index mapping causes query to fail HOT 2
- Use preview images after installing redash with script setup HOT 16
- How to deploy services through multiple instances HOT 6
- Error running query: failed communicating with server. Please check your Internet connection and try again. HOT 2
- Introduction of VTable - Collaboration Opportunity HOT 4
- DB upgrade from 8 -> preview fails HOT 4
- User from groups are removed upon logout leaving with defult group
- Failure to start redash with a local docker install (InFailedSqlTransaction) HOT 3
- Map (Markers) visualization fails to center on the markers
- Typo error in message HOT 4
- Query Based Dropdown is not working HOT 1
- API does not return status 404 for some missing resources
- Getting tables fails if Redash DB user has no permissions to a schema in PG database
- Postgres BYTEA columns visualized as hex in table, breaks UI
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redash.