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 migration script for v1.2 upgrade #651

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Changes from all 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
Expand Up @@ -7,21 +7,61 @@
"""
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'
down_revision = '4392a0c9747f'
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 True...")
op.add_column(
'datapipeline',
sa.Column('template', sa.String(), nullable=True)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we declare nullable=True for devStrategy and devStages if both columns are originally nullable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The important part of this stage is to update the existing_type

op.alter_column(
'datapipeline',
'devStages',
existing_type=postgresql.ARRAY(sa.VARCHAR()),
nullable=True
)
op.alter_column(
'datapipeline',
'devStrategy',
existing_type=sa.VARCHAR(),
nullable=True
)
print("Backfilling values for devStages and devStrategy...")
# Backfill values
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 False...")
# Force nullable = False
op.alter_column(
'datapipeline',
'devStages',
Expand Down