114d3a68c1fd_add_updated_on_column_to_issues.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """add last_update to issues and pull-requests
  2. Revision ID: 114d3a68c1fd
  3. Revises: 5083efccac7
  4. Create Date: 2016-11-15 11:02:30.652540
  5. """
  6. # revision identifiers, used by Alembic.
  7. revision = '114d3a68c1fd'
  8. down_revision = '5083efccac7'
  9. from alembic import op
  10. import sqlalchemy as sa
  11. import datetime
  12. def upgrade():
  13. ''' Add the column last_updated to the table issues/pull-requests
  14. '''
  15. op.add_column(
  16. 'issues',
  17. sa.Column('last_updated', sa.DateTime, nullable=True,
  18. default=datetime.datetime.utcnow,
  19. onupdate=datetime.datetime.utcnow)
  20. )
  21. # Update all the tickets having comments
  22. op.execute('''
  23. UPDATE "issues" SET last_updated=o_date
  24. FROM (
  25. SELECT issue_uid, GREATEST(date_created, edited_on) AS o_date
  26. FROM issue_comments
  27. ORDER BY o_date DESC
  28. ) AS subq
  29. WHERE "issues".uid = issue_uid;''')
  30. # Update all the tickets without comments
  31. op.execute('''UPDATE "issues" SET last_updated=date_created '''
  32. '''WHERE last_updated IS NULL;''')
  33. # Require `last_updated` no NULL at the DB level
  34. op.alter_column(
  35. 'issues', 'last_updated',
  36. nullable=False, existing_nullable=True)
  37. op.add_column(
  38. 'pull_requests',
  39. sa.Column('last_updated', sa.DateTime, nullable=True,
  40. default=datetime.datetime.utcnow,
  41. onupdate=datetime.datetime.utcnow)
  42. )
  43. # Update all the PRs having comments
  44. op.execute('''
  45. UPDATE "pull_requests" SET last_updated=o_date
  46. FROM (
  47. SELECT pull_request_uid, GREATEST(date_created, edited_on) AS o_date
  48. FROM pull_request_comments
  49. ORDER BY o_date DESC
  50. ) AS subq
  51. WHERE "pull_requests".uid = pull_request_uid;''')
  52. # Update all the PRs without comments
  53. op.execute('''UPDATE "pull_requests" SET last_updated=date_created '''
  54. '''WHERE last_updated IS NULL;''')
  55. # Require `last_updated` no NULL at the DB level
  56. op.alter_column(
  57. 'pull_requests', 'last_updated',
  58. nullable=False, existing_nullable=True)
  59. def downgrade():
  60. ''' Drop the column last_update from the table issues/pull-requests
  61. '''
  62. op.drop_column('issues', 'last_updated')
  63. op.drop_column('pull_requests', 'last_updated')