From 3749597c1f304f72c09cf32c153c1d689693e108 Mon Sep 17 00:00:00 2001 From: dlpzx Date: Thu, 10 Aug 2023 14:06:18 +0200 Subject: [PATCH 1/3] Fix migration script for v1.2 upgrade --- ...cdc0dc987a_fix_template_column_in_table.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py b/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py index 5c92d6ca5..35eba1f94 100644 --- a/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py +++ b/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py @@ -18,10 +18,36 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### + # Modify column types op.add_column( 'datapipeline', sa.Column('template', sa.String(), nullable=True) ) + op.alter_column( + 'datapipeline', + 'devStages', + existing_type=postgresql.ARRAY(sa.VARCHAR()), + nullable=True + ) + op.alter_column( + 'datapipeline', + 'devStrategy', + existing_type=sa.VARCHAR(), + nullable=True + ) + # Backfill values + table = sa.table('datapipeline', sa.column('devStrategy'), sa.VARCHAR(), sa.column('devStages'), postgresql.ARRAY(sa.VARCHAR())) + op.execute( + table.update() + .where(table.c.devStrategy is None) + .values(devStrategy='gitflowBlueprint') + ) + op.execute( + table.update() + .where(table.c.devStages is None) + .values(devStages=['dev', 'test', 'prod']) + ) + # Force nullable = False op.alter_column( 'datapipeline', 'devStages', From f4aebbcc64fa9dbb1e943b9101a21da2cce14454 Mon Sep 17 00:00:00 2001 From: dlpzx Date: Fri, 11 Aug 2023 09:13:48 +0200 Subject: [PATCH 2/3] Replace op.execute for query, linting and comments --- ...cdc0dc987a_fix_template_column_in_table.py | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py b/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py index 35eba1f94..84cc52faf 100644 --- a/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py +++ b/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py @@ -7,7 +7,9 @@ """ from alembic import op import sqlalchemy as sa +from sqlalchemy import orm, Column, String from sqlalchemy.dialects import postgresql +from sqlalchemy.ext.declarative import declarative_base # revision identifiers, used by Alembic. revision = 'b1cdc0dc987a' @@ -15,10 +17,22 @@ branch_labels = None depends_on = None +Base = declarative_base() + + +class DataPipeline(Base): + __tablename__ = 'datapipeline' + DataPipelineUri = Column( + String, nullable=False, primary_key=True + ) + devStrategy = Column(String, nullable=True) + devStages = Column(postgresql.ARRAY(String), nullable=True) + def upgrade(): # ### commands auto generated by Alembic - please adjust! ### # Modify column types + print("Upgrade devStages and devStrategy column types. Updating nullable to False...") op.add_column( 'datapipeline', sa.Column('template', sa.String(), nullable=True) @@ -35,18 +49,18 @@ def upgrade(): existing_type=sa.VARCHAR(), nullable=True ) + print("Backfilling values for devStages and devStrategy...") # Backfill values - table = sa.table('datapipeline', sa.column('devStrategy'), sa.VARCHAR(), sa.column('devStages'), postgresql.ARRAY(sa.VARCHAR())) - op.execute( - table.update() - .where(table.c.devStrategy is None) - .values(devStrategy='gitflowBlueprint') - ) - op.execute( - table.update() - .where(table.c.devStages is None) - .values(devStages=['dev', 'test', 'prod']) - ) + bind = op.get_bind() + session = orm.Session(bind=bind) + session.query(DataPipeline).filter(DataPipeline.devStrategy is None).update( + {DataPipeline.devStrategy: 'gitflowBlueprint'}, synchronize_session=False) + + session.query(DataPipeline).filter(DataPipeline.devStages is None).update( + {DataPipeline.devStages: ['dev', 'test', 'prod']}, synchronize_session=False) + session.commit() + + print("Backfilling values for devStages and devStrategy is done. Updating nullable to True...") # Force nullable = False op.alter_column( 'datapipeline', From 9633e9355bdbf5b4c622d358000f68a360d5989d Mon Sep 17 00:00:00 2001 From: dlpzx Date: Fri, 11 Aug 2023 09:15:05 +0200 Subject: [PATCH 3/3] comments --- .../versions/b1cdc0dc987a_fix_template_column_in_table.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py b/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py index 84cc52faf..7c134012e 100644 --- a/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py +++ b/backend/migrations/versions/b1cdc0dc987a_fix_template_column_in_table.py @@ -32,7 +32,7 @@ class DataPipeline(Base): def upgrade(): # ### commands auto generated by Alembic - please adjust! ### # Modify column types - print("Upgrade devStages and devStrategy column types. Updating nullable to False...") + print("Upgrade devStages and devStrategy column types. Updating nullable to True...") op.add_column( 'datapipeline', sa.Column('template', sa.String(), nullable=True) @@ -60,7 +60,7 @@ def upgrade(): {DataPipeline.devStages: ['dev', 'test', 'prod']}, synchronize_session=False) session.commit() - print("Backfilling values for devStages and devStrategy is done. Updating nullable to True...") + print("Backfilling values for devStages and devStrategy is done. Updating nullable to False...") # Force nullable = False op.alter_column( 'datapipeline',