4ace53fd72c8_update_folder_table_datetime.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Update folder table and change DateTime to BigInteger for timestamp fields
  2. Revision ID: 4ace53fd72c8
  3. Revises: af906e964978
  4. Create Date: 2024-10-23 03:00:00.000000
  5. """
  6. from alembic import op
  7. import sqlalchemy as sa
  8. revision = "4ace53fd72c8"
  9. down_revision = "af906e964978"
  10. branch_labels = None
  11. depends_on = None
  12. def upgrade():
  13. # Perform safe alterations using batch operation
  14. with op.batch_alter_table("folder", schema=None) as batch_op:
  15. # Step 1: Remove server defaults for created_at and updated_at
  16. batch_op.alter_column(
  17. "created_at",
  18. server_default=None, # Removing server default
  19. )
  20. batch_op.alter_column(
  21. "updated_at",
  22. server_default=None, # Removing server default
  23. )
  24. # Step 2: Change the column types to BigInteger for created_at
  25. batch_op.alter_column(
  26. "created_at",
  27. type_=sa.BigInteger(),
  28. existing_type=sa.DateTime(),
  29. existing_nullable=False,
  30. postgresql_using="extract(epoch from created_at)::bigint", # Conversion for PostgreSQL
  31. )
  32. # Change the column types to BigInteger for updated_at
  33. batch_op.alter_column(
  34. "updated_at",
  35. type_=sa.BigInteger(),
  36. existing_type=sa.DateTime(),
  37. existing_nullable=False,
  38. postgresql_using="extract(epoch from updated_at)::bigint", # Conversion for PostgreSQL
  39. )
  40. def downgrade():
  41. # Downgrade: Convert columns back to DateTime and restore defaults
  42. with op.batch_alter_table("folder", schema=None) as batch_op:
  43. batch_op.alter_column(
  44. "created_at",
  45. type_=sa.DateTime(),
  46. existing_type=sa.BigInteger(),
  47. existing_nullable=False,
  48. server_default=sa.func.now(), # Restoring server default on downgrade
  49. )
  50. batch_op.alter_column(
  51. "updated_at",
  52. type_=sa.DateTime(),
  53. existing_type=sa.BigInteger(),
  54. existing_nullable=False,
  55. server_default=sa.func.now(), # Restoring server default on downgrade
  56. onupdate=sa.func.now(), # Restoring onupdate behavior if it was there
  57. )