1
0

644ef887bb6f_add_close_status.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Add close status
  2. Revision ID: 644ef887bb6f
  3. Revises: 368fd931cf7f
  4. Create Date: 2016-10-04 15:38:41.908679
  5. """
  6. # revision identifiers, used by Alembic.
  7. revision = '644ef887bb6f'
  8. down_revision = '368fd931cf7f'
  9. from alembic import op
  10. import sqlalchemy as sa
  11. try:
  12. from pagure.lib import model
  13. except ImportError:
  14. import sys
  15. sys.path.insert(0, '.')
  16. from pagure.lib import model
  17. def upgrade():
  18. ''' Add the column _close_status to the table projects.
  19. '''
  20. op.add_column(
  21. 'projects',
  22. sa.Column('_close_status', sa.Text, nullable=True)
  23. )
  24. op.add_column(
  25. 'issues',
  26. sa.Column('close_status', sa.Text, nullable=True)
  27. )
  28. engine = op.get_bind()
  29. Session = sa.orm.scoped_session(sa.orm.sessionmaker())
  30. Session.configure(bind=engine)
  31. session = Session()
  32. # Update all the existing projects
  33. statuses = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  34. for project in session.query(model.Project).all():
  35. project.close_status = statuses
  36. session.add(project)
  37. session.commit()
  38. # Add the status 'Closed' for issues
  39. ticket_stat = model.StatusIssue(status='Closed')
  40. session.add(ticket_stat)
  41. session.commit()
  42. # Set the close_status for all the closed tickets
  43. op.execute('''UPDATE "issues" SET "close_status"=status where status != 'Open'; ''')
  44. # Mark all the tickets as closed
  45. op.execute('''UPDATE "issues" SET status='Closed' where status != 'Open'; ''')
  46. # Remove the old status
  47. op.execute('''DELETE FROM "status_issue" WHERE "status" NOT IN ('Open', 'Closed'); ''')
  48. def downgrade():
  49. ''' Add the column _close_status to the table projects.
  50. '''
  51. engine = op.get_bind()
  52. Session = sa.orm.scoped_session(sa.orm.sessionmaker())
  53. Session.configure(bind=engine)
  54. session = Session()
  55. statuses = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
  56. for status in statuses:
  57. ticket_stat = model.StatusIssue(status=status)
  58. session.add(ticket_stat)
  59. session.commit()
  60. # Set the close_status for all the closed tickets
  61. op.execute('''UPDATE "issues" SET status=close_status where status != 'Open'; ''')
  62. # Remove the old status
  63. op.execute('''DELETE FROM "status_issue" WHERE status = 'Closed'; ''')
  64. op.drop_column('projects', '_close_status')
  65. op.drop_column('issues', 'close_status')