aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorÉtienne Barrié <etienne.barrie@gmail.com>2010-05-16 18:50:25 +0200
committerJeremy Kemper <jeremy@bitsweat.net>2010-05-18 10:55:41 -0700
commit3809c80cd55ac2838f050346800889b6f8e041ef (patch)
tree220fa9b645e29e4a31c166ba813bb14e39fd33b9 /activerecord/test/cases
parentd3e62fc57ce3a0a1df62359f53d217d434c2d2e6 (diff)
downloadrails-3809c80cd55ac2838f050346800889b6f8e041ef.tar.gz
rails-3809c80cd55ac2838f050346800889b6f8e041ef.tar.bz2
rails-3809c80cd55ac2838f050346800889b6f8e041ef.zip
make add_index and remove_index more resilient; new rename_index method; track database limits
[#3452 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/active_schema_test_mysql.rb5
-rw-r--r--activerecord/test/cases/migration_test.rb34
-rw-r--r--activerecord/test/cases/schema_test_postgresql.rb4
3 files changed, 41 insertions, 2 deletions
diff --git a/activerecord/test/cases/active_schema_test_mysql.rb b/activerecord/test/cases/active_schema_test_mysql.rb
index f4d123be15..3526f49afd 100644
--- a/activerecord/test/cases/active_schema_test_mysql.rb
+++ b/activerecord/test/cases/active_schema_test_mysql.rb
@@ -16,6 +16,10 @@ class ActiveSchemaTest < ActiveRecord::TestCase
end
def test_add_index
+ # add_index calls index_exists? which can't work since execute is stubbed
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:define_method, :index_exists?) do |*|
+ false
+ end
expected = "CREATE INDEX `index_people_on_last_name` ON `people` (`last_name`)"
assert_equal expected, add_index(:people, :last_name, :length => nil)
@@ -30,6 +34,7 @@ class ActiveSchemaTest < ActiveRecord::TestCase
expected = "CREATE INDEX `index_people_on_last_name_and_first_name` ON `people` (`last_name`(15), `first_name`(10))"
assert_equal expected, add_index(:people, [:last_name, :first_name], :length => {:last_name => 15, :first_name => 10})
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:remove_method, :index_exists?)
end
def test_drop_table
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 768a44f6df..851daa69a5 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -119,6 +119,40 @@ if ActiveRecord::Base.connection.supports_migrations?
end
end
+ def test_add_index_length_limit
+ good_index_name = 'x' * Person.connection.index_name_length
+ too_long_index_name = good_index_name + 'x'
+ assert_nothing_raised { Person.connection.add_index("people", "first_name", :name => too_long_index_name) }
+ assert !Person.connection.index_exists?("people", too_long_index_name, false)
+ assert_nothing_raised { Person.connection.add_index("people", "first_name", :name => good_index_name) }
+ assert Person.connection.index_exists?("people", good_index_name, false)
+ end
+
+ def test_remove_nonexistent_index
+ # we do this by name, so OpenBase is a wash as noted above
+ unless current_adapter?(:OpenBaseAdapter)
+ assert_nothing_raised { Person.connection.remove_index("people", "no_such_index") }
+ end
+ end
+
+ def test_rename_index
+ unless current_adapter?(:OpenBaseAdapter)
+ # keep the names short to make Oracle and similar behave
+ Person.connection.add_index('people', [:first_name], :name => 'old_idx')
+ assert_nothing_raised { Person.connection.rename_index('people', 'old_idx', 'new_idx') }
+ # if the adapter doesn't support the indexes call, pick defaults that let the test pass
+ assert !Person.connection.index_exists?('people', 'old_idx', false)
+ assert Person.connection.index_exists?('people', 'new_idx', true)
+ end
+ end
+
+ def test_double_add_index
+ unless current_adapter?(:OpenBaseAdapter)
+ Person.connection.add_index('people', [:first_name], :name => 'some_idx')
+ assert_nothing_raised { Person.connection.add_index('people', [:first_name], :name => 'some_idx') }
+ end
+ end
+
def testing_table_with_only_foo_attribute
Person.connection.create_table :testings, :id => false do |t|
t.column :foo, :string
diff --git a/activerecord/test/cases/schema_test_postgresql.rb b/activerecord/test/cases/schema_test_postgresql.rb
index 3ed73786a7..3ed9b1974d 100644
--- a/activerecord/test/cases/schema_test_postgresql.rb
+++ b/activerecord/test/cases/schema_test_postgresql.rb
@@ -152,11 +152,11 @@ class SchemaTest < ActiveRecord::TestCase
def test_with_uppercase_index_name
ActiveRecord::Base.connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
- assert_nothing_raised { ActiveRecord::Base.connection.remove_index :things, :name => "#{SCHEMA_NAME}.things_Index"}
+ assert_nothing_raised { ActiveRecord::Base.connection.remove_index! "things", "#{SCHEMA_NAME}.things_Index"}
ActiveRecord::Base.connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
ActiveRecord::Base.connection.schema_search_path = SCHEMA_NAME
- assert_nothing_raised { ActiveRecord::Base.connection.remove_index :things, :name => "things_Index"}
+ assert_nothing_raised { ActiveRecord::Base.connection.remove_index! "things", "things_Index"}
ActiveRecord::Base.connection.schema_search_path = "public"
end