GEstimator

Project database format and persistence

Current contract, checked against source on 2026-11-23. Navigation: index · architecture · backend · MCP reporting.

Format and authority

An .eproj file is a SQLite database. The canonical schema is get_orm_model; project defaults and the current marker are in estimator/misc.py. The file version is a key/value row in projecttable, not a separate column:

key: file_version
value: GESTIMATOR_FILE_REFERENCE_VER_2

The modern open path copies a read-only source into a temporary working database using SQLite backup, then validates/migrates the copy. Version 1 migration adds the schedule colour field and updates the marker. Never invoke the engine’s migration directly on an attached library or an original user file. Follow Service.call for the open sequence.

The original design spreadsheet is historical evidence, not the current schema specification.

Core tables

Names below are physical table/column names generated by the current Peewee models. SQLite identifiers are case-insensitive. Except projecttable, each table has an integer id primary key.

Table Columns beyond id Constraints and meaning
projecttable key, value key is the primary key; values are strings, including serialized JSON
schedulecategorytable description, order Unique description; explicit display order
resourcecategorytable description, order Unique description; explicit display order
scheduletable code, description, unit, rate, qty, remarks, ana_remarks, category_id, parent_id, order, suborder, colour Unique code; nullable category and parent foreign keys; nullable unit, numeric values, remarks, suborder and colour
resourcetable code, description, unit, rate, vat, discount, reference, category_id, order Unique code; nullable VAT, discount, reference and category; rate/unit required
sequencetable id_seq, id_sch_id, itemtype, value, code, description Unique (id_seq, id_sch_id); ordered analysis rows for a schedule item; value/code/description nullable
resourceitemtable id_sch_id, id_seq_id, id_res_id, qty, remarks Resource uses within an analysis group; remarks nullable

Foreign keys use cascade deletion. category_id points to the corresponding category table; parent_id points to scheduletable.id. Resource-use foreign keys point to schedule, sequence and resource primary keys respectively. open_database enables PRAGMA foreign_keys=ON on engine connections.

sequencetable.id_seq is the sequence position; resourceitemtable.id_seq_id is a foreign key to the sequence row’s primary key. Do not confuse these when writing queries or changing import/export code.

Numeric storage

Peewee models expose Decimal fields. Schedule/resource rates and resource discounts declare two decimal places with auto_round=True; VAT declares four. Schedule quantities, sequence values and resource-use quantities inherit the Peewee DecimalField defaults (currently five declared decimal places with auto_round=False). These declarations are not a substitute for application validation or guarantees that SQLite arithmetic is exact.

Use the engine and numeric evaluation contract for calculations and entry rounding. Preserve Decimal strings over IPC. Do not recalculate money with JavaScript numbers or raw SQLite floating-point SUMs; MCP reporting deliberately exposes numeric values as text and has separate limits.

Ordering and identities

Category order, item order and schedule suborder define application order. The supported schedule editor hierarchy is parent plus child; do not introduce deeper nesting without a deliberate compatibility change. Keep SUB ANALYSIS after ordinary categories through keep_sub_analysis_last.

Database primary keys, source-qualified item codes, session row UUIDs and MCP positional analysis aliases are different identities. Session UUIDs and undo histories are not new columns in the project format. Preserve record IDs with their values during drafts, undo and row moves; never persist frontend selection or a table’s visible order as the authoritative data model.

Project settings and measurements

Default keys are file_version, project_name, project_item_code, project_resource_code and project_measurement. project_metadata optionally holds author-maintained database notes in Markdown. See metadata.py for ## Category: <name> note sections and MCP source notes.

project_measurement contains JSON from Measurement.get_model:

["Measurement", [caption, items]]

heading item:
["MeasurementItemHeading", [remark]]

custom item:
["MeasurementItemCustom", [itemnos, records, remark, item_remarks, user_data, itemtype]]

itemnos contains linked schedule primary keys or null values; item_remarks contains per-link remarks. records holds lists of raw cell strings, preserving expressions. itemtype selects a trusted installed Python template by its validated name. Template callbacks derive display and totals; do not replace raw record strings with formatted values or serialized callback code.

These schedule links live inside JSON and are not SQL foreign keys. Schedule deletion must clear affected links while retaining measurement records/remarks; otherwise SQLite key reuse can silently retarget them. Test undo restoring links as well as SQL rows. Template numeric types and rounding are documented in the backend contract.

Optional metadata and derived data

meta_enrichment(scope TEXT PRIMARY KEY, data TEXT NOT NULL) is optional. metadata.py reads schema-1 JSON and ignores invalid optional documents; the database scope contains summaries, category/item annotations and provenance. Original descriptions, codes and numeric definitions remain authoritative. Missing enrichment must not prevent ordinary use.

Search indexes and the MCP v_* reporting views are derived outside source files. Reporting views are built in a disposable projection, not installed into user/library databases. Their columns are owned by mcp_query.SCHEMA, described by describe_source section=schema and governed by the MCP contract.

Working copies, save and recovery

The owners are Service, ProjectEditor and persistence.py. The save lock coordinates participating app instances; it is not a lock imposed on every external editor.

Changing this contract

Start with development rules and backend transaction ownership. A schema/format change requires an explicit migration plan, original-file preservation, round-trip coverage and updated documentation. Verify SQLite integrity and foreign keys, measurement link behavior, Decimal results, undo, save/reopen and workbook output using disposable copies of bundled data.

Relevant tests include modern backend, project editors, record drafts, clipboard, document sessions and MCP queries. Follow the testing guide and record actual results in the validation log.