diff options
Diffstat (limited to 'activerecord')
3 files changed, 45 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index b0865d9aab..ba18bad504 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -377,8 +377,9 @@ module ActiveRecord end def add_column(table_name, column_name, type, options = {}) - add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{native_database_types[type]}" - add_column_sql << "(#{limit})" if options[:limit] + native_type = native_database_types[type] + add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{native_type[:name]}" + add_column_sql << "(#{options[:limit] || native_type[:limit]})" if options[:limit] || native_type[:limit] add_column_sql << " DEFAULT '#{options[:default]}'" if options[:default] execute(add_column_sql) end diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 0c78b921b6..9537269260 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -66,17 +66,17 @@ module ActiveRecord def native_database_types { - :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", - :string => "varchar(255)", - :text => "text", - :integer => "int(11)", - :float => "float", - :datetime => "datetime", - :timestamp => "datetime", - :time => "datetime", - :date => "date", - :binary => "blob", - :boolean => "tinyint(1)" + :primary_key => { :name => "int(11) DEFAULT NULL auto_increment PRIMARY KEY" }, + :string => { :name => "varchar", :limit => 255 }, + :text => { :name => "text" }, + :integer => { :name => "int", :limit => 11 }, + :float => { :name => "float" }, + :datetime => { :name => "datetime" }, + :timestamp => { :name => "datetime" }, + :time => { :name => "datetime" }, + :date => { :name => "date" }, + :binary => { :name => "blob" }, + :boolean => { :name => "tinyint", :limit => 1 } } end diff --git a/activerecord/test/active_schema_mysql.rb b/activerecord/test/active_schema_mysql.rb new file mode 100644 index 0000000000..d3ee5bfb66 --- /dev/null +++ b/activerecord/test/active_schema_mysql.rb @@ -0,0 +1,31 @@ +require 'abstract_unit' + +class ActiveSchemaTest < Test::Unit::TestCase + def setup + ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do + alias_method :real_execute, :execute + def execute(sql, name = nil) return sql end + end + end + + def teardown + ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:alias_method, :execute, :real_execute) + end + + def test_drop_table + assert_equal "DROP TABLE people", drop_table(:people) + end + + def test_add_column + assert_equal "ALTER TABLE people ADD last_name varchar(255)", add_column(:people, :last_name, :string) + end + + def test_add_column_with_limit + assert_equal "ALTER TABLE people ADD key varchar(32)", add_column(:people, :key, :string, :limit => 32) + end + + private + def method_missing(method_symbol, *arguments) + ActiveRecord::Base.connection.send(method_symbol, *arguments) + end +end
\ No newline at end of file |