[ADD] database: check-unused command to detect orphaned x_ fields#159
[ADD] database: check-unused command to detect orphaned x_ fields#159poma-odoo wants to merge 7 commits into
Conversation
Scans a local Odoo database for custom fields (x_ prefix) that are not referenced in views, server actions, automations, filters, record rules, mail templates, reports, or exports. Outputs a table or CSV with model, field name, and description. Ported and integrated from a standalone psycopg2 script. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Use self._database.psql(name) instead of the default postgres connector - Exclude fields prefixed with x_plan from the unused field results Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Fields that are referenced in views/code but contain only null/false/0/empty values are now also reported as unused (reason: "no data"). Checked per ttype: - boolean: no TRUE rows - integer/float/monetary: all NULL or 0 - char/text/html/selection: all NULL or empty string - many2one/date/datetime/reference: all NULL - binary/one2many/many2many: skipped Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Rename command to check-unused (alias: cu), file to check_unused.py - Add --fields flag to explicitly select the x_ fields check - Add --all-fields flag stub (raises NotImplementedError) - Replace --csv stdout flag with --save [FILE] that writes to disk (defaults to unused_fields.csv when no path given) - Running without a check flag now shows a helpful error Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
--non-custom is a modifier for --fields that will extend the scope beyond x_ prefixed fields to all non-standard fields (not yet implemented). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Replace bare except Exception with RuntimeError in _field_has_data - Bump minor version for new check-unused command Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
brinkflew
left a comment
There was a problem hiding this comment.
I love the idea. This makes me think to what extent we could supplement this new command to check unused methods and classes as well by building and walking an AST or with existing tools like vulture (which would need to be adapted somehow to understand how Odoo works).
Quality of the code looks great too.
Nice work!
I have an old script for that. It was taking a lot of time, so I made a json cache. I cannot find it now, but the logic is similar to this old script which finds common test classes, it was useful for R&D to find out which common test class might have setUpClass definition which could be useful for the new test. So basically, it used to make a json file of all classes and methods, then it was much faster to query on them. p.s. looking for the script I found my old odev equivalent script for the time I was working in R&D, https://github.com/poma-odoo/odoo-scripts/blob/main/README.md And another thing that I found was the relaxng checker, it was providing a lot better error messages than element Odoo has extra element using pyjing... it is actually a one liner |
Summary
check-unusedcommand (alias:cu) under thedatabasegroup--fieldschecks x_ custom fields (excludingx_plan*) that are either not referenced in views, server actions, automations, filters, record rules, mail templates, reports or exports — or are referenced but contain no meaningful data in the DB--fields --non-customstub for future extension to all non-standard fields (raisesNotImplementedError)--save [FILE]writes results to CSV (defaults tounused_fields.csv); omitting it prints a Rich table to the terminalnot referencedvsno dataRoadmap
Data emptiness rules (per field type)
booleanFALSEorNULLinteger,float,monetary0orNULLchar,text,html,selection''orNULLmany2one,date,datetime,referenceNULLbinary,one2many,many2manyTest plan
odev cu <db> --fields— prints table with unused fieldsodev cu <db> --fields --save— writesunused_fields.csvodev cu <db> --fields --save report.csv— writes to named fileodev cu <db> --fields --non-custom— raises NotImplementedErrorodev cu <db>— shows helpful error message🤖 Generated with Claude Code