Browse files

Merge pull request #13597 from prathamesh-sonpatki/hstore_migration

Make change_table use object of current database adapter
  • Loading branch information...
2 parents 3a33e8e + eb589fe commit ab21f42245b1e6fa9ccf03d4ebe190803add1569 @senny senny committed Jan 7, 2014
View
8 activerecord/CHANGELOG.md
@@ -1,3 +1,11 @@
+* `change_table` now uses the current adapter's `update_table_definition`
+ method to retrieve a specific table definition.
+ This ensures that `change_table` and `create_table` will use
+ similar objects.
+ Fixes #13577 and #13503.
+
+ *Nishant Modak*, *Prathamesh Sonpatki*, *Rafael Mendonça França*
+
* Fixed ActiveRecord::Store nil conversion TypeError when using YAML coder.
In case the YAML passed as paramter is nil, uses an empty string.
View
10 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -714,7 +714,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
# require the order columns appear in the SELECT.
#
# columns_for_distinct("posts.id", ["posts.created_at desc"])
- def columns_for_distinct(columns, orders) # :nodoc:
+ def columns_for_distinct(columns, orders) #:nodoc:
columns
end
@@ -736,6 +736,10 @@ def remove_timestamps(table_name)
remove_column table_name, :created_at
end
+ def update_table_definition(table_name, base) #:nodoc:
+ Table.new(table_name, base)
+ end
+
protected
def add_index_sort_order(option_strings, column_names, options = {})
if options.is_a?(Hash) && order = options[:order]
@@ -848,10 +852,6 @@ def create_table_definition(name, temporary, options, as = nil)
def create_alter_table(name)
AlterTable.new create_table_definition(name, false, {})
end
-
- def update_table_definition(table_name, base)
- Table.new(table_name, base)
- end
end
end
end
View
10 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -721,6 +721,10 @@ def valid_type?(type)
!native_database_types[type].nil?
end
+ def update_table_definition(table_name, base) #:nodoc:
+ Table.new(table_name, base)
+ end
+
protected
# Returns the version of the connected PostgreSQL server.
@@ -800,7 +804,7 @@ def initialize_type_map(type_map)
end
end
- FEATURE_NOT_SUPPORTED = "0A000" # :nodoc:
+ FEATURE_NOT_SUPPORTED = "0A000" #:nodoc:
def exec_no_cache(sql, name, binds)
log(sql, name, binds) { @connection.async_exec(sql) }
@@ -990,10 +994,6 @@ def extract_table_ref_from_insert_sql(sql)
def create_table_definition(name, temporary, options, as = nil)
TableDefinition.new native_database_types, name, temporary, options, as
end
-
- def update_table_definition(table_name, base)
- Table.new(table_name, base)
- end
end
end
end
View
10 activerecord/lib/active_record/migration/command_recorder.rb
@@ -86,7 +86,7 @@ def #{method}(*args, &block) # def create_table(*args, &block)
alias :remove_belongs_to :remove_reference
def change_table(table_name, options = {})
- yield ConnectionAdapters::Table.new(table_name, self)
+ yield delegate.update_table_definition(table_name, self)
end
private
@@ -159,9 +159,11 @@ def invert_remove_index(args)
# Forwards any missing method call to the \target.
def method_missing(method, *args, &block)
- @delegate.send(method, *args, &block)
- rescue NoMethodError => e
- raise e, e.message.sub(/ for #<.*$/, " via proxy for #{@delegate}")
+ if @delegate.respond_to?(method)
+ @delegate.send(method, *args, &block)
+ else
+ super
+ end
end
end
end
View
17 activerecord/test/cases/adapters/postgresql/hstore_test.rb
@@ -70,6 +70,23 @@ def test_change_table_supports_hstore
Hstore.reset_column_information
end
+ def test_hstore_migration
+ hstore_migration = Class.new(ActiveRecord::Migration) do
+ def change
+ change_table("hstores") do |t|
+ t.hstore :keys
+ end
+ end
+ end
+
+ hstore_migration.new.suppress_messages do
+ hstore_migration.migrate(:up)
+ assert_includes @connection.columns(:hstores).map(&:name), "keys"
+ hstore_migration.migrate(:down)
+ assert_not_includes @connection.columns(:hstores).map(&:name), "keys"
+ end
+ end
+
def test_cast_value_on_write
x = Hstore.new tags: {"bool" => true, "number" => 5}
assert_equal({"bool" => "true", "number" => "5"}, x.tags)
View
3 activerecord/test/cases/migration/command_recorder_test.rb
@@ -4,7 +4,8 @@ module ActiveRecord
class Migration
class CommandRecorderTest < ActiveRecord::TestCase
def setup
- @recorder = CommandRecorder.new
+ connection = ActiveRecord::Base.connection
+ @recorder = CommandRecorder.new(connection)
end
def test_respond_to_delegates

0 comments on commit ab21f42

Please sign in to comment.