From c17f91223965cff2ede653402caad9c5ab15081c Mon Sep 17 00:00:00 2001 From: Adrian Turjak Date: Wed, 4 Nov 2020 10:16:08 +1300 Subject: [PATCH] Fix migrations reusing column objects Closes: https://github.com/gnocchixyz/gnocchi/issues/1080 Related to: https://github.com/sqlalchemy/sqlalchemy/issues/5669 (cherry picked from commit 5eeb3f06f48cfa7bd1b4152130241a6e7b40798c) --- .../5c4f93e5bb4_mysql_float_to_timestamp.py | 10 +++---- .../aba5a217ca9b_merge_created_in_creator.py | 26 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/gnocchi/indexer/alembic/versions/5c4f93e5bb4_mysql_float_to_timestamp.py b/gnocchi/indexer/alembic/versions/5c4f93e5bb4_mysql_float_to_timestamp.py index 6c73dd73e..74a790a61 100644 --- a/gnocchi/indexer/alembic/versions/5c4f93e5bb4_mysql_float_to_timestamp.py +++ b/gnocchi/indexer/alembic/versions/5c4f93e5bb4_mysql_float_to_timestamp.py @@ -55,18 +55,18 @@ def upgrade(): existing_type = sa.types.DECIMAL( precision=20, scale=6, asdecimal=True) - existing_col = sa.Column( + existing_col = lambda: sa.Column( column_name, existing_type, nullable=nullable) - temp_col = sa.Column( + temp_col = lambda: sa.Column( column_name + "_ts", sqlalchemy_types.TimestampUTC(), nullable=True) - op.add_column(table_name, temp_col) - t = sa.sql.table(table_name, existing_col, temp_col) + op.add_column(table_name, temp_col()) + t = sa.sql.table(table_name, existing_col(), temp_col()) op.execute(t.update().values( - **{column_name + "_ts": func.from_unixtime(existing_col)})) + **{column_name + "_ts": func.from_unixtime(existing_col())})) op.drop_column(table_name, column_name) op.alter_column(table_name, column_name + "_ts", diff --git a/gnocchi/indexer/alembic/versions/aba5a217ca9b_merge_created_in_creator.py b/gnocchi/indexer/alembic/versions/aba5a217ca9b_merge_created_in_creator.py index 72339057b..5d133c851 100644 --- a/gnocchi/indexer/alembic/versions/aba5a217ca9b_merge_created_in_creator.py +++ b/gnocchi/indexer/alembic/versions/aba5a217ca9b_merge_created_in_creator.py @@ -34,20 +34,24 @@ def upgrade(): for table_name in ("resource", "resource_history", "metric"): - creator_col = sa.Column("creator", sa.String(255)) - created_by_user_id_col = sa.Column("created_by_user_id", - sa.String(255)) - created_by_project_id_col = sa.Column("created_by_project_id", - sa.String(255)) - op.add_column(table_name, creator_col) + creator_col = lambda: sa.Column("creator", sa.String(255)) + created_by_user_id_col = lambda: sa.Column( + "created_by_user_id", sa.String(255)) + created_by_project_id_col = lambda: sa.Column( + "created_by_project_id", sa.String(255)) + op.add_column(table_name, creator_col()) t = sa.sql.table( - table_name, creator_col, - created_by_user_id_col, created_by_project_id_col) + table_name, + creator_col(), + created_by_user_id_col(), + created_by_project_id_col(), + ) op.execute( t.update().values( creator=( - created_by_user_id_col + ":" + created_by_project_id_col - )).where((created_by_user_id_col is not None) - | (created_by_project_id_col is not None))) + created_by_user_id_col() + ":" + + created_by_project_id_col() + )).where((created_by_user_id_col() is not None) + | (created_by_project_id_col() is not None))) op.drop_column(table_name, "created_by_user_id") op.drop_column(table_name, "created_by_project_id")