Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use big (64bit) integers for monetary values #154

Merged
merged 3 commits into from Oct 18, 2018
Merged

Use big (64bit) integers for monetary values #154

merged 3 commits into from Oct 18, 2018

Conversation

@orien
Copy link
Member

orien commented Oct 15, 2018

Use bigint for monetary values to avoid integer overflow. This increases the valid range of values from (-231 – 231-1) to (-263 – 263-1). That's a lot of money!

To update an existing installation, apply the following migration.

change_column :double_entry_account_balances, :balance, :bigint, null: false

change_column :double_entry_line_aggregates, :amount, :bigint, null: false

change_column :double_entry_lines, :amount, :bigint, null: false
change_column :double_entry_lines, :balance, :bigint, null: false

Similarly, since version 5.1 Rails has started using bigint as the default type for ActiveRecord primary keys (id column). However, DoubleEntry specified its foreign keys as integer. Here we change that definition to use the Rails references helper. This'll match the type used by Rails: integer on Rails versions prior to 5.1, and bigint on 5.1 and above.

Use this migration to update the foreign keys. A similar migration can be used to update the primary keys.

change_column :double_entry_line_checks, :last_line_id, :bigint, null: false

change_column :double_entry_line_metadata, :line_id, :bigint, null: false

change_column :double_entry_lines, :partner_id, :bigint, null: true
change_column :double_entry_lines, :detail_id, :bigint, null: true

A MySQL 5.6 schema diff on Rails 5.2 looks like this:

45c45
<   `balance` int(11) NOT NULL,
---
>   `balance` bigint(20) NOT NULL,
73c73
<   `amount` int(11) NOT NULL,
---
>   `amount` bigint(20) NOT NULL,
92c92
<   `last_line_id` int(11) NOT NULL,
---
>   `last_line_id` bigint(20) NOT NULL,
111c111
<   `line_id` int(11) NOT NULL,
---
>   `line_id` bigint(20) NOT NULL,
133,135c133,135
<   `amount` int(11) NOT NULL,
<   `balance` int(11) NOT NULL,
<   `partner_id` int(11) DEFAULT NULL,
---
>   `amount` bigint(20) NOT NULL,
>   `balance` bigint(20) NOT NULL,
>   `partner_id` bigint(20) DEFAULT NULL,
138d137
<   `detail_id` int(11) DEFAULT NULL,
139a139
>   `detail_id` bigint(20) DEFAULT NULL,

Fixes #113

orien added 2 commits Oct 14, 2018
In Rails 5.1 the default type for row ids changed from integer to
bigint.
@orien orien requested a review from envato/money-in-manatees Oct 15, 2018
Copy link

ricobl left a comment

Is it a good idea to add the migrations to the changelog?

@orien orien force-pushed the big-integer branch from 90fd33d to 6053091 Oct 17, 2018
@orien

This comment has been minimized.

Copy link
Member Author

orien commented Oct 17, 2018

@ricobl: Yes, it's a very good idea.

I've added them in 6053091.

@orien orien merged commit a230504 into master Oct 18, 2018
2 checks passed
2 checks passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
@orien orien deleted the big-integer branch Oct 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.