fix: db initialization and vector registration order

This commit is contained in:
Clawdbot
2026-02-12 10:38:32 +11:00
parent aab18d3a4f
commit effa8fa43d

View File

@@ -18,11 +18,15 @@ EMBEDDING_DIM = 768 # BAAI/bge-base-en-v1.5
mcp = FastMCP("knowledge-mcp")
@contextmanager
def get_db():
def get_db(init=False):
"""Provide a database connection."""
conn = psycopg.connect(DATABASE_URL, autocommit=True)
# Register vector type handler
register_vector(conn)
# Register vector type handler (skip during init phase before extension exists)
if not init:
try:
register_vector(conn)
except Exception as e:
logging.warning(f"Vector registration failed (ignoring if init): {e}")
try:
yield conn
finally:
@@ -31,9 +35,13 @@ def get_db():
def init_db():
"""Initialize database schema."""
try:
with get_db() as conn:
# Pass init=True to skip vector registration before extension exists
with get_db(init=True) as conn:
conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
# Now we can register it for the rest of the session if we wanted,
# but for this function we just need to create tables.
# Notebooks table (simple registry)
conn.execute("""
CREATE TABLE IF NOT EXISTS notebooks (