mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Added preliminary support for an agile database migration technique (currently only for MySQL)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@818 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -45,13 +45,13 @@ class CreateTablesTest < Test::Unit::TestCase
|
||||
|
||||
def test_table_creation
|
||||
adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
|
||||
run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".drop.sql"
|
||||
run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".sql"
|
||||
run_sql_file ActiveRecord::Base.connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + ".drop.sql"
|
||||
run_sql_file ActiveRecord::Base.connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + ".sql"
|
||||
|
||||
# Now do the same thing with the connection used by multiple_db_test.rb
|
||||
adapter_name = Course.retrieve_connection.adapter_name.downcase
|
||||
run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.drop.sql"
|
||||
run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.sql"
|
||||
run_sql_file Course.retrieve_connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + "2.drop.sql"
|
||||
run_sql_file Course.retrieve_connection, "#{File.dirname(__FILE__)}/fixtures/db_definitions/" + adapter_name + "2.sql"
|
||||
|
||||
assert_equal 1,1
|
||||
end
|
||||
|
||||
9
activerecord/test/fixtures/migrations/1_people_have_last_names.rb
vendored
Normal file
9
activerecord/test/fixtures/migrations/1_people_have_last_names.rb
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
class PeopleHaveLastNames < ActiveRecord::Migration
|
||||
def self.up
|
||||
add_column "people", "last_name", :string
|
||||
end
|
||||
|
||||
def self.down
|
||||
remove_column "people", "last_name"
|
||||
end
|
||||
end
|
||||
12
activerecord/test/fixtures/migrations/2_we_need_reminders.rb
vendored
Normal file
12
activerecord/test/fixtures/migrations/2_we_need_reminders.rb
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
class WeNeedReminders < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table("reminders") do |t|
|
||||
t.column :content, :text
|
||||
t.column :remind_at, :datetime
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table "reminders"
|
||||
end
|
||||
end
|
||||
104
activerecord/test/migration_mysql.rb
Normal file
104
activerecord/test/migration_mysql.rb
Normal file
@@ -0,0 +1,104 @@
|
||||
require 'abstract_unit'
|
||||
require 'fixtures/person'
|
||||
require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names'
|
||||
require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders'
|
||||
|
||||
class Reminder < ActiveRecord::Base; end
|
||||
|
||||
class MigrationTest < Test::Unit::TestCase
|
||||
def setup
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActiveRecord::Base.connection.initialize_schema_information
|
||||
ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0"
|
||||
|
||||
Reminder.connection.drop_table("reminders") rescue nil
|
||||
Reminder.reset_column_information
|
||||
|
||||
Person.connection.remove_column("people", "last_name") rescue nil
|
||||
Person.reset_column_information
|
||||
end
|
||||
|
||||
def test_add_remove_single_field
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
PeopleHaveLastNames.up
|
||||
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
|
||||
PeopleHaveLastNames.down
|
||||
|
||||
Person.reset_column_information
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
end
|
||||
|
||||
def test_add_table
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
|
||||
WeNeedReminders.up
|
||||
|
||||
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
|
||||
assert "hello world", Reminder.find_first
|
||||
|
||||
WeNeedReminders.down
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.find_first }
|
||||
end
|
||||
|
||||
def test_migrator
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
|
||||
|
||||
assert_equal 2, ActiveRecord::Migrator.current_version
|
||||
Person.reset_column_information
|
||||
assert Person.column_methods_hash.include?(:last_name)
|
||||
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
|
||||
assert "hello world", Reminder.find_first
|
||||
|
||||
|
||||
ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/')
|
||||
|
||||
assert_equal 0, ActiveRecord::Migrator.current_version
|
||||
Person.reset_column_information
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.find_first }
|
||||
end
|
||||
|
||||
def test_migrator_one_up
|
||||
assert !Person.column_methods_hash.include?(:last_name)
|
||||
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
|
||||
|
||||
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 }
|
||||
|
||||
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2)
|
||||
|
||||
assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
|
||||
assert "hello world", Reminder.find_first
|
||||
end
|
||||
|
||||
def test_migrator_one_down
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
|
||||
|
||||
ActiveRecord::Migrator.down(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 }
|
||||
end
|
||||
|
||||
def test_migrator_one_up_one_down
|
||||
ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
|
||||
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 }
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user