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 #529 2 - "This Session's transaction has been rolled back" #531

Merged
merged 8 commits into from
Jun 2, 2016
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Fix wrong constraint on table columns
Revision ID: 1226819ee0e3
Revises: 956a063c52b3
Create Date: 2016-05-27 15:03:32.980343
"""

# revision identifiers, used by Alembic.
revision = '1226819ee0e3'
down_revision = '956a063c52b3'

from alembic import op
import sqlalchemy as sa

naming_convention = {
"fk":
"fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
}

def find_constraint_name(upgrade = True):
Copy link
Contributor

Choose a reason for hiding this comment

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

On top of this should we probabily generalize this function so can be reused in other migrations?

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 agree in general.
There's just a question of timing, I don't have much time to dedicate on this bug fix right now and I think this issue is pretty urgent (basically Druid doesn't work on any DB that support Foreign Keys).

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, i can do it after this is in :)

__table = 'columns'
__cols = {'column_name'} if upgrade else {'datasource_name'}
__referenced = 'datasources'
__ref_cols = {'datasource_name'} if upgrade else {'column_name'}

engine = op.get_bind().engine
m = sa.MetaData({})
t=sa.Table(__table,m, autoload=True, autoload_with=engine)

for fk in t.foreign_key_constraints:
if fk.referred_table.name == __referenced and \
set(fk.column_keys) == __cols:
return fk.name
return None

def upgrade():
constraint = find_constraint_name() or 'fk_columns_column_name_datasources'
with op.batch_alter_table("columns",
naming_convention=naming_convention) as batch_op:
batch_op.drop_constraint(constraint, type_="foreignkey")
batch_op.create_foreign_key('fk_columns_datasource_name_datasources', 'datasources', ['datasource_name'], ['datasource_name'])

def downgrade():
constraint = find_constraint_name(False) or 'fk_columns_datasource_name_datasources'
with op.batch_alter_table("columns",
naming_convention=naming_convention) as batch_op:
batch_op.drop_constraint(constraint, type_="foreignkey")
batch_op.create_foreign_key('fk_columns_column_name_datasources', 'datasources', ['column_name'], ['datasource_name'])