diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-10-28 07:40:28 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-10-28 07:40:28 +0000 |
commit | 816f37a2adde4449a17712cb0891a15f2c2ea7c0 (patch) | |
tree | 43f33e8edc56816d3ff04dc44f641af6d77d416c /activerecord/test | |
parent | cb0e27cee90c9417798c9d9db78dd0eb36c7fa24 (diff) | |
download | rails-816f37a2adde4449a17712cb0891a15f2c2ea7c0.tar.gz rails-816f37a2adde4449a17712cb0891a15f2c2ea7c0.tar.bz2 rails-816f37a2adde4449a17712cb0891a15f2c2ea7c0.zip |
Added migration support to SQL Server adapter (please someone do the same for Oracle and DB2) (closes #2625) [Tom Ward]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2778 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-x | activerecord/test/base_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/migration_test.rb | 70 |
2 files changed, 51 insertions, 25 deletions
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 5d924f540a..0b8c08a27e 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -16,6 +16,7 @@ class CreditCard < ActiveRecord::Base; end class MasterCreditCard < ActiveRecord::Base; end class Post < ActiveRecord::Base; end class Computer < ActiveRecord::Base; end +class NonExistentTable < ActiveRecord::Base; end class LoosePerson < ActiveRecord::Base attr_protected :credit_rating, :administrator @@ -42,6 +43,11 @@ end class BasicsTest < Test::Unit::TestCase fixtures :topics, :companies, :developers, :projects, :computers + def test_table_exists + assert !NonExistentTable.table_exists? + assert Topic.table_exists? + end + def test_set_attributes topic = Topic.find(1) topic.attributes = { "title" => "Budget", "author_name" => "Jason" } diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index 4d10767991..be1dfef7b0 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -4,7 +4,6 @@ require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names' require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders' if ActiveRecord::Base.connection.supports_migrations? - class Reminder < ActiveRecord::Base; end class MigrationTest < Test::Unit::TestCase @@ -19,6 +18,7 @@ if ActiveRecord::Base.connection.supports_migrations? Reminder.connection.drop_table("reminders") rescue nil Reminder.connection.drop_table("people_reminders") rescue nil + Reminder.connection.drop_table("prefix_reminders_suffix") rescue nil Reminder.reset_column_information Person.connection.remove_column("people", "last_name") rescue nil @@ -92,11 +92,29 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.drop_table :testings rescue nil end - def test_add_column_not_null + # SQL Server will not allow you to add a NOT NULL column + # to a table without specifying a default value, so the + # following test must be skipped + unless current_adapter?(:SQLServerAdapter) + def test_add_column_not_null_without_default + Person.connection.create_table :testings do |t| + t.column :foo, :string + end + Person.connection.add_column :testings, :bar, :string, :null => false + + assert_raises(ActiveRecord::StatementInvalid) do + Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)" + end + ensure + Person.connection.drop_table :testings rescue nil + end + end + + def test_add_column_not_null_with_default Person.connection.create_table :testings do |t| t.column :foo, :string end - Person.connection.add_column :testings, :bar, :string, :null => false + Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" assert_raises(ActiveRecord::StatementInvalid) do Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)" @@ -128,7 +146,14 @@ if ActiveRecord::Base.connection.supports_migrations? assert_equal String, bob.bio.class assert_equal Fixnum, bob.age.class assert_equal Time, bob.birthday.class - assert_equal Date, bob.favorite_day.class + + if current_adapter?(:SQLServerAdapter) + # SQL Server doesn't differentiate between date/time + assert_equal Time, bob.favorite_day.class + else + assert_equal Date, bob.favorite_day.class + end + assert_equal TrueClass, bob.male?.class end @@ -162,11 +187,11 @@ if ActiveRecord::Base.connection.supports_migrations? def test_add_rename Person.delete_all - - Person.connection.add_column "people", "girlfriend", :string - Person.create :girlfriend => 'bobette' - + begin + Person.connection.add_column "people", "girlfriend", :string + Person.create :girlfriend => 'bobette' + Person.connection.rename_column "people", "girlfriend", "exgirlfriend" Person.reset_column_information @@ -237,8 +262,8 @@ if ActiveRecord::Base.connection.supports_migrations? end def test_add_table - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } - + assert !Reminder.table_exists? + WeNeedReminders.up assert Reminder.create("content" => "hello world", "remind_at" => Time.now) @@ -250,7 +275,7 @@ if ActiveRecord::Base.connection.supports_migrations? def test_migrator assert !Person.column_methods_hash.include?(:last_name) - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } + assert !Reminder.table_exists? ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') @@ -270,14 +295,13 @@ if ActiveRecord::Base.connection.supports_migrations? def test_migrator_one_up assert !Person.column_methods_hash.include?(:last_name) - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } - + assert !Reminder.table_exists? + ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1) Person.reset_column_information assert Person.column_methods_hash.include?(:last_name) - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } - + assert !Reminder.table_exists? ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2) @@ -292,7 +316,7 @@ if ActiveRecord::Base.connection.supports_migrations? Person.reset_column_information assert Person.column_methods_hash.include?(:last_name) - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } + assert !Reminder.table_exists? end def test_migrator_one_up_one_down @@ -300,7 +324,7 @@ if ActiveRecord::Base.connection.supports_migrations? ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0) assert !Person.column_methods_hash.include?(:last_name) - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } + assert !Reminder.table_exists? end def test_migrator_going_down_due_to_version_target @@ -308,7 +332,7 @@ if ActiveRecord::Base.connection.supports_migrations? ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/', 0) assert !Person.column_methods_hash.include?(:last_name) - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } + assert !Reminder.table_exists? ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/') @@ -359,13 +383,11 @@ if ActiveRecord::Base.connection.supports_migrations? end def test_add_drop_table_with_prefix_and_suffix - assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } - + assert !Reminder.table_exists? ActiveRecord::Base.table_name_prefix = 'prefix_' ActiveRecord::Base.table_name_suffix = '_suffix' Reminder.reset_table_name WeNeedReminders.up - assert Reminder.create("content" => "hello world", "remind_at" => Time.now) assert_equal "hello world", Reminder.find(:first).content @@ -375,7 +397,5 @@ if ActiveRecord::Base.connection.supports_migrations? ActiveRecord::Base.table_name_suffix = '' Reminder.reset_table_name end - - end - -end + end +end
\ No newline at end of file |