A utility to port your app's `frappe.db.sql` to Frappe Query Builder

Now that Frappe has announced Postgres support for ERPNext, raw frappe.db.sql() calls in your custom app are likely to break or return unexpected results. You can run our codemod to convert your existing frappe.db.sql calls at a >95% success rate. The basic flow is scan → triage → rewrite → validate:

sql_registry scan --directory ~/apps/your_app   # builds .sql_registry.json
sql_registry report --output sql_report.md       # status per call
sql_registry rewrite <call_id> --apply            # convert one call in place

Each call is classified as one of “Query Builder”, “ORM-eligible” or “Manual Review.” Conversions are parsed with sqlglot, regenerated as Query Builder/ORM, and checked for structural equivalence before being marked complete. Calls are addressed by AST path rather than line number, so applying one rewrite doesn’t invalidate the others.

We’ve run it against our apps, Frappe, ERPNext and HRMS and it does a good job on all but the most complex cases and the most poorly written SQL. It is also available as a pre-commit hook so you can prevent new database-specific SQL from entering your codebase with automatic refactor. This codemod doesn’t use any LLMs but the mid tier ones are pretty good at cleaning up the codemod’s attempt for manual fixes.

Complete documentation is here

4 Likes