-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: add UUID column to saved_query for export/import #11397
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5c970c3
Add UUID to saved_query
betodealmeida 1e10d5e
Merge branch 'master' into SO-1130
betodealmeida eea5e1f
Merge branch 'master' into SO-1130
betodealmeida e864c9b
Merge branch 'master' into SO-1130
betodealmeida a8d261f
Reuse function from previous migration
betodealmeida 7c6f8ce
Point to new head
betodealmeida c9f8872
Merge branch 'master' into SO-1130
betodealmeida d2d2450
Merge branch 'master' into SO-1130
betodealmeida File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
superset/migrations/versions/96e99fb176a0_add_import_mixing_to_saved_query.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""add_import_mixing_to_saved_query | ||
|
||
Revision ID: 96e99fb176a0 | ||
Revises: af30ca79208f | ||
Create Date: 2020-10-21 21:09:55.945956 | ||
|
||
""" | ||
|
||
import os | ||
import time | ||
from uuid import uuid4 | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects.mysql.base import MySQLDialect | ||
from sqlalchemy.dialects.postgresql.base import PGDialect | ||
from sqlalchemy.exc import OperationalError | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy_utils import UUIDType | ||
|
||
from superset import db | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "96e99fb176a0" | ||
down_revision = "af30ca79208f" | ||
|
||
|
||
Base = declarative_base() | ||
|
||
|
||
class ImportMixin: | ||
id = sa.Column(sa.Integer, primary_key=True) | ||
uuid = sa.Column(UUIDType(binary=True), primary_key=False, default=uuid4) | ||
|
||
|
||
class SavedQuery(Base, ImportMixin): | ||
__tablename__ = "saved_query" | ||
|
||
|
||
default_batch_size = int(os.environ.get("BATCH_SIZE", 200)) | ||
|
||
# Add uuids directly using built-in SQL uuid function | ||
add_uuids_by_dialect = { | ||
MySQLDialect: """UPDATE saved_query SET uuid = UNHEX(REPLACE(uuid(), "-", ""));""", | ||
PGDialect: """UPDATE saved_query SET uuid = uuid_in(md5(random()::text || clock_timestamp()::text)::cstring);""", | ||
} | ||
|
||
|
||
def add_uuids(session, batch_size=default_batch_size): | ||
"""Populate columns with pre-computed uuids""" | ||
bind = op.get_bind() | ||
objects_query = session.query(SavedQuery) | ||
count = objects_query.count() | ||
|
||
# Silently skip if the table is empty (suitable for db initialization) | ||
if count == 0: | ||
return | ||
|
||
print(f"\nAdding uuids for `saved_query`...") | ||
start_time = time.time() | ||
|
||
# Use dialect specific native SQL queries if possible | ||
for dialect, sql in add_uuids_by_dialect.items(): | ||
if isinstance(bind.dialect, dialect): | ||
op.execute(sql) | ||
print(f"Done. Assigned {count} uuids in {time.time() - start_time:.3f}s.") | ||
return | ||
|
||
# Otherwise use Python uuid function | ||
start = 0 | ||
while start < count: | ||
end = min(start + batch_size, count) | ||
for obj, uuid in map(lambda obj: (obj, uuid4()), objects_query[start:end]): | ||
obj.uuid = uuid | ||
session.merge(obj) | ||
session.commit() | ||
if start + batch_size < count: | ||
print(f" uuid assigned to {end} out of {count}\r", end="") | ||
start += batch_size | ||
|
||
print(f"Done. Assigned {count} uuids in {time.time() - start_time:.3f}s.") | ||
|
||
|
||
def upgrade(): | ||
bind = op.get_bind() | ||
session = db.Session(bind=bind) | ||
|
||
# Add uuid column | ||
try: | ||
with op.batch_alter_table("saved_query") as batch_op: | ||
batch_op.add_column( | ||
sa.Column( | ||
"uuid", UUIDType(binary=True), primary_key=False, default=uuid4, | ||
), | ||
) | ||
except OperationalError: | ||
# Ignore column update errors so that we can run upgrade multiple times | ||
pass | ||
|
||
add_uuids(session) | ||
|
||
try: | ||
# Add uniqueness constraint | ||
with op.batch_alter_table("saved_query") as batch_op: | ||
# Batch mode is required for sqllite | ||
batch_op.create_unique_constraint("uq_saved_query_uuid", ["uuid"]) | ||
except OperationalError: | ||
pass | ||
|
||
|
||
def downgrade(): | ||
bind = op.get_bind() | ||
session = db.Session(bind=bind) | ||
|
||
# Remove uuid column | ||
with op.batch_alter_table("saved_query") as batch_op: | ||
batch_op.drop_constraint("uq_saved_query_uuid", type_="unique") | ||
batch_op.drop_column("uuid") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe you can import these functions from the earlier migration script to DRY and also indicate they are related?
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.
I was on the fence about doing this, because it requires small changes in the previous script, and changing a previous migration script seems dangerous.
In this case it seems safe enough... let me know what you think.
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.
Makes sense. Didn't know updates were needed, in that case I'm OK with either approach.