d4d2c5aa8a0_add_granularity_to_watching_repos.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Add granularity to watching repos
  2. Revision ID: d4d2c5aa8a0
  3. Revises: 4255158a6913
  4. Create Date: 2017-04-28 14:39:09.746953
  5. """
  6. # revision identifiers, used by Alembic.
  7. revision = 'd4d2c5aa8a0'
  8. down_revision = '4255158a6913'
  9. from alembic import op
  10. import sqlalchemy as sa
  11. # A helper table that is a hybrid with both states. This is used for data
  12. # migrations later on.
  13. watcher_helper = sa.Table(
  14. 'watchers',
  15. sa.MetaData(),
  16. sa.Column('id', sa.Integer, primary_key=True),
  17. sa.Column('watch_issues', sa.Boolean),
  18. sa.Column('watch_commits', sa.Boolean),
  19. sa.Column('watch', sa.Boolean),
  20. )
  21. def upgrade():
  22. op.add_column('watchers', sa.Column('watch_commits', sa.Boolean(),
  23. nullable=True))
  24. op.add_column('watchers', sa.Column('watch_issues', sa.Boolean(),
  25. nullable=True))
  26. # This section is to update the `watch_issues` and `watch_commits` columns
  27. # with the value of `watch`
  28. connection = op.get_bind()
  29. for watcher in connection.execute(watcher_helper.select()):
  30. connection.execute(
  31. watcher_helper.update().where(
  32. watcher_helper.c.id == watcher.id
  33. ).values(
  34. watch_issues=watcher.watch,
  35. watch_commits=False
  36. )
  37. )
  38. with op.batch_alter_table('watchers') as b:
  39. # Set nullable to False now that we've set values
  40. b.alter_column('watch_issues', nullable=False)
  41. b.alter_column('watch_commits', nullable=False)
  42. # Remove the watch column
  43. b.drop_column('watch')
  44. def downgrade():
  45. op.add_column('watchers', sa.Column('watch', sa.BOOLEAN(), nullable=True))
  46. # This section is to update the `watch` column with the value of
  47. # `watch_issues`
  48. connection = op.get_bind()
  49. for watcher in connection.execute(watcher_helper.select()):
  50. connection.execute(
  51. watcher_helper.update().where(
  52. watcher_helper.c.id == watcher.id
  53. ).values(
  54. watch=watcher.watch_issues
  55. )
  56. )
  57. with op.batch_alter_table('watchers') as b:
  58. # Set nullable to False now that we've set values
  59. b.alter_column('watch', nullable=False)
  60. # Drop the added columns
  61. b.drop_column('watch_issues')
  62. b.drop_column('watch_commits')