@@ -1340,31 +1340,6 @@ def _get_validation_base_schema(sess: Session) -> Tuple[Any, Any, float]:
13401340 return (base_schema_id , base_schema , now )
13411341
13421342
1343- def _ext_models_set (doc : Optional [dict ], inst : Any , inst_id : str ) -> set [str ]:
1344- """Extract model keys from extension document (root or institutions.* layout).
1345-
1346- Args:
1347- doc: Extension schema JSON doc (or None).
1348- inst: Institution row (for id in institutions lookup).
1349- inst_id: Institution id string (for institutions lookup).
1350-
1351- Returns:
1352- Set of lowercase model names (e.g. {"student", "course"}).
1353- """
1354- if not doc or not isinstance (doc , dict ):
1355- return set ()
1356- if isinstance (doc .get ("data_models" ), dict ):
1357- return {str (k ).lower () for k in doc ["data_models" ].keys ()}
1358- inst_key_candidates = {str (getattr (inst , "id" , "" )), inst_id }
1359- insts = doc .get ("institutions" , {})
1360- if isinstance (insts , dict ):
1361- for key in inst_key_candidates :
1362- block = insts .get (key )
1363- if isinstance (block , dict ) and isinstance (block .get ("data_models" ), dict ):
1364- return {str (k ).lower () for k in block ["data_models" ].keys ()}
1365- return set ()
1366-
1367-
13681343def _resolve_edvise_schema (
13691344 sess : Session , now : float
13701345) -> Tuple [str , Optional [Dict [str , Any ]]]:
@@ -1422,109 +1397,6 @@ def _resolve_pdp_schema(
14221397 return (schema_namespace , inst_schema )
14231398
14241399
1425- def _persist_custom_schema_extension (
1426- sess : Session ,
1427- inst_id : str ,
1428- schema_extension : Dict [str , Any ],
1429- base_schema_id : Any ,
1430- cache_key : str ,
1431- ) -> None :
1432- """Deactivate existing extension records and insert new one; update cache."""
1433- import time
1434-
1435- existing_extensions = (
1436- sess .execute (
1437- select (SchemaRegistryTable ).where (
1438- SchemaRegistryTable .inst_id == str_to_uuid (inst_id ),
1439- SchemaRegistryTable .doc_type == DocType .extension ,
1440- SchemaRegistryTable .is_active .is_ (True ),
1441- )
1442- )
1443- .scalars ()
1444- .all ()
1445- )
1446- for existing in existing_extensions :
1447- existing .is_active = False
1448- new_record = SchemaRegistryTable (
1449- doc_type = DocType .extension ,
1450- inst_id = str_to_uuid (inst_id ),
1451- is_pdp = False , # type: ignore
1452- version_label = "1.0.0" ,
1453- extends_schema_id = base_schema_id ,
1454- json_doc = schema_extension ,
1455- is_active = True ,
1456- )
1457- sess .add (new_record )
1458- sess .flush ()
1459- logging .info (
1460- "Schema record inserted for '%s' (deactivated %d existing)" ,
1461- inst_id ,
1462- len (existing_extensions ),
1463- )
1464- STATE ._ext_cache [cache_key ] = (time .monotonic () + EXT_TTL , schema_extension )
1465-
1466-
1467- def _resolve_custom_schema (
1468- sess : Session ,
1469- inst : Any ,
1470- inst_id : str ,
1471- now : float ,
1472- allowed_schemas : List [str ],
1473- bucket : str ,
1474- base_schema : dict ,
1475- base_schema_id : Any ,
1476- file_name : str ,
1477- ) -> Tuple [str , Optional [Dict [str , Any ]]]:
1478- """Resolve schema namespace and extension for custom (non-PDP/ES/legacy) institutions."""
1479- schema_namespace = str (getattr (inst , "id" , "" ))
1480- ext_cache = STATE ._ext_cache
1481- key = str (getattr (inst , "id" , "" ))
1482- cached = ext_cache .get (key )
1483- if cached and now < cached [0 ]:
1484- inst_schema = cached [1 ]
1485- else :
1486- inst_schema = sess .execute (
1487- select (SchemaRegistryTable .json_doc )
1488- .where (
1489- SchemaRegistryTable .inst_id == getattr (inst , "id" , None ),
1490- SchemaRegistryTable .is_active .is_ (True ),
1491- SchemaRegistryTable .doc_type == DocType .extension ,
1492- )
1493- .limit (1 )
1494- ).scalar_one_or_none ()
1495- ext_cache [key ] = (now + EXT_TTL , inst_schema )
1496- inferred_lower = {m .lower () for m in allowed_schemas }
1497- ext_models = _ext_models_set (inst_schema , inst , inst_id )
1498- if inferred_lower .issubset (ext_models ):
1499- return (schema_namespace , inst_schema )
1500- dbc = DatabricksControl ()
1501- schema_extension : Optional [Dict [str , Any ]] = dbc .create_custom_schema_extension (
1502- bucket_name = bucket ,
1503- inst_query = inst ,
1504- file_name = file_name ,
1505- base_schema = base_schema ,
1506- extension_schema = inst_schema ,
1507- )
1508- if schema_extension is not None :
1509- try :
1510- _persist_custom_schema_extension (
1511- sess , inst_id , schema_extension , base_schema_id , key
1512- )
1513- except IntegrityError as e :
1514- sess .rollback ()
1515- logging .warning ("IntegrityError: %s" , e )
1516- except Exception as e :
1517- sess .rollback ()
1518- logging .error ("Unexpected DB error: %s" , e )
1519- raise HTTPException (
1520- status_code = 500 ,
1521- detail = f"Unexpected database error while inserting file record: { e } " ,
1522- )
1523- return (schema_namespace , schema_extension )
1524- logging .info ("No-op: extension already contains this model for inst %s" , inst_id )
1525- return (schema_namespace , inst_schema )
1526-
1527-
15281400def _resolve_schema_namespace_and_extension (
15291401 sess : Session ,
15301402 inst : Any ,
@@ -1536,7 +1408,7 @@ def _resolve_schema_namespace_and_extension(
15361408 base_schema_id : Any ,
15371409 file_name : str ,
15381410) -> Tuple [str , Optional [Dict [str , Any ]]]:
1539- """Resolve schema_namespace and updated_inst_schema by institution type (edvise/pdp/legacy/custom )."""
1411+ """Resolve schema_namespace and updated_inst_schema by institution type (edvise/pdp/legacy)."""
15401412 pdp_id = getattr (inst , "pdp_id" , None )
15411413 edvise_id = getattr (inst , "edvise_id" , None )
15421414 legacy_id = getattr (inst , "legacy_id" , None )
@@ -1552,16 +1424,12 @@ def _resolve_schema_namespace_and_extension(
15521424 return _resolve_pdp_schema (sess , now )
15531425 if legacy_id :
15541426 return ("legacy" , None )
1555- return _resolve_custom_schema (
1556- sess ,
1557- inst ,
1558- inst_id ,
1559- now ,
1560- allowed_schemas ,
1561- bucket ,
1562- base_schema ,
1563- base_schema_id ,
1564- file_name ,
1427+ raise HTTPException (
1428+ status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
1429+ detail = (
1430+ "Institution configuration error: institution has no pdp_id, edvise_id, "
1431+ "or legacy_id; cannot resolve validation schema."
1432+ ),
15651433 )
15661434
15671435
0 commit comments