mistercrunch commented on issue #33152:
URL: https://github.com/apache/superset/issues/33152#issuecomment-2902887008

   GPT's summary of my thoughts with a proposal:
   
   ## GPT Recommended Approach for Superset 5.0 Migration Issue with 
`chart.query_context`
   
   **Problem**  
   - The `chart.query_context` field (also known historically as 
`query_object`) is generated entirely on the frontend from `form_data`.
   - Backend features like Alerts & Reports, Cache Warmup, and backend-rendered 
charts (e.g., Table, Pivot Table) rely on this field.
   - It is not versioned, not schema-ed, and not migrated across Superset 
upgrades.
   - After upgrading to 5.0, stale or missing `chart.query_context` can break 
these features.
   - The backend **cannot validate or regenerate** this field — only the 
**frontend logic knows how**.
   
   ---
   
   **Recommendations**
   
   ### ✅ Treat `chart.query_context` as a frontend-generated cache
   - The backend must not assume the field is valid.
   - Only the frontend can:
     - Determine if it's out-of-date
     - Regenerate it from `form_data`
     - Update the database if needed
   
   ---
   
   ### ✅ Add a self-healing runtime layer
   
   #### In backend:
   
   ```
   ensure_query_context_and_run_report(
       refresh_cache_only=refresh_cache_only,
       generate_screenshot=generate_screenshot,
       fix_query_context=True,
   )
   
   safe_chart = load_chart_from_db_definition()
   # At this point, chart.query_context is assumed fresh and correct
   ```
   
   - This function should:
     - Launch a headless browser (or Node sandbox)
     - Trigger the frontend to validate and regenerate `chart.query_context` if 
needed
     - Save the updated field back to the database
     - Proceed with report, alert, or warmup logic
   
   ---
   
   ### ✅ Frontend (TypeScript) logic:
   
   ```ts
   function validateAndMaybeRegenerate(formData, currentQueryContext) {
     const regenerated = buildQueryContext(formData);
     const isDifferent = JSON.stringify(regenerated) !== 
JSON.stringify(currentQueryContext);
     return isDifferent ? regenerated : currentQueryContext;
   }
   ```
   
   - This lives entirely in the frontend and is the **source of truth** for 
freshness.
   
   ---
   
   ### 🧠 Chart Model Enhancement
   
   - Add a new `is_query_context_trusted: boolean` field to the `chart` model.
   - Set it to `false` in any DB migration that might invalidate 
`chart.query_context`.
   - Set it to `true` when it is regenerated and known to be in sync.
   - Use this field to:
     - Identify which charts need regeneration
     - Skip unnecessary regeneration checks in runtime logic
   
   ---
   
   ### 🛠️ Trigger points
   
   This regeneration logic should run automatically:
   - At alert/report runtime
   - During cache warmup jobs
   - Only for chart types that need backend-rendered data (e.g., Table, Pivot 
Table)
   
   ---
   
   ### 🧰 Optional Tools
   
   - CLI: `superset regenerate-query-contexts --only-alerts`
   - UI button: “Fix query object” when editing charts
   - Metadata: use `is_query_context_trusted` and optionally a hash or version 
field to track freshness
   
   ---
   
   **Net Result**: Fully progressive, self-healing system with no disruption to 
users or need for manual upgrades.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to