Skip to content

Commit dfdce6a

Browse files
authored
🔍 fix: Creation of custom_id Index (#135)
* Fixed creation of index on startup issue * updated log statement * updated log message * fixed test
1 parent f7a3254 commit dfdce6a

File tree

3 files changed

+4
-26
lines changed

3 files changed

+4
-26
lines changed

app/services/database.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,10 @@ async def ensure_custom_id_index_on_embedding():
2525

2626
pool = await PSQLDatabase.get_pool()
2727
async with pool.acquire() as conn:
28-
# Check if the index exists
29-
index_exists = await check_index_exists(conn, index_name)
30-
31-
if not index_exists:
32-
# If the index does not exist, create it
33-
await conn.execute(f"""
28+
await conn.execute(f"""
3429
CREATE INDEX IF NOT EXISTS {index_name} ON {table_name} ({column_name});
3530
""")
36-
logger.debug(f"Created index '{index_name}' on '{table_name}({column_name})'")
37-
else:
38-
logger.debug(f"Index '{index_name}' already exists on '{table_name}({column_name})'")
39-
40-
41-
async def check_index_exists(conn, index_name: str) -> bool:
42-
# Adjust the SQL query if necessary
43-
result = await conn.fetchval("""
44-
SELECT EXISTS (
45-
SELECT FROM pg_class c
46-
JOIN pg_namespace n ON n.oid = c.relnamespace
47-
WHERE c.relname = $1 AND n.nspname = 'public' -- Adjust schema if necessary
48-
);
49-
""", index_name)
50-
return result
31+
logger.debug(f"Checking if index '{index_name}' on '{table_name}({column_name}) exists, if not found then the index is created.'")
5132

5233

5334
async def pg_health_check() -> bool:

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from starlette.responses import JSONResponse
99

10-
from app.config import debug_mode, RAG_HOST, RAG_PORT, CHUNK_SIZE, CHUNK_OVERLAP, PDF_EXTRACT_IMAGES, VECTOR_DB_TYPE, \
10+
from app.config import VectorDBType, debug_mode, RAG_HOST, RAG_PORT, CHUNK_SIZE, CHUNK_OVERLAP, PDF_EXTRACT_IMAGES, VECTOR_DB_TYPE, \
1111
LogMiddleware, logger
1212
from app.middleware import security_middleware
1313
from app.routes import document_routes, pgvector_routes
@@ -16,7 +16,7 @@
1616
@asynccontextmanager
1717
async def lifespan(app: FastAPI):
1818
# Startup logic goes here
19-
if VECTOR_DB_TYPE == "pgvector":
19+
if VECTOR_DB_TYPE == VectorDBType.PGVECTOR:
2020
await PSQLDatabase.get_pool() # Initialize the pool
2121
await ensure_custom_id_index_on_embedding()
2222

tests/services/test_database.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ def dummy_pool(monkeypatch):
3434
import asyncio
3535
@pytest.mark.asyncio
3636
async def test_ensure_custom_id_index_on_embedding(monkeypatch, dummy_pool):
37-
async def dummy_check_index_exists(conn, index_name: str) -> bool:
38-
return False
39-
monkeypatch.setattr("app.services.database.check_index_exists", dummy_check_index_exists)
4037
result = await ensure_custom_id_index_on_embedding()
4138
# If no exceptions are raised, the function worked as expected.
4239
assert result is None

0 commit comments

Comments
 (0)