Topic: Columns of type "double" and unit tests


Have been hunting around for a while on various Rails forums and have not found anything mentioning this...

Ruby: ruby 1.8.4 (2005-12-24) [x86_64-linux]
Rails: 1.2.1
ActiveRecord: 1.15.1
MySQL: mysql  Ver 14.12 Distrib 5.0.24a, for pc-linux-gnu (x86_64) using readline 5.1

We have some tables that have columns of type "double".  We can use migrations to create these columns just fine.  However, when we run unit tests that check values of these columns, the tests fail because the test database gets generated with all the double columns changed to float columns.

I've poked around in the ActiveRecord code and have found that columns of type double really aren't supported.  It seems like the migrations we have written that define columns of type double are working due to an accident, not by design.  I know that the ruby type Float is double-precision and will handle the MySQL double columns values, but the ActiveRecord code that creates the test database schema when running tests just doesn't do the right thing.

My question: is this a bug or by design? If it is by design, should I just rewrite the tests to ignore the precision of the results?

Doug Seifert

Re: Columns of type "double" and unit tests

Sounds more like a bug to me. Have you considered submitting a ticket/patch?

Railscasts - Free Ruby on Rails Screencasts

Re: Columns of type "double" and unit tests

I'll try to find the original link, but I have a reference sheet in front of me that shows the RoR datatype and the various datatypes on 8 different databases.

:double is not listed at all. The closest match is :float which maps to a float type in all databases except Oracle which maps to number. From that I think that its an accident that it works at all, it should bork because its an unsupported datatype.

Re: Columns of type "double" and unit tests

Found it, its in the Agile Development book. So the diagram is likely out of date.

Re: Columns of type "double" and unit tests

I will submit a patch.

Along with this bug, I have found another problem:  If you create a migration and specify :options => "XXX" in create_table statements, the options are not preserved when dumping the schema.  So, for example, if I create a table in mysql with :options => "DEFAULT CHARSET=utf8", the dumped schema doesn't include this and all my unit tests run against the default DEFAULT CHARSET, which seems to be latin1 for my version of mysql.  This will not cause problems in 99.99% of cases, but in mine, where I'm dealing with a legacy db that involves internationalization, this makes writing good tests difficult.