Skip to content

Commit 31db2e0

Browse files
committed
fix alembic not detecting the medical history fulltext index
1 parent 63220cc commit 31db2e0

File tree

5 files changed

+53
-20
lines changed

5 files changed

+53
-20
lines changed

backend/app/alembic/versions/60300545fecf_add_fulltext_index_for_patient_medical_.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818

1919

2020
def upgrade() -> None:
21-
# Create GIN index on medical_history column
22-
op.execute("""
23-
CREATE EXTENSION IF NOT EXISTS pg_trgm;
24-
CREATE INDEX IF NOT EXISTS ix_patient_medical_history_gin
25-
ON patient
26-
USING GIN (medical_history gin_trgm_ops);
27-
""")
28-
21+
op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm;")
22+
op.create_index(
23+
'ix_patient_medical_history_gin',
24+
'patient',
25+
['medical_history'],
26+
unique=False,
27+
postgresql_using='gin',
28+
postgresql_ops={'medical_history': 'gin_trgm_ops'}
29+
)
2930

3031
def downgrade() -> None:
31-
# Drop the GIN index
32-
op.execute("""
33-
DROP INDEX IF EXISTS ix_patient_medical_history_gin;
34-
""")
32+
op.drop_index('ix_patient_medical_history_gin', table_name='patient')
33+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""drop storage_path column
2+
3+
Revision ID: da61fe01b001
4+
Revises: e62dc93fe967
5+
Create Date: 2025-02-23 20:49:29.015113
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
import sqlmodel.sql.sqltypes
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = 'da61fe01b001'
15+
down_revision = 'e62dc93fe967'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.drop_column('attachment', 'storage_path')
23+
# ### end Alembic commands ###
24+
25+
26+
def downgrade():
27+
# ### commands auto generated by Alembic - please adjust! ###
28+
op.add_column('attachment', sa.Column('storage_path', sa.VARCHAR(length=1024), autoincrement=False, nullable=False))
29+
# ### end Alembic commands ###

backend/app/models/patients.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
21
from typing import TYPE_CHECKING
32
from datetime import datetime
4-
from sqlmodel import Field, Relationship, SQLModel
3+
4+
from sqlmodel import Field, Relationship, SQLModel, create_engine, Session, select
5+
from sqlalchemy import Index
56
import uuid
67

78
if TYPE_CHECKING:
@@ -27,6 +28,16 @@ class Patient(PatientBase, table=True):
2728
id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
2829
attachments: list["Attachment"] = Relationship(back_populates="patient", cascade_delete=True)
2930

31+
__table_args__ = (
32+
# define a fulltext index on medical_history
33+
Index(
34+
'ix_patient_medical_history_gin',
35+
'medical_history',
36+
postgresql_using='gin',
37+
postgresql_ops={'medical_history': 'gin_trgm_ops'}
38+
),
39+
)
40+
3041
# Properties to return via API, id is always required
3142
class PatientPublic(PatientBase):
3243
id: uuid.UUID

backend/app/tests/api/routes/test_attachments.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def test_create_attachment(
2525
data = {
2626
"file_name": "test.pdf",
2727
"mime_type": "application/pdf",
28-
"storage_path": "patients/attachments/test.pdf",
2928
"description": "Test attachment",
3029
"patient_id": str(patient.id)
3130
}
@@ -58,7 +57,6 @@ def test_read_attachment_details(
5857
attachment = Attachment(
5958
file_name="test.pdf",
6059
mime_type="application/pdf",
61-
storage_path="patients/attachments/test.pdf",
6260
description="Test attachment",
6361
patient_id=patient.id
6462
)
@@ -94,13 +92,11 @@ def test_list_attachments(
9492
attachment1 = Attachment(
9593
file_name="test1.pdf",
9694
mime_type="application/pdf",
97-
storage_path="patients/attachments/test1.pdf",
9895
patient_id=patient.id
9996
)
10097
attachment2 = Attachment(
10198
file_name="test2.pdf",
10299
mime_type="application/pdf",
103-
storage_path="patients/attachments/test2.pdf",
104100
patient_id=patient.id
105101
)
106102
db.add(attachment1)

backend/app/tests/api/routes/test_patients.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,11 @@ def test_read_patients_attachment_filter(
210210
attachment1 = Attachment(
211211
file_name="test1.bam",
212212
mime_type="application/x-bam",
213-
storage_path="patients/attachments/test1.bam",
214213
patient_id=patient1.id
215214
)
216215
attachment2 = Attachment(
217216
file_name="test.cram",
218217
mime_type="application/x-cram",
219-
storage_path="patients/attachments/test.cram",
220218
patient_id=patient2.id
221219
)
222220
db.add(attachment1)

0 commit comments

Comments
 (0)