From 677dc9129b676317638a237ab24dc1db94b99c1d Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Mon, 6 Apr 2009 13:57:35 -0400 Subject: [PATCH] Adapt change_column to use USING CAST syntax for Postgres 8.0 and higher to explicitly type cast columns. --- .../connection_adapters/postgresql_adapter.rb | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index b561fd4..3b944b6 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -816,12 +816,18 @@ module ActiveRecord change_column_null(table_name, column_name, false, default) if notnull end + def using_cast(column_name, type, options) + return "" if postgresql_version < 80000 + " USING CAST(#{quote_column_name(column_name)} AS #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}" + end + # Changes the column of a table. def change_column(table_name, column_name, type, options = {}) quoted_table_name = quote_table_name(table_name) begin - execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}" + execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], +options[:scale])}#{using_cast(column_name, type, options)})" rescue ActiveRecord::StatementInvalid => e raise e if postgresql_version > 80000 # This is PostgreSQL 7.x, so we have to use a more arcane way of doing it. -- 1.5.4.3