| """Update chat table |
| |
| Revision ID: 242a2047eae0 |
| Revises: 6a39f3d8e55c |
| Create Date: 2024-10-09 21:02:35.241684 |
| |
| """ |
|
|
| from alembic import op |
| import sqlalchemy as sa |
| from sqlalchemy.sql import table, select, update |
|
|
| import json |
|
|
| revision = "242a2047eae0" |
| down_revision = "6a39f3d8e55c" |
| branch_labels = None |
| depends_on = None |
|
|
|
|
| def upgrade(): |
| conn = op.get_bind() |
| inspector = sa.inspect(conn) |
|
|
| columns = inspector.get_columns("chat") |
| column_dict = {col["name"]: col for col in columns} |
|
|
| chat_column = column_dict.get("chat") |
| old_chat_exists = "old_chat" in column_dict |
|
|
| if chat_column: |
| if isinstance(chat_column["type"], sa.Text): |
| print("Converting 'chat' column to JSON") |
|
|
| if old_chat_exists: |
| print("Dropping old 'old_chat' column") |
| op.drop_column("chat", "old_chat") |
|
|
| |
| print("Renaming 'chat' column to 'old_chat'") |
| op.alter_column( |
| "chat", "chat", new_column_name="old_chat", existing_type=sa.Text() |
| ) |
|
|
| |
| print("Adding new 'chat' column of type JSON") |
| op.add_column("chat", sa.Column("chat", sa.JSON(), nullable=True)) |
| else: |
| |
| pass |
|
|
| |
| chat_table = table( |
| "chat", |
| sa.Column("id", sa.String(), primary_key=True), |
| sa.Column("old_chat", sa.Text()), |
| sa.Column("chat", sa.JSON()), |
| ) |
|
|
| |
| connection = op.get_bind() |
| results = connection.execute(select(chat_table.c.id, chat_table.c.old_chat)) |
| for row in results: |
| try: |
| |
| json_data = json.loads(row.old_chat) |
| except json.JSONDecodeError: |
| json_data = None |
|
|
| connection.execute( |
| sa.update(chat_table) |
| .where(chat_table.c.id == row.id) |
| .values(chat=json_data) |
| ) |
|
|
| |
| print("Dropping 'old_chat' column") |
| op.drop_column("chat", "old_chat") |
|
|
|
|
| def downgrade(): |
| |
| op.add_column("chat", sa.Column("old_chat", sa.Text(), nullable=True)) |
|
|
| |
| chat_table = table( |
| "chat", |
| sa.Column("id", sa.String(), primary_key=True), |
| sa.Column("chat", sa.JSON()), |
| sa.Column("old_chat", sa.Text()), |
| ) |
|
|
| connection = op.get_bind() |
| results = connection.execute(select(chat_table.c.id, chat_table.c.chat)) |
| for row in results: |
| text_data = json.dumps(row.chat) if row.chat is not None else None |
| connection.execute( |
| sa.update(chat_table) |
| .where(chat_table.c.id == row.id) |
| .values(old_chat=text_data) |
| ) |
|
|
| |
| op.drop_column("chat", "chat") |
|
|
| |
| op.alter_column("chat", "old_chat", new_column_name="chat", existing_type=sa.Text()) |
|
|