Skip to content

Commit

Permalink
allow the "USING" statement to be specified on change column calls
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Nov 24, 2014
1 parent 9f33e3d commit fbef981
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ def change_column(table_name, column_name, type, options = {})
quoted_table_name = quote_table_name(table_name)
sql_type = type_to_sql(type, options[:limit], options[:precision], options[:scale])
sql_type << "[]" if options[:array]
execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{sql_type}"
sql = "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{sql_type}"
sql << " USING #{options[:using]}" if options[:using]
execute sql

change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/cases/adapters/postgresql/change_schema_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'cases/helper'

module ActiveRecord
class Migration
class PGChangeSchemaTest < ActiveRecord::TestCase
attr_reader :connection

def setup
super
@connection = ActiveRecord::Base.connection
connection.create_table(:strings) do |t|
t.string :somedate
end
end

def teardown
connection.drop_table :strings
end

def test_change_string_to_date
connection.change_column :strings, :somedate, :timestamp, using: 'CAST("somedate" AS timestamp)'
end
end
end
end

0 comments on commit fbef981

Please sign in to comment.