@@ -66,6 +66,16 @@ async def schema_version(connection: asyncpg.Connection) -> str|None:
6666 )
6767 return row [0 ] if row is not None else None
6868
69+ def schema_mismatch_error (expected : str , actual : str | None , query_id : str | None = None ) -> str :
70+ #=============================================================================
71+ found = actual if actual is not None else 'missing metadata/schema_version'
72+ query = f' (query { query_id } )' if query_id is not None else ''
73+ return (
74+ f'Competency schema version mismatch{ query } : '
75+ f'expected `{ expected } ` but found `{ found } `. '
76+ 'Some queries may fail until the database schema and query definitions are aligned.'
77+ )
78+
6979#===============================================================================
7080#===============================================================================
7181
@@ -158,6 +168,7 @@ async def query(data: QueryRequest, request: Request) -> QueryResults|QueryError
158168 return {'error' : f'Error building query: { err } ' }
159169 if (pool := get_competency_pool (request .app )) is None :
160170 return {'error' : 'Backend cannot connect to Competency database' }
171+ db_schema = get_competency_schema_version (request .app )
161172 try :
162173 async with pool .acquire () as connection :
163174 records = await connection .fetch (sql , * params )
@@ -173,6 +184,9 @@ async def query(data: QueryRequest, request: Request) -> QueryResults|QueryError
173184 }
174185 }
175186 except Exception as err :
176- return {'error' : f'Error executing query: { err } ' }
187+ error_msg = f'Error executing query: { err } .'
188+ if db_schema != COMPETENCY_SCHEMA_VERSION :
189+ error_msg += f' { schema_mismatch_error (COMPETENCY_SCHEMA_VERSION , db_schema , data ["query_id" ])} '
190+ return {'error' : error_msg }
177191
178192#===============================================================================
0 commit comments