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

fix: alter sql columns to long text #7463

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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.
"""update the sql, select_sql, and executed_sql columns in the
query table to support larger text

Revision ID: afc69274c25a
Revises: e9df189e5c7e
Create Date: 2019-05-06 14:30:26.181449

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'afc69274c25a'
down_revision = 'e9df189e5c7e'

text_len = 10 ** 9 - 1


def upgrade():
try:
# Set text length if database accepts it
with op.batch_alter_table('query') as batch_op:
batch_op.alter_column(
'sql', existing_type=sa.Text, type_=sa.Text(length=text_len))
batch_op.alter_column(
'select_sql', existing_type=sa.Text, type_=sa.Text(length=text_len))
batch_op.alter_column(
'executed_sql', existing_type=sa.Text, type_=sa.Text(length=text_len))
except:
# Many databases do not have a length on text objects
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer having a MySQL-specific if block then a try silencing all errors

Copy link
Member

Choose a reason for hiding this comment

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

Actually after reading https://docs.sqlalchemy.org/en/13/core/type_basics.html#sqlalchemy.types.Text I'm not sure either way. It's probably ok as is since catching a more specific exception might be hard because driver-specific...

Copy link
Contributor

@DiggidyDave DiggidyDave May 7, 2019

Choose a reason for hiding this comment

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

I would feel uncomfortable leaving a migration in an indeterminate state thought... this is a bit ugly but since there is divergent behavior between dbs for this column type, what about:

try:
   do_upgrade(with_limit=True)
except:
  do_upgrade(with_limit=False)

# if it fails after that, ok...

Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, are you suggesting that if it errors out then it didn't wan't/need the migration in the first place? That makes sense to me I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if it errors out, it doesn't need the migration in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a thought for the record: If we get rid of the batch_alter_table, it will fail after the first one and we will skip it gracefully. I think that's what we want. Not sure we need the batch here. A failure will fail as a batch anyway. Some really weird degenerate case would be if it is a mysql db and gets through 1 or 2 then the db crashes leaving one unmigrated. But that is not a catastrophic bad state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up making the migration script only update the query table for mysql dbs. I know that it is very specific to Lyft's situation but I felt that my original implementation was not a good solution since every db has different requirements and data storage sizes for Text columns. So I don't think it is a good idea for me to try to set an arbitrary size and I wouldn't be surprised if it messed up for some other dbs.

# so skip altering for those databases
pass


def downgrade():
try:
with op.batch_alter_table('query') as batch_op:
batch_op.alter_column(
'sql', existing_type=sa.Text(length=text_len), type_=sa.Text)
batch_op.alter_column(
'select_sql', existing_type=sa.Text(length=text_len), type_=sa.Text)
batch_op.alter_column(
'executed_sql', existing_type=sa.Text(length=text_len), type_=sa.Text)
except:
pass