Blame alembic/versions/644ef887bb6f_add_close_status.py

Pierre-Yves Chibon 73001b
"""Add close status
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
Revision ID: 644ef887bb6f
Pierre-Yves Chibon 73001b
Revises: 368fd931cf7f
Pierre-Yves Chibon 73001b
Create Date: 2016-10-04 15:38:41.908679
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
"""
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
# revision identifiers, used by Alembic.
Pierre-Yves Chibon 73001b
revision = '644ef887bb6f'
Pierre-Yves Chibon 73001b
down_revision = '368fd931cf7f'
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
from alembic import op
Pierre-Yves Chibon 73001b
import sqlalchemy as sa
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
try:
Pierre-Yves Chibon 73001b
    from pagure.lib import model
Pierre-Yves Chibon 73001b
except ImportError:
Pierre-Yves Chibon 73001b
    import sys
Pierre-Yves Chibon 73001b
    sys.path.insert(0, '.')
Pierre-Yves Chibon 73001b
    from pagure.lib import model
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
def upgrade():
Pierre-Yves Chibon 73001b
    ''' Add the column _close_status to the table projects.
Pierre-Yves Chibon 73001b
    '''
Pierre-Yves Chibon 73001b
    op.add_column(
Pierre-Yves Chibon 73001b
        'projects',
Pierre-Yves Chibon 73001b
        sa.Column('_close_status', sa.Text, nullable=True)
Pierre-Yves Chibon 73001b
    )
Pierre-Yves Chibon 73001b
    op.add_column(
Pierre-Yves Chibon 73001b
        'issues',
Pierre-Yves Chibon 73001b
        sa.Column('close_status', sa.Text, nullable=True)
Pierre-Yves Chibon 73001b
    )
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    engine = op.get_bind()
Pierre-Yves Chibon 73001b
    Session = sa.orm.scoped_session(sa.orm.sessionmaker())
Pierre-Yves Chibon 73001b
    Session.configure(bind=engine)
Pierre-Yves Chibon 73001b
    session = Session()
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    # Update all the existing projects
Pierre-Yves Chibon 73001b
    statuses = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon 73001b
    for project in session.query(model.Project).all():
Pierre-Yves Chibon 73001b
        project.close_status = statuses
Pierre-Yves Chibon 73001b
        session.add(project)
Pierre-Yves Chibon 73001b
    session.commit()
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    # Add the status 'Closed' for issues
Pierre-Yves Chibon 73001b
    ticket_stat = model.StatusIssue(status='Closed')
Pierre-Yves Chibon 73001b
    session.add(ticket_stat)
Pierre-Yves Chibon 73001b
    session.commit()
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    # Set the close_status for all the closed tickets
Pierre-Yves Chibon 73001b
    op.execute('''UPDATE "issues" SET "close_status"=status where status != 'Open'; ''')
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    # Mark all the tickets as closed
Pierre-Yves Chibon 73001b
    op.execute('''UPDATE "issues" SET status='Closed' where status != 'Open';  ''')
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon f02145
    # Remove the old status
Pierre-Yves Chibon f02145
    op.execute('''DELETE FROM "status_issue" WHERE "status" NOT IN ('Open', 'Closed'); ''')
Pierre-Yves Chibon f02145
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
def downgrade():
Pierre-Yves Chibon 73001b
    ''' Add the column _close_status to the table projects.
Pierre-Yves Chibon 73001b
    '''
Pierre-Yves Chibon 73001b
    engine = op.get_bind()
Pierre-Yves Chibon 73001b
    Session = sa.orm.scoped_session(sa.orm.sessionmaker())
Pierre-Yves Chibon 73001b
    Session.configure(bind=engine)
Pierre-Yves Chibon 73001b
    session = Session()
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    statuses = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon 73001b
    for status in statuses:
Pierre-Yves Chibon 73001b
        ticket_stat = model.StatusIssue(status=status)
Pierre-Yves Chibon 73001b
        session.add(ticket_stat)
Pierre-Yves Chibon 73001b
        session.commit()
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    # Set the close_status for all the closed tickets
Pierre-Yves Chibon 73001b
    op.execute('''UPDATE "issues" SET status=close_status where status != 'Open'; ''')
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    # Remove the old status
Pierre-Yves Chibon 73001b
    op.execute('''DELETE FROM "status_issue" WHERE status = 'Closed'; ''')
Pierre-Yves Chibon 73001b
Pierre-Yves Chibon 73001b
    op.drop_column('projects', '_close_status')
Pierre-Yves Chibon 73001b
    op.drop_column('issues', 'close_status')