Skip to content

Commit 0bcbf27

Browse files
committed
feat(db): add indexes on documents.user_id and documents.filename
1 parent 629728e commit 0bcbf27

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

backend/app/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class Document(Base):
233233

234234
id = Column(GUID, primary_key=True, default=uuid.uuid4)
235235
user_id = Column(GUID, ForeignKey("users.id"), nullable=False, index=True)
236-
filename = Column(String(255), nullable=False)
236+
filename = Column(String(255), nullable=False, index=True)
237237
original_name = Column(String(255), nullable=False)
238238
file_size = Column(Integer, default=0)
239239
page_count = Column(Integer, default=0)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
One-time migration script to add indexes on documents.user_id and documents.filename.
3+
Run this from the 'backend' directory.
4+
"""
5+
import sys
6+
import os
7+
8+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
9+
10+
from app.database import engine
11+
from sqlalchemy import text
12+
13+
def migrate():
14+
print("🚀 Starting migration: adding indexes on 'documents' table...")
15+
try:
16+
with engine.connect() as conn:
17+
conn.execute(text(
18+
"CREATE INDEX IF NOT EXISTS ix_documents_user_id ON documents (user_id)"
19+
))
20+
conn.execute(text(
21+
"CREATE INDEX IF NOT EXISTS ix_documents_filename ON documents (filename)"
22+
))
23+
conn.commit()
24+
print("✅ Migration successful!")
25+
except Exception as e:
26+
print(f"❌ Migration failed: {e}")
27+
28+
if __name__ == "__main__":
29+
migrate()

0 commit comments

Comments
 (0)