This project is archived and is in readonly mode.
Decimal columns with a scale of 0 are broken using ruby schemas
-
Nick
Having played with this some more, I've got a better idea of the conceptual nature of the problem.
A column's simplified_type seems to be there to specify the Ruby type that best represents the data in a particular sql_type - so specifying DECIMAL(n,0) as an integer actually makes a lot of sense in that context, and makes a lot of things easier when working with these numbers in ruby (since you're passing around Fixnum or Bignum objects, not BigDecimal ones).
The issue is that a column's type is conflated with it's simplified_type - it's assumed that the two are equivalent, when in fact they are not all the time (although they are almost all the time. Maybe this is the only case where the logic is wrong?)
I think SchemaDumper should probably use sql_type directly, rather than attempting to reconstruct it from the simplified_type and the variously parsed options. Alternatively, there should be a canonical_type (or just type) and a simplified_type (containing what type has currently). Then everything that cares about the best Ruby representation of the object can use simplified_type, and SchemaDumper and other things that interface with the database can use canonical_type/type.
Maybe.
-
Nick
Current monkey-patch. This gets almost everything right (although I wouldn't want to see it inside activerecord!) with one exception: the schema_dumper sets the default to 0.0 (it should be 0).
@@@ruby module ActiveRecord module ConnectionAdapters
class Column alias_method :base_simplified_type, :simplified_type def simplified_type(field_type) if field_type.match(/decimal|numeric|number/) && extract_scale(field_type) == 0 && caller.select {|c| c.match(/schema_dumper/) }.nitems > 0 :decimal else base_simplified_type(field_type) end end endend end
-
Marius Nuennerich
Isn't this fixed with commit 532219fd091837a9312a301c74e0fbf06abab3a8 ?
-
Nick
That does indeed fix the issue - excellent stuff! :)
/Nick
-
Santiago Pastorino
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino
- State changed from open to stale