From fe6628b0a411929c06a5454cc154d4afeb192035 Mon Sep 17 00:00:00 2001 From: Luca Albertalli Date: Thu, 2 Jun 2016 12:27:59 -0700 Subject: [PATCH] Fix #529 2 - "This Session's transaction has been rolled back" (#531) * Created migration to fix the bug * Working also on MySQL * Added support for Vertica Grains (#515) * Fix #529 1 "This Session's transaction has been rolled back" (#530) * Fixing the specific issue * Added an additional fix for a similar error in #529 Background: - When an object is modified by SQLAlchemy, it is invalidated so need to be fetched again from the DB - If there's an exception during a transaction, SQLAlchemy performs a rollback and mark the connection as dirty. Bug: - When handling exceptions, the exception handler tries to access the name of the cluster in the main object. Since the name has been invalidated due to a write, SQLAlchemy tries to fetch it on a 'dirty' connection and spits out an error. Solution: - Fetch the information for handling the exception before starting the process. * Modified the migration function to to automatically detect the the foreign keys based on the signature. It supports also sqlite using batch migrations * i18n: Fix typo in Druid cluster broker port label (#512) * Update models.py (#541) removing duplicated `user_id` def --- ...3_fix_wrong_constraint_on_table_columns.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 caravel/migrations/versions/1226819ee0e3_fix_wrong_constraint_on_table_columns.py diff --git a/caravel/migrations/versions/1226819ee0e3_fix_wrong_constraint_on_table_columns.py b/caravel/migrations/versions/1226819ee0e3_fix_wrong_constraint_on_table_columns.py new file mode 100644 index 0000000000000..af96538849aa1 --- /dev/null +++ b/caravel/migrations/versions/1226819ee0e3_fix_wrong_constraint_on_table_columns.py @@ -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): + __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']) + \ No newline at end of file