242a2047eae0_update_chat_table.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Update chat table
  2. Revision ID: 242a2047eae0
  3. Revises: 6a39f3d8e55c
  4. Create Date: 2024-10-09 21:02:35.241684
  5. """
  6. from alembic import op
  7. import sqlalchemy as sa
  8. from sqlalchemy.sql import table, select, update
  9. import json
  10. revision = "242a2047eae0"
  11. down_revision = "6a39f3d8e55c"
  12. branch_labels = None
  13. depends_on = None
  14. def upgrade():
  15. conn = op.get_bind()
  16. inspector = sa.inspect(conn)
  17. columns = inspector.get_columns("chat")
  18. column_dict = {col["name"]: col for col in columns}
  19. chat_column = column_dict.get("chat")
  20. old_chat_exists = "old_chat" in column_dict
  21. if chat_column:
  22. if isinstance(chat_column["type"], sa.Text):
  23. print("Converting 'chat' column to JSON")
  24. if old_chat_exists:
  25. print("Dropping old 'old_chat' column")
  26. op.drop_column("chat", "old_chat")
  27. # Step 1: Rename current 'chat' column to 'old_chat'
  28. print("Renaming 'chat' column to 'old_chat'")
  29. op.alter_column(
  30. "chat", "chat", new_column_name="old_chat", existing_type=sa.Text()
  31. )
  32. # Step 2: Add new 'chat' column of type JSON
  33. print("Adding new 'chat' column of type JSON")
  34. op.add_column("chat", sa.Column("chat", sa.JSON(), nullable=True))
  35. else:
  36. # If the column is already JSON, no need to do anything
  37. pass
  38. # Step 3: Migrate data from 'old_chat' to 'chat'
  39. chat_table = table(
  40. "chat",
  41. sa.Column("id", sa.String(), primary_key=True),
  42. sa.Column("old_chat", sa.Text()),
  43. sa.Column("chat", sa.JSON()),
  44. )
  45. # - Selecting all data from the table
  46. connection = op.get_bind()
  47. results = connection.execute(select(chat_table.c.id, chat_table.c.old_chat))
  48. for row in results:
  49. try:
  50. # Convert text JSON to actual JSON object, assuming the text is in JSON format
  51. json_data = json.loads(row.old_chat)
  52. except json.JSONDecodeError:
  53. json_data = None # Handle cases where the text cannot be converted to JSON
  54. connection.execute(
  55. sa.update(chat_table)
  56. .where(chat_table.c.id == row.id)
  57. .values(chat=json_data)
  58. )
  59. # Step 4: Drop 'old_chat' column
  60. print("Dropping 'old_chat' column")
  61. op.drop_column("chat", "old_chat")
  62. def downgrade():
  63. # Step 1: Add 'old_chat' column back as Text
  64. op.add_column("chat", sa.Column("old_chat", sa.Text(), nullable=True))
  65. # Step 2: Convert 'chat' JSON data back to text and store in 'old_chat'
  66. chat_table = table(
  67. "chat",
  68. sa.Column("id", sa.String(), primary_key=True),
  69. sa.Column("chat", sa.JSON()),
  70. sa.Column("old_chat", sa.Text()),
  71. )
  72. connection = op.get_bind()
  73. results = connection.execute(select(chat_table.c.id, chat_table.c.chat))
  74. for row in results:
  75. text_data = json.dumps(row.chat) if row.chat is not None else None
  76. connection.execute(
  77. sa.update(chat_table)
  78. .where(chat_table.c.id == row.id)
  79. .values(old_chat=text_data)
  80. )
  81. # Step 3: Remove the new 'chat' JSON column
  82. op.drop_column("chat", "chat")
  83. # Step 4: Rename 'old_chat' back to 'chat'
  84. op.alter_column("chat", "old_chat", new_column_name="chat", existing_type=sa.Text())