Skip to content

[ADD] database: check-unused command to detect orphaned x_ fields#159

Open
poma-odoo wants to merge 7 commits into
betafrom
feat-unused-x-fields-poma
Open

[ADD] database: check-unused command to detect orphaned x_ fields#159
poma-odoo wants to merge 7 commits into
betafrom
feat-unused-x-fields-poma

Conversation

@poma-odoo

@poma-odoo poma-odoo commented May 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds a new check-unused command (alias: cu) under the database group
  • --fields checks x_ custom fields (excluding x_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-custom stub for future extension to all non-standard fields (raises NotImplementedError)
  • --save [FILE] writes results to CSV (defaults to unused_fields.csv); omitting it prints a Rich table to the terminal
  • Results include a Reason column: not referenced vs no data

Roadmap

  • Implement checking dev fields too
  • Implement unused views, reports and actions

Data emptiness rules (per field type)

Type Considered empty when
boolean all FALSE or NULL
integer, float, monetary all 0 or NULL
char, text, html, selection all '' or NULL
many2one, date, datetime, reference all NULL
binary, one2many, many2many skipped

Test plan

  • odev cu <db> --fields — prints table with unused fields
  • odev cu <db> --fields --save — writes unused_fields.csv
  • odev cu <db> --fields --save report.csv — writes to named file
  • odev cu <db> --fields --non-custom — raises NotImplementedError
  • odev cu <db> — shows helpful error message

🤖 Generated with Claude Code

poma-odoo and others added 7 commits May 28, 2026 13:39
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>
@poma-odoo poma-odoo requested review from brinkflew and sea-odoo May 28, 2026 12:25

@brinkflew brinkflew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@poma-odoo

Copy link
Copy Markdown
Contributor Author

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).

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.
https://github.com/poma-odoo/odoo-scripts/blob/main/common_tests.py

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
it was mostly useful for R&D so I just stopped using it, it is nostalgic now...

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 find ${1:-.} -not -path '*/static/*' -name '*.xml' -exec sh -c "pyjing ~/src/odoo/odoo/odoo/import_xml.rng {} && echo '{} <ok>' || ( echo $(realpath {}) | sed 's/./^/g') " \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants