Blame alembic/versions/d4d2c5aa8a0_add_granularity_to_watching_repos.py

Matt Prahl b49f93
"""Add granularity to watching repos
Matt Prahl b49f93
Matt Prahl b49f93
Revision ID: d4d2c5aa8a0
Matt Prahl b49f93
Revises: 4255158a6913
Matt Prahl b49f93
Create Date: 2017-04-28 14:39:09.746953
Matt Prahl b49f93
Matt Prahl b49f93
"""
Matt Prahl b49f93
Matt Prahl b49f93
# revision identifiers, used by Alembic.
Matt Prahl b49f93
revision = 'd4d2c5aa8a0'
Matt Prahl b49f93
down_revision = '4255158a6913'
Matt Prahl b49f93
Matt Prahl b49f93
from alembic import op
Matt Prahl b49f93
import sqlalchemy as sa
Matt Prahl b49f93
Matt Prahl b49f93
# A helper table that is a hybrid with both states. This is used for data
Matt Prahl b49f93
# migrations later on.
Matt Prahl b49f93
watcher_helper = sa.Table(
Matt Prahl b49f93
    'watchers',
Matt Prahl b49f93
    sa.MetaData(),
Matt Prahl b49f93
    sa.Column('id', sa.Integer, primary_key=True),
Matt Prahl b49f93
    sa.Column('watch_issues', sa.Boolean),
Matt Prahl b49f93
    sa.Column('watch_commits', sa.Boolean),
Matt Prahl b49f93
    sa.Column('watch', sa.Boolean),
Matt Prahl b49f93
)
Matt Prahl b49f93
Matt Prahl b49f93
Matt Prahl b49f93
def upgrade():
Matt Prahl b49f93
    op.add_column('watchers', sa.Column('watch_commits', sa.Boolean(),
Matt Prahl a47c0d
                                        nullable=True))
Matt Prahl b49f93
    op.add_column('watchers', sa.Column('watch_issues', sa.Boolean(),
Matt Prahl a47c0d
                                        nullable=True))
Matt Prahl b49f93
    # This section is to update the `watch_issues` and `watch_commits` columns
Matt Prahl b49f93
    # with the value of `watch`
Matt Prahl b49f93
    connection = op.get_bind()
Matt Prahl b49f93
    for watcher in connection.execute(watcher_helper.select()):
Matt Prahl b49f93
        connection.execute(
Matt Prahl b49f93
            watcher_helper.update().where(
Matt Prahl b49f93
                watcher_helper.c.id == watcher.id
Matt Prahl b49f93
            ).values(
Matt Prahl b49f93
                watch_issues=watcher.watch,
Matt Prahl b49f93
                watch_commits=False
Matt Prahl b49f93
            )
Matt Prahl b49f93
        )
Matt Prahl b49f93
Matt Prahl b49f93
    with op.batch_alter_table('watchers') as b:
Matt Prahl a47c0d
        # Set nullable to False now that we've set values
Matt Prahl a47c0d
        b.alter_column('watch_issues', nullable=False)
Matt Prahl a47c0d
        b.alter_column('watch_commits', nullable=False)
Matt Prahl b49f93
        # Remove the watch column
Matt Prahl b49f93
        b.drop_column('watch')
Matt Prahl b49f93
Matt Prahl b49f93
Matt Prahl b49f93
def downgrade():
Matt Prahl a47c0d
    op.add_column('watchers', sa.Column('watch', sa.BOOLEAN(), nullable=True))
Matt Prahl b49f93
Matt Prahl b49f93
    # This section is to update the `watch` column with the value of
Matt Prahl b49f93
    # `watch_issues`
Matt Prahl b49f93
    connection = op.get_bind()
Matt Prahl b49f93
    for watcher in connection.execute(watcher_helper.select()):
Matt Prahl b49f93
        connection.execute(
Matt Prahl b49f93
            watcher_helper.update().where(
Matt Prahl b49f93
                watcher_helper.c.id == watcher.id
Matt Prahl b49f93
            ).values(
Matt Prahl b49f93
                watch=watcher.watch_issues
Matt Prahl b49f93
            )
Matt Prahl b49f93
        )
Matt Prahl b49f93
Matt Prahl b49f93
    with op.batch_alter_table('watchers') as b:
Matt Prahl a47c0d
        # Set nullable to False now that we've set values
Matt Prahl a47c0d
        b.alter_column('watch', nullable=False)
Matt Prahl b49f93
        # Drop the added columns
Matt Prahl b49f93
        b.drop_column('watch_issues')
Matt Prahl b49f93
        b.drop_column('watch_commits')