Spaces:
Running
Running
fix: add auto-migration for remote_id and summary columns in main.py lifespan
Browse files
alembic/versions/dc6a7b8c9d0e_add_remote_id_to_research_paper.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""add remote_id to research_paper
|
| 2 |
+
|
| 3 |
+
Revision ID: dc6a7b8c9d0e
|
| 4 |
+
Revises: 8a2d3e4f5b6c
|
| 5 |
+
Create Date: 2026-04-26 13:00:00.000000
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from typing import Sequence, Union
|
| 9 |
+
from alembic import op
|
| 10 |
+
import sqlalchemy as sa
|
| 11 |
+
|
| 12 |
+
# revision identifiers, used by Alembic.
|
| 13 |
+
revision: str = 'dc6a7b8c9d0e'
|
| 14 |
+
down_revision: Union[str, None] = '8a2d3e4f5b6c'
|
| 15 |
+
branch_labels: Union[str, Sequence[str], None] = None
|
| 16 |
+
depends_on: Union[str, Sequence[str], None] = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def upgrade() -> None:
|
| 20 |
+
# Add remote_id and summary columns to research_papers table
|
| 21 |
+
op.add_column('research_papers', sa.Column('remote_id', sa.Text(), nullable=True))
|
| 22 |
+
op.add_column('research_papers', sa.Column('summary', sa.Text(), nullable=True))
|
| 23 |
+
op.create_index(op.f('ix_research_papers_remote_id'), 'research_papers', ['remote_id'], unique=False)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def downgrade() -> None:
|
| 27 |
+
op.drop_index(op.f('ix_research_papers_remote_id'), table_name='research_papers')
|
| 28 |
+
op.drop_column('research_papers', 'summary')
|
| 29 |
+
op.drop_column('research_papers', 'remote_id')
|
app/main.py
CHANGED
|
@@ -65,6 +65,20 @@ async def lifespan(app: FastAPI):
|
|
| 65 |
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as ddl_conn:
|
| 66 |
ddl_conn.execute(text("ALTER TABLE users ADD COLUMN research_profile TEXT"))
|
| 67 |
logger.info("Added 'research_profile' column to users table.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
logger.warning(f"Auto-migration warning (non-fatal): {str(e)}")
|
| 70 |
|
|
|
|
| 65 |
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as ddl_conn:
|
| 66 |
ddl_conn.execute(text("ALTER TABLE users ADD COLUMN research_profile TEXT"))
|
| 67 |
logger.info("Added 'research_profile' column to users table.")
|
| 68 |
+
|
| 69 |
+
# Check if 'remote_id' column exists in research_papers
|
| 70 |
+
res_remote = conn.execute(text("SELECT column_name FROM information_schema.columns WHERE table_name='research_papers' AND column_name='remote_id'")).fetchone()
|
| 71 |
+
if not res_remote:
|
| 72 |
+
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as ddl_conn:
|
| 73 |
+
ddl_conn.execute(text("ALTER TABLE research_papers ADD COLUMN remote_id TEXT"))
|
| 74 |
+
logger.info("Added 'remote_id' column to research_papers table.")
|
| 75 |
+
|
| 76 |
+
# Check if 'summary' column exists in research_papers
|
| 77 |
+
res_sum = conn.execute(text("SELECT column_name FROM information_schema.columns WHERE table_name='research_papers' AND column_name='summary'")).fetchone()
|
| 78 |
+
if not res_sum:
|
| 79 |
+
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as ddl_conn:
|
| 80 |
+
ddl_conn.execute(text("ALTER TABLE research_papers ADD COLUMN summary TEXT"))
|
| 81 |
+
logger.info("Added 'summary' column to research_papers table.")
|
| 82 |
except Exception as e:
|
| 83 |
logger.warning(f"Auto-migration warning (non-fatal): {str(e)}")
|
| 84 |
|