@@ -1050,6 +1050,8 @@ def _parse_collection(self, page_blocks: typing.List[PageBaseBlock], block):
10501050 # Parse column weight configuration from description
10511051 description = database .get ('description' , [])
10521052 column_weights = self ._parse_column_weights (description )
1053+ if column_weights :
1054+ print (f"[Database Parsing] Found property-order config: { column_weights } " )
10531055
10541056 # Prepare HTTP client
10551057 http_client_req = None
@@ -1076,13 +1078,25 @@ def _parse_collection(self, page_blocks: typing.List[PageBaseBlock], block):
10761078
10771079 # Determine sorting logic using utility class
10781080 from utils .database_utils import DatabaseColumnOrderingUtils
1079- sorts = DatabaseColumnOrderingUtils .get_database_sorts (properties )
1080-
1081- # If properties are empty (common with Inline Databases), we miss schema info for sorting.
1082- # Perform a pre-query to infer schema from the first row to check for 'Order' column.
1083- is_default_sort = len (sorts ) == 1 and sorts [0 ].get ('timestamp' ) == 'created_time'
1081+
1082+ # Check for page-order configuration in description first
1083+ description_text = NotionUtils .get_plain_text (description )
1084+ page_order_sorts = DatabaseColumnOrderingUtils .parse_page_order (description_text )
1085+
1086+ if page_order_sorts :
1087+ print (f"[Database Parsing] Found page-order config: { page_order_sorts } " )
1088+ sorts = page_order_sorts
1089+ # Bypassing pre-query if page-order is configured
1090+ is_default_sort = False
1091+ else :
1092+ sorts = DatabaseColumnOrderingUtils .get_database_sorts (properties )
1093+ print (f"[Database Parsing] Using schema-based/default sorts: { sorts } " )
1094+ # If properties are empty (common with Inline Databases), we miss schema info for sorting.
1095+ # Perform a pre-query to infer schema from the first row to check for 'Order' column.
1096+ is_default_sort = len (sorts ) == 1 and sorts [0 ].get ('timestamp' ) == 'created_time'
10841097
10851098 if not properties and is_default_sort :
1099+ print (f"[Database Parsing] Schema properties empty & default sort detected. Attempting Pre-query inference..." )
10861100 # Pre-query one row to check schema
10871101 pre_query_body = {
10881102 "page_size" : 1 ,
@@ -1096,9 +1110,14 @@ def _parse_collection(self, page_blocks: typing.List[PageBaseBlock], block):
10961110 if pre_results :
10971111 # Infer properties from row data
10981112 inferred_props = pre_results [0 ].get ('properties' , {})
1113+ print (f"[Database Parsing] Inferred properties from Pre-query: { list (inferred_props .keys ())} " )
1114+ # Re-calculate sorts with inferred properties
10991115 sorts = DatabaseColumnOrderingUtils .get_database_sorts (inferred_props )
1116+ print (f"[Database Parsing] Recalculated sorts after inference: { sorts } " )
1117+ else :
1118+ print ("[Database Parsing] Pre-query returned no results. Cannot infer schema." )
11001119 except Exception as e :
1101- print (f"Failed to infer schema for sorting: { e } " )
1120+ print (f"[Database Parsing] Failed to infer schema for sorting: { e } " )
11021121
11031122 query_body = {
11041123 "sorts" : sorts
@@ -1108,16 +1127,22 @@ def _parse_collection(self, page_blocks: typing.List[PageBaseBlock], block):
11081127
11091128 response .raise_for_status ()
11101129 results = response .json ().get ('results' , [])
1130+ print (f"[Database Parsing] Query returned { len (results )} rows." )
11111131
11121132 headers = list (properties .keys ()) if properties else []
11131133 if not headers and results :
11141134 # Infer headers from the first row if schema properties are empty
11151135 first_row_props = results [0 ].get ('properties' , {})
11161136 headers = list (first_row_props .keys ())
1137+ print (f"[Database Parsing] Inferred headers from first row: { headers } " )
11171138
11181139 # Apply column ordering
11191140 if headers and column_weights :
1141+ # Only apply weighted ordering if explicitly configured
11201142 headers = self ._sort_columns_by_weight (headers , column_weights )
1143+ print (f"[Database Parsing] Applied column weights. Final headers: { headers } " )
1144+ elif headers :
1145+ print (f"[Database Parsing] No column weights config. Using default headers: { headers } " )
11211146
11221147 rows = []
11231148 for page in results :
0 commit comments