From 321e82a71c6402dd6ca39d9a8295761897b1923d Mon Sep 17 00:00:00 2001 From: Nathan Rashleigh Date: Mon, 8 Apr 2024 23:20:11 +1000 Subject: [PATCH] add msg_id --- main.py | 2 +- ...3e7b2b_add_msg_id_column_to_attachments.py | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/5dfa093e7b2b_add_msg_id_column_to_attachments.py diff --git a/main.py b/main.py index d5a3a17..cda6b27 100644 --- a/main.py +++ b/main.py @@ -217,7 +217,7 @@ async def main(): # db setup db_url = config['POSTGRES_URL'] - db_engine = create_async_engine(db_url, echo=False) + db_engine = create_async_engine(db_url, echo=True) await migrate_db(db_engine) print('db is up to date') diff --git a/migrations/versions/5dfa093e7b2b_add_msg_id_column_to_attachments.py b/migrations/versions/5dfa093e7b2b_add_msg_id_column_to_attachments.py new file mode 100644 index 0000000..e236b62 --- /dev/null +++ b/migrations/versions/5dfa093e7b2b_add_msg_id_column_to_attachments.py @@ -0,0 +1,34 @@ +"""add msg_id column to attachments + +Revision ID: 5dfa093e7b2b +Revises: 1dc4564189c1 +Create Date: 2024-04-08 23:08:51.542184 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '5dfa093e7b2b' +down_revision: Union[str, None] = '1dc4564189c1' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('attachments', sa.Column('msg_id', sa.BigInteger(), nullable=True)) + op.execute("UPDATE attachments SET msg_id = (message->>'id')::bigint") + op.alter_column('attachments', 'msg_id', nullable=False) + op.create_index(op.f('ix_attachments_msg_id'), 'attachments', ['msg_id'], unique=False) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_attachments_msg_id'), table_name='attachments') + op.drop_column('attachments', 'msg_id') + # ### end Alembic commands ###