From 9892f94fbe44505c8767ea5c4a558cbe6d35dcc6 Mon Sep 17 00:00:00 2001 From: taylorlynn Date: Fri, 20 Feb 2026 11:22:18 -0500 Subject: [PATCH 1/5] changed the stream update logic --- .../implementations/analysis_repository.py | 13 +++++++++++++ app/services/analysis_orchestrator.py | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/repositories/implementations/analysis_repository.py b/app/repositories/implementations/analysis_repository.py index ecd7a46..b944c1a 100644 --- a/app/repositories/implementations/analysis_repository.py +++ b/app/repositories/implementations/analysis_repository.py @@ -33,6 +33,19 @@ async def create(self, analysis: Analysis) -> Analysis: self._session.expunge(model) 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) + + # 2. Merge the state into the current session + merged_obj = await self._session.merge(db_obj) + + # 3. Commit the transaction to release the locks and clear the 'idle in transaction' + 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.""" 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", From d07e2865eee2bca4e36aaee2bf5e409b8746057b Mon Sep 17 00:00:00 2001 From: taylorlynn Date: Fri, 20 Feb 2026 11:23:02 -0500 Subject: [PATCH 2/5] code cleanup --- app/repositories/implementations/analysis_repository.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/repositories/implementations/analysis_repository.py b/app/repositories/implementations/analysis_repository.py index b944c1a..e502f36 100644 --- a/app/repositories/implementations/analysis_repository.py +++ b/app/repositories/implementations/analysis_repository.py @@ -37,11 +37,7 @@ async def create(self, analysis: Analysis) -> Analysis: async def update_stream_safe(self, analysis: Analysis) -> Analysis: """Update with proper async handling.""" db_obj = self._to_model(analysis) - - # 2. Merge the state into the current session merged_obj = await self._session.merge(db_obj) - - # 3. Commit the transaction to release the locks and clear the 'idle in transaction' await self._session.commit() self._session.expunge(merged_obj) From bac8d09fc3d89488f69bae86d2c8ca4514be94b5 Mon Sep 17 00:00:00 2001 From: taylorlynn Date: Fri, 20 Feb 2026 11:25:24 -0500 Subject: [PATCH 3/5] formating --- app/repositories/implementations/analysis_repository.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/repositories/implementations/analysis_repository.py b/app/repositories/implementations/analysis_repository.py index e502f36..29fea4a 100644 --- a/app/repositories/implementations/analysis_repository.py +++ b/app/repositories/implementations/analysis_repository.py @@ -33,14 +33,14 @@ async def create(self, analysis: Analysis) -> Analysis: self._session.expunge(model) 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]: From 657bbceeafb01364509a028bf95ba55a18b7bb51 Mon Sep 17 00:00:00 2001 From: Dorsaf Sallami <37942992+Sallemi7Dorsaf@users.noreply.github.com> Date: Thu, 12 Mar 2026 14:53:47 -0400 Subject: [PATCH 4/5] migration temp fix --- docker-compose.yml | 2 +- ...0c6fd_add_batch_user_id_to_claims_table.py | 53 ++++++++++++------- 2 files changed, 36 insertions(+), 19 deletions(-) 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..8a80d49 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") \ No newline at end of file From 1bd0bb2432c7609854320f1a091b449a5f4d5430 Mon Sep 17 00:00:00 2001 From: Dorsaf Sallami <37942992+Sallemi7Dorsaf@users.noreply.github.com> Date: Thu, 12 Mar 2026 15:10:33 -0400 Subject: [PATCH 5/5] migration temp fix --- .pre-commit-config.yaml | 1 + .../2abb9260c6fd_add_batch_user_id_to_claims_table.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) 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/migrations/versions/2abb9260c6fd_add_batch_user_id_to_claims_table.py b/migrations/versions/2abb9260c6fd_add_batch_user_id_to_claims_table.py index 8a80d49..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 @@ -31,7 +31,7 @@ def upgrade() -> None: """ ) - #The change makes the migration safe by preventing inserts into social_media_clients unless the referenced user already exists in users. + # 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( @@ -79,4 +79,4 @@ def downgrade() -> None: ) # Existing migration: remove the batch_user_id column - op.drop_column("claims", "batch_user_id") \ No newline at end of file + op.drop_column("claims", "batch_user_id")