201709041519_d0ffadc3ea48_add_hidden_columns.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Add hidden columns
  2. Revision ID: d0ffadc3ea48
  3. Revises:
  4. Create Date: 2017-09-04 15:19:38.519991
  5. """
  6. from alembic import op
  7. import sqlalchemy as sa
  8. import flaskbb
  9. # revision identifiers, used by Alembic.
  10. revision = 'd0ffadc3ea48'
  11. down_revision = '881dd22cab94'
  12. branch_labels = ()
  13. depends_on = None
  14. def upgrade():
  15. # ### commands auto generated by Alembic - please adjust! ###
  16. with op.batch_alter_table('groups', schema=None) as batch_op:
  17. batch_op.add_column(sa.Column('makehidden', sa.Boolean(), nullable=True, default=False))
  18. batch_op.add_column(sa.Column('viewhidden', sa.Boolean(), nullable=True, default=False))
  19. with op.batch_alter_table('groups', schema=None) as batch_op:
  20. groups = sa.sql.table(
  21. 'groups',
  22. sa.sql.column('viewhidden'), sa.sql.column('makehidden'),
  23. sa.sql.column('admin'), sa.sql.column('super_mod'), sa.sql.column('mod')
  24. )
  25. batch_op.execute(
  26. groups.update().where(
  27. sa.or_(
  28. groups.c.admin == True,
  29. groups.c.mod == True,
  30. groups.c.super_mod == True
  31. )
  32. ).values(viewhidden=True, makehidden=True)
  33. )
  34. batch_op.execute(
  35. groups.update().where(sa.and_(
  36. groups.c.admin != True,
  37. groups.c.mod != True,
  38. groups.c.super_mod != True
  39. )).values(viewhidden=False, makehidden=False)
  40. )
  41. batch_op.alter_column('viewhidden', existing_type=sa.Boolean(), nullable=False)
  42. batch_op.alter_column('makehidden', existing_type=sa.Boolean(), nullable=False)
  43. with op.batch_alter_table('posts', schema=None) as batch_op:
  44. batch_op.add_column(sa.Column('hidden', sa.Boolean(), nullable=True, default=False))
  45. batch_op.add_column(sa.Column('hidden_at', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))
  46. batch_op.add_column(sa.Column('hidden_by_id', sa.Integer(), nullable=True))
  47. batch_op.create_foreign_key('fk_Post_hidden_by', 'users', ['hidden_by_id'], ['id'])
  48. with op.batch_alter_table('posts', schema=None) as batch_op:
  49. posts = sa.sql.table('posts', sa.sql.column('hidden'))
  50. batch_op.execute(
  51. posts.update().values(hidden=False)
  52. )
  53. batch_op.alter_column('hidden', existing_type=sa.Boolean(), nullable=False)
  54. with op.batch_alter_table('topics', schema=None) as batch_op:
  55. batch_op.add_column(sa.Column('hidden', sa.Boolean(), nullable=True))
  56. batch_op.add_column(sa.Column('hidden_at', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))
  57. batch_op.add_column(sa.Column('hidden_by_id', sa.Integer(), nullable=True))
  58. batch_op.create_foreign_key('fk_Topic_hidden_by', 'users', ['hidden_by_id'], ['id'])
  59. with op.batch_alter_table('topics', schema=None) as batch_op:
  60. topics = sa.sql.table('topics', sa.sql.column('hidden'))
  61. batch_op.execute(
  62. topics.update().values(hidden=False)
  63. )
  64. batch_op.alter_column('hidden', existing_type=sa.Boolean(), nullable=False)
  65. # ### end Alembic commands ###
  66. def downgrade():
  67. # ### commands auto generated by Alembic - please adjust! ###
  68. with op.batch_alter_table('topics', schema=None) as batch_op:
  69. batch_op.drop_constraint('fk_Topic_hidden_by', type_='foreignkey')
  70. batch_op.drop_column('hidden_by_id')
  71. batch_op.drop_column('hidden_at')
  72. batch_op.drop_column('hidden')
  73. with op.batch_alter_table('posts', schema=None) as batch_op:
  74. batch_op.drop_constraint('fk_Post_hidden_by', type_='foreignkey')
  75. batch_op.drop_column('hidden_by_id')
  76. batch_op.drop_column('hidden_at')
  77. batch_op.drop_column('hidden')
  78. with op.batch_alter_table('groups', schema=None) as batch_op:
  79. batch_op.drop_column('viewhidden')
  80. batch_op.drop_column('makehidden')
  81. # ### end Alembic commands ###