feat(invoices): store invoice money as NUMERIC(15,4) — money migration phase 1#340
Merged
Merged
Conversation
… phase 1) Phase 1 of the float->Decimal money migration (see docs/todos/invoice-money-decimal-migration-plan.md): convert the invoice, payment and invoice-item money columns (amount, subtotal, discount_value, quantity, price) from double-precision float to Numeric(15, 4) so the database stores exact fixed-precision values. Columns use asdecimal=False for now, so the ORM keeps returning float and the existing float-based arithmetic / float API schemas are unaffected. Phase 2 will switch to asdecimal=True and Decimal math with boundary rounding. Includes a per-tenant migration (scripts/migrate_invoice_money_to_numeric.py) that ALTERs the column types with a numeric cast — run it BEFORE deploying.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 1 of the invoice money
float→Decimalmigration (plan:docs/todos/invoice-money-decimal-migration-plan.md). Storage foundation only — by design this does not change the arithmetic or the API contract yet.What changed
models_per_tenant.py: invoice money columnsFloat→Numeric(15, 4):Invoice.amount,Invoice.subtotal,Invoice.discount_valueInvoiceItem.quantity,InvoiceItem.price,InvoiceItem.amountPayment.amountscripts/migrate_invoice_money_to_numeric.py—ALTER COLUMN ... TYPE NUMERIC(15,4) USING col::numeric(15,4)across all tenant DBs (idempotent; skips columns already numeric). The cast also cleans existing binary-float drift (e.g.19.989999…→19.9900).Why
asdecimal=False(important)The columns use
Numeric(15, 4, asdecimal=False), so the ORM keeps returning float. This is deliberate: flipping toDecimalnow would make existingDecimal * floatarithmetic across reports/stats/services throwTypeError, and would change the float-typed Pydantic response schemas. Keepingasdecimal=Falsemeans:asdecimal=True+Decimalmath with boundary rounding (ROUND_HALF_UP, quantize0.01), and updates PDF/email/UI.Deploy ordering⚠️
Run the migration before deploying this model change:
Scope / not in this PR
Verification
Model imports clean and all 7 columns reflect as
Numeric(precision=15, scale=4, asdecimal=False). Full end-to-end (insert/read round-trip) needs the stack up; flagging for review before Phase 2.