diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2ce0c18..4dc80c8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,6 +3,7 @@ repos: rev: 22.10.0 hooks: - id: black + language_version: python3.12 - repo: https://github.com/PyCQA/flake8 rev: 7.1.1 hooks: diff --git a/app/repositories/implementations/analysis_repository.py b/app/repositories/implementations/analysis_repository.py index ecd7a46..29fea4a 100644 --- a/app/repositories/implementations/analysis_repository.py +++ b/app/repositories/implementations/analysis_repository.py @@ -34,6 +34,15 @@ async def create(self, analysis: Analysis) -> Analysis: return self._to_domain(model) + async def update_stream_safe(self, analysis: Analysis) -> Analysis: + """Update with proper async handling.""" + db_obj = self._to_model(analysis) + merged_obj = await self._session.merge(db_obj) + await self._session.commit() + self._session.expunge(merged_obj) + + return analysis + async def get_with_relations(self, analysis_id: UUID) -> Optional[Analysis]: """Get analysis with related sources and feedback.""" query = ( diff --git a/app/services/analysis_orchestrator.py b/app/services/analysis_orchestrator.py index 1a9e2f3..2ea3aa4 100644 --- a/app/services/analysis_orchestrator.py +++ b/app/services/analysis_orchestrator.py @@ -251,7 +251,7 @@ async def _generate_analysis( log_data = LogProbsData(tokens=analysis_text, probs=log_probs) current_analysis.log_probs = log_data - updated_analysis = await self._analysis_repo.update(current_analysis) + updated_analysis = await self._analysis_repo.update_stream_safe(current_analysis) yield { "type": "analysis_complete", diff --git a/docker-compose.yml b/docker-compose.yml index cfc91c6..156bd34 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,7 @@ services: depends_on: misinformation_mitigation_db: condition: service_healthy - command: [ "/app/docker-entrypoint.sh" ] + command: ["sh","/app/docker-entrypoint.sh" ] misinformation_mitigation_db: container_name: misinformation_mitigation_db diff --git a/migrations/versions/2abb9260c6fd_add_batch_user_id_to_claims_table.py b/migrations/versions/2abb9260c6fd_add_batch_user_id_to_claims_table.py index f04cded..a6d5978 100644 --- a/migrations/versions/2abb9260c6fd_add_batch_user_id_to_claims_table.py +++ b/migrations/versions/2abb9260c6fd_add_batch_user_id_to_claims_table.py @@ -3,12 +3,12 @@ Revision ID: 2abb9260c6fd Revises: b2122b621d0a Create Date: 2025-04-10 21:03:31.820890 - """ + from typing import Sequence, Union -from alembic import op import sqlalchemy as sa +from alembic import op # revision identifiers, used by Alembic. revision: str = "2abb9260c6fd" @@ -18,48 +18,65 @@ def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### + # Existing migration: add the new column op.add_column("claims", sa.Column("batch_user_id", sa.Text(), nullable=True)) - # ### end Alembic commands ### + # Existing migration: create the new table op.execute( """ CREATE TABLE social_media_clients ( - auth0_id VARCHAR PRIMARY KEY REFERENCES users(auth0_id), - platform TEXT NOT NULL + auth0_id VARCHAR PRIMARY KEY REFERENCES users(auth0_id), + platform TEXT NOT NULL ); - """ + """ ) + # The change makes the migration safe by preventing inserts into social_media_clients unless the referenced user already exists in users. + + # New: only insert BlueSky client if the matching user already exists op.execute( """ INSERT INTO social_media_clients (auth0_id, platform) - VALUES ('I1eyLfAX26wlOMiY4n5SxWOsWrSNXLWU@clients', 'BlueSky'); - """ + SELECT 'I1eyLfAX26wlOMiY4n5SxWOsWrSNXLWU@clients', 'BlueSky' + WHERE EXISTS ( + SELECT 1 FROM users + WHERE auth0_id = 'I1eyLfAX26wlOMiY4n5SxWOsWrSNXLWU@clients' + ); + """ ) + # New: only insert X client if the matching user already exists op.execute( """ INSERT INTO social_media_clients (auth0_id, platform) - VALUES ('K46Fnu6E21BG0x3KfNknffbKdTbOHlzw@clients', 'X'); - """ + SELECT 'K46Fnu6E21BG0x3KfNknffbKdTbOHlzw@clients', 'X' + WHERE EXISTS ( + SELECT 1 FROM users + WHERE auth0_id = 'K46Fnu6E21BG0x3KfNknffbKdTbOHlzw@clients' + ); + """ ) + # New: only insert Reddit client if the matching user already exists op.execute( """ INSERT INTO social_media_clients (auth0_id, platform) - VALUES ('GbaexhSrWJnbX19M4HYuGH87ROyzwJne@clients', 'Reddit'); - """ + SELECT 'GbaexhSrWJnbX19M4HYuGH87ROyzwJne@clients', 'Reddit' + WHERE EXISTS ( + SELECT 1 FROM users + WHERE auth0_id = 'GbaexhSrWJnbX19M4HYuGH87ROyzwJne@clients' + ); + """ ) def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column("claims", "batch_user_id") - # ### end Alembic commands ### - + # Existing migration: drop the social_media_clients table op.execute( """ DROP TABLE social_media_clients; - """ + """ ) + + # Existing migration: remove the batch_user_id column + op.drop_column("claims", "batch_user_id")