Blame alembic/versions/114d3a68c1fd_add_updated_on_column_to_issues.py

Mark Reynolds 23b4d2
"""add last_update to issues and pull-requests
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
Revision ID: 114d3a68c1fd
Mark Reynolds 23b4d2
Revises: 5083efccac7
Mark Reynolds 23b4d2
Create Date: 2016-11-15 11:02:30.652540
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
"""
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
# revision identifiers, used by Alembic.
Mark Reynolds 23b4d2
revision = '114d3a68c1fd'
Mark Reynolds 23b4d2
down_revision = '5083efccac7'
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
from alembic import op
Mark Reynolds 23b4d2
import sqlalchemy as sa
Mark Reynolds 23b4d2
import datetime
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
def upgrade():
Mark Reynolds 23b4d2
    ''' Add the column last_updated to the table issues/pull-requests
Mark Reynolds 23b4d2
    '''
Mark Reynolds 23b4d2
    op.add_column(
Mark Reynolds 23b4d2
        'issues',
Mark Reynolds 23b4d2
        sa.Column('last_updated', sa.DateTime, nullable=True,
Pierre-Yves Chibon 336bdc
            default=datetime.datetime.utcnow,
Mark Reynolds 23b4d2
            onupdate=datetime.datetime.utcnow)
Mark Reynolds 23b4d2
    )
Pierre-Yves Chibon 3bf0cd
    # Update all the tickets having comments
Pierre-Yves Chibon 336bdc
    op.execute('''
Pierre-Yves Chibon 336bdc
UPDATE "issues" SET last_updated=o_date
Pierre-Yves Chibon 336bdc
FROM (
Pierre-Yves Chibon 336bdc
    SELECT issue_uid, GREATEST(date_created, edited_on) AS o_date
Pierre-Yves Chibon 336bdc
    FROM issue_comments
Pierre-Yves Chibon 336bdc
    ORDER BY o_date DESC
Pierre-Yves Chibon 336bdc
) AS subq
Pierre-Yves Chibon 336bdc
WHERE "issues".uid = issue_uid;''')
Pierre-Yves Chibon 3bf0cd
    # Update all the tickets without comments
Pierre-Yves Chibon 336bdc
    op.execute('''UPDATE "issues" SET last_updated=date_created '''
Pierre-Yves Chibon 336bdc
               '''WHERE last_updated IS NULL;''')
Pierre-Yves Chibon 3bf0cd
    # Require `last_updated` no NULL at the DB level
Pierre-Yves Chibon 3bf0cd
    op.alter_column(
Pierre-Yves Chibon 3bf0cd
        'issues', 'last_updated',
Pierre-Yves Chibon 3bf0cd
        nullable=False, existing_nullable=True)
Pierre-Yves Chibon 3bf0cd
Mark Reynolds 23b4d2
    op.add_column(
Mark Reynolds 23b4d2
        'pull_requests',
Mark Reynolds 23b4d2
        sa.Column('last_updated', sa.DateTime, nullable=True,
Pierre-Yves Chibon 336bdc
            default=datetime.datetime.utcnow,
Mark Reynolds 23b4d2
            onupdate=datetime.datetime.utcnow)
Mark Reynolds 23b4d2
    )
Pierre-Yves Chibon 3bf0cd
    # Update all the PRs having comments
Pierre-Yves Chibon 336bdc
    op.execute('''
Pierre-Yves Chibon 336bdc
UPDATE "pull_requests" SET last_updated=o_date
Pierre-Yves Chibon 336bdc
FROM (
Pierre-Yves Chibon 336bdc
    SELECT pull_request_uid, GREATEST(date_created, edited_on) AS o_date
Pierre-Yves Chibon 336bdc
    FROM pull_request_comments
Pierre-Yves Chibon 336bdc
    ORDER BY o_date DESC
Pierre-Yves Chibon 336bdc
) AS subq
Pierre-Yves Chibon 336bdc
WHERE "pull_requests".uid = pull_request_uid;''')
Pierre-Yves Chibon 3bf0cd
    # Update all the PRs without comments
Pierre-Yves Chibon 336bdc
    op.execute('''UPDATE "pull_requests" SET last_updated=date_created '''
Pierre-Yves Chibon 336bdc
               '''WHERE last_updated IS NULL;''')
Pierre-Yves Chibon 3bf0cd
    # Require `last_updated` no NULL at the DB level
Pierre-Yves Chibon 3bf0cd
    op.alter_column(
Pierre-Yves Chibon 3bf0cd
        'pull_requests', 'last_updated',
Pierre-Yves Chibon 3bf0cd
        nullable=False, existing_nullable=True)
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
Mark Reynolds 23b4d2
def downgrade():
Mark Reynolds 23b4d2
    ''' Drop the column last_update from the table issues/pull-requests
Mark Reynolds 23b4d2
    '''
Mark Reynolds 23b4d2
    op.drop_column('issues', 'last_updated')
Mark Reynolds 23b4d2
    op.drop_column('pull_requests', 'last_updated')