d0ffadc3ea48_add_hidden_columns.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = None
  12. branch_labels = ('default',)
  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))
  18. batch_op.add_column(sa.Column('viewhidden', sa.Boolean(), nullable=True))
  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))
  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('topics', schema=None) as batch_op:
  49. batch_op.add_column(sa.Column('hidden', sa.Boolean(), nullable=True))
  50. batch_op.add_column(sa.Column('hidden_at', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))
  51. batch_op.add_column(sa.Column('hidden_by_id', sa.Integer(), nullable=True))
  52. batch_op.create_foreign_key('fk_Topic_hidden_by', 'users', ['hidden_by_id'], ['id'])
  53. # ### end Alembic commands ###
  54. def downgrade():
  55. # ### commands auto generated by Alembic - please adjust! ###
  56. with op.batch_alter_table('topics', schema=None) as batch_op:
  57. batch_op.drop_constraint('fk_Topic_hidden_by', type_='foreignkey')
  58. batch_op.drop_column('hidden_by_id')
  59. batch_op.drop_column('hidden_at')
  60. batch_op.drop_column('hidden')
  61. with op.batch_alter_table('posts', schema=None) as batch_op:
  62. batch_op.drop_constraint('fk_Post_hidden_by', type_='foreignkey')
  63. batch_op.drop_column('hidden_by_id')
  64. batch_op.drop_column('hidden_at')
  65. batch_op.drop_column('hidden')
  66. with op.batch_alter_table('groups', schema=None) as batch_op:
  67. batch_op.drop_column('viewhidden')
  68. batch_op.drop_column('makehidden')
  69. # ### end Alembic commands ###