I could always risk botching my dev database (PostgreSQL) & rebuild it but thought I’d ask first to see if someone had experience.
Ideally all data would be truncated to its integer amount: (eg: 5.7 would become 5) or perhaps it would round values (5.7 becomes 6)? or perhaps it simply nullifies or zeros out all values and data is lost? I suppose the worst outcome would be unreliable data (5.7 becomes 23).
Is there a general rule of thumb for these types of migrations?
class ChangeBookFromDecimalToInteger < ActiveRecord::Migration
def self.up
change_column :book, :price, :integer
end
def self.down
change_column :book, :price, :decimal
end
end
ActiveRecord will send an ALTER TABLE ALTER COLUMN … TYPE to the database and the database will perform the type conversion. PostgreSQL will convert
decimaltointusinground:Note that
round(numeric)rounds to the nearest integer.If you want specific conversion behavior, you should say so with a USING in the ALTER TABLE:
If you need a USING clause, you have to issue the ALTER TABLE by hand as ActiveRecord doesn’t know anything about USING, for example: