Ticket #1604: 1604_postgres_migrations_2.patch
| File 1604_postgres_migrations_2.patch, 19.1 kB (added by tobi, 3 years ago) |
|---|
-
activerecord/test/migration_mysql.rb
old new 1 require 'abstract_unit'2 require 'fixtures/person'3 require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names'4 require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders'5 6 class Reminder < ActiveRecord::Base; end7 8 class MigrationTest < Test::Unit::TestCase9 def setup10 end11 12 def teardown13 ActiveRecord::Base.connection.initialize_schema_information14 ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0"15 16 Reminder.connection.drop_table("reminders") rescue nil17 Reminder.reset_column_information18 19 Person.connection.remove_column("people", "last_name") rescue nil20 Person.reset_column_information21 end22 23 def test_add_remove_single_field24 assert !Person.column_methods_hash.include?(:last_name)25 26 PeopleHaveLastNames.up27 28 Person.reset_column_information29 assert Person.column_methods_hash.include?(:last_name)30 31 PeopleHaveLastNames.down32 33 Person.reset_column_information34 assert !Person.column_methods_hash.include?(:last_name)35 end36 37 def test_add_table38 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }39 40 WeNeedReminders.up41 42 assert Reminder.create("content" => "hello world", "remind_at" => Time.now)43 assert "hello world", Reminder.find(:first)44 45 WeNeedReminders.down46 assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }47 end48 49 def test_migrator50 assert !Person.column_methods_hash.include?(:last_name)51 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }52 53 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')54 55 assert_equal 2, ActiveRecord::Migrator.current_version56 Person.reset_column_information57 assert Person.column_methods_hash.include?(:last_name)58 assert Reminder.create("content" => "hello world", "remind_at" => Time.now)59 assert "hello world", Reminder.find(:first)60 61 62 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/')63 64 assert_equal 0, ActiveRecord::Migrator.current_version65 Person.reset_column_information66 assert !Person.column_methods_hash.include?(:last_name)67 assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }68 end69 70 def test_migrator_one_up71 assert !Person.column_methods_hash.include?(:last_name)72 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }73 74 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)75 76 Person.reset_column_information77 assert Person.column_methods_hash.include?(:last_name)78 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }79 80 81 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2)82 83 assert Reminder.create("content" => "hello world", "remind_at" => Time.now)84 assert "hello world", Reminder.find(:first)85 end86 87 def test_migrator_one_down88 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')89 90 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1)91 92 Person.reset_column_information93 assert Person.column_methods_hash.include?(:last_name)94 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }95 end96 97 def test_migrator_one_up_one_down98 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)99 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)100 101 assert !Person.column_methods_hash.include?(:last_name)102 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }103 end104 end -
activerecord/test/associations_go_eager_test.rb
old new 11 11 12 12 def test_loading_with_one_association 13 13 posts = Post.find(:all, :include => :comments) 14 assert_equal 2, posts.first.comments.size 15 assert posts.first.comments.include?(comments(:greetings)) 14 post = posts.find { |p| p.id == 1 } 15 assert_equal 2, post.comments.size 16 assert post.comments.include?(comments(:greetings)) 16 17 17 18 post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'") 18 19 assert_equal 2, post.comments.size -
activerecord/test/migration_test.rb
old new 3 3 require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names' 4 4 require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders' 5 5 6 class Reminder < ActiveRecord::Base; end 6 if ActiveRecord::Base.connection.supports_migrations? 7 7 8 class MigrationTest < Test::Unit::TestCase 9 def setup 10 end 8 class Reminder < ActiveRecord::Base; end 11 9 12 def teardown13 ActiveRecord::Base.connection.initialize_schema_information14 ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0"10 class MigrationTest < Test::Unit::TestCase 11 def setup 12 end 15 13 16 Reminder.connection.drop_table("reminders") rescue nil 17 Reminder.reset_column_information 14 def teardown 15 ActiveRecord::Base.connection.initialize_schema_information 16 ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" 18 17 19 Person.connection.remove_column("people", "last_name") rescue nil 20 Person.reset_column_information 21 end 18 Reminder.connection.drop_table("reminders") rescue nil 19 Reminder.reset_column_information 22 20 23 def test_add_remove_single_field 24 assert !Person.column_methods_hash.include?(:last_name) 21 Person.connection.remove_column("people", "last_name") rescue nil 22 Person.connection.remove_column("people", "bio") rescue nil 23 Person.connection.remove_column("people", "age") rescue nil 24 Person.connection.remove_column("people", "height") rescue nil 25 Person.connection.remove_column("people", "birthday") rescue nil 26 Person.connection.remove_column("people", "favorite_day") rescue nil 27 Person.connection.remove_column("people", "male") rescue nil 28 Person.reset_column_information 29 end 30 31 def test_native_types 32 33 Person.delete_all 34 Person.connection.add_column "people", "last_name", :string 35 Person.connection.add_column "people", "bio", :text 36 Person.connection.add_column "people", "age", :integer 37 Person.connection.add_column "people", "height", :float 38 Person.connection.add_column "people", "birthday", :datetime 39 Person.connection.add_column "people", "favorite_day", :date 40 Person.connection.add_column "people", "male", :boolean 41 assert_nothing_raised { Person.create :first_name => 'bob', :last_name => 'bobsen', :bio => "I was born ....", :age => 18, :height => 1.78, :birthday => 18.years.ago, :favorite_day => 10.days.ago, :male => true } 42 bob = Person.find(:first) 43 44 assert_equal bob.first_name, 'bob' 45 assert_equal bob.last_name, 'bobsen' 46 assert_equal bob.bio, "I was born ...." 47 assert_equal bob.age, 18 48 assert_equal bob.male?, true 49 50 assert_equal String, bob.first_name.class 51 assert_equal String, bob.last_name.class 52 assert_equal String, bob.bio.class 53 assert_equal Fixnum, bob.age.class 54 assert_equal Time, bob.birthday.class 55 assert_equal Date, bob.favorite_day.class 56 assert_equal TrueClass, bob.male?.class 57 end 25 58 26 PeopleHaveLastNames.up 59 def test_add_remove_single_field 60 assert !Person.column_methods_hash.include?(:last_name) 27 61 28 Person.reset_column_information 29 assert Person.column_methods_hash.include?(:last_name) 62 PeopleHaveLastNames.up 63 64 Person.reset_column_information 65 assert Person.column_methods_hash.include?(:last_name) 30 66 31 PeopleHaveLastNames.down67 PeopleHaveLastNames.down 32 68 33 Person.reset_column_information34 assert !Person.column_methods_hash.include?(:last_name)35 end69 Person.reset_column_information 70 assert !Person.column_methods_hash.include?(:last_name) 71 end 36 72 37 def test_add_table38 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }73 def test_add_table 74 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 39 75 40 WeNeedReminders.up76 WeNeedReminders.up 41 77 42 assert Reminder.create("content" => "hello world", "remind_at" => Time.now)43 assert "hello world", Reminder.find(:first)78 assert Reminder.create("content" => "hello world", "remind_at" => Time.now) 79 assert_equal "hello world", Reminder.find(:first).content 44 80 45 WeNeedReminders.down46 assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }47 end81 WeNeedReminders.down 82 assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) } 83 end 48 84 49 def test_migrator50 assert !Person.column_methods_hash.include?(:last_name)51 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }85 def test_migrator 86 assert !Person.column_methods_hash.include?(:last_name) 87 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 52 88 53 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')89 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') 54 90 55 assert_equal 2, ActiveRecord::Migrator.current_version56 Person.reset_column_information57 assert Person.column_methods_hash.include?(:last_name)58 assert Reminder.create("content" => "hello world", "remind_at" => Time.now)59 assert "hello world", Reminder.find(:first)91 assert_equal 2, ActiveRecord::Migrator.current_version 92 Person.reset_column_information 93 assert Person.column_methods_hash.include?(:last_name) 94 assert Reminder.create("content" => "hello world", "remind_at" => Time.now) 95 assert_equal "hello world", Reminder.find(:first).content 60 96 97 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/') 61 98 62 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/') 99 assert_equal 0, ActiveRecord::Migrator.current_version 100 Person.reset_column_information 101 assert !Person.column_methods_hash.include?(:last_name) 102 assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) } 103 end 63 104 64 assert_equal 0, ActiveRecord::Migrator.current_version 65 Person.reset_column_information 66 assert !Person.column_methods_hash.include?(:last_name) 67 assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) } 68 end 105 def test_migrator_one_up 106 assert !Person.column_methods_hash.include?(:last_name) 107 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 69 108 70 def test_migrator_one_up 71 assert !Person.column_methods_hash.include?(:last_name) 72 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 109 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1) 73 110 74 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1) 111 Person.reset_column_information 112 assert Person.column_methods_hash.include?(:last_name) 113 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 75 114 76 Person.reset_column_information77 assert Person.column_methods_hash.include?(:last_name)78 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }79 115 116 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2) 80 117 81 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2) 82 83 assert Reminder.create("content" => "hello world", "remind_at" => Time.now) 84 assert "hello world", Reminder.find(:first) 85 end 118 assert Reminder.create("content" => "hello world", "remind_at" => Time.now) 119 assert_equal "hello world", Reminder.find(:first).content 120 end 86 121 87 def test_migrator_one_down88 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')122 def test_migrator_one_down 123 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') 89 124 90 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1)125 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1) 91 126 92 Person.reset_column_information93 assert Person.column_methods_hash.include?(:last_name)94 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }95 end127 Person.reset_column_information 128 assert Person.column_methods_hash.include?(:last_name) 129 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 130 end 96 131 97 def test_migrator_one_up_one_down98 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)99 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)132 def test_migrator_one_up_one_down 133 ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1) 134 ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0) 100 135 101 assert !Person.column_methods_hash.include?(:last_name) 102 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 136 assert !Person.column_methods_hash.include?(:last_name) 137 assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 138 end 103 139 end 104 end 140 end -
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
old new 359 359 360 360 def initialize_schema_information 361 361 begin 362 execute "CREATE TABLE schema_info (version #{ native_database_types[:integer][:name]}(#{native_database_types[:integer][:limit]}))"362 execute "CREATE TABLE schema_info (version #{type_to_sql(:integer)})" 363 363 insert "INSERT INTO schema_info (version) VALUES(0)" 364 364 rescue ActiveRecord::StatementInvalid 365 365 # Schema has been intialized … … 378 378 379 379 def add_column(table_name, column_name, type, options = {}) 380 380 native_type = native_database_types[type] 381 add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{native_type[:name]}" 382 add_column_sql << "(#{options[:limit] || native_type[:limit]})" if options[:limit] || native_type[:limit] 381 add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type)}" 383 382 add_column_sql << " DEFAULT '#{options[:default]}'" if options[:default] 384 383 execute(add_column_sql) 385 384 end … … 387 386 def remove_column(table_name, column_name) 388 387 execute "ALTER TABLE #{table_name} DROP #{column_name}" 389 388 end 389 390 def supports_migrations? 391 false 392 end 390 393 391 392 394 protected 395 396 def type_to_sql(type) 397 native = native_database_types[type] 398 column_type_sql = native[:name] 399 column_type_sql << "(#{native[:limit]})" if native[:limit] 400 column_type_sql 401 end 402 393 403 def log(sql, name) 394 404 begin 395 405 if block_given? … … 439 449 "%s %s" % [message, dump] 440 450 end 441 451 end 442 end452 end 443 453 444 454 class TableDefinition 445 455 attr_accessor :columns -
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
old new 60 60 # * <tt>:encoding</tt> -- An optional client encoding that is using in a SET client_encoding TO <encoding> call on connection. 61 61 # * <tt>:min_messages</tt> -- An optional client min messages that is using in a SET client_min_messages TO <min_messages> call on connection. 62 62 class PostgreSQLAdapter < AbstractAdapter 63 64 def native_database_types 65 { 66 :primary_key => "serial primary key", 67 :string => { :name => "character varying", :limit => 255 }, 68 :text => { :name => "text" }, 69 :integer => { :name => "integer" }, 70 :float => { :name => "float" }, 71 :datetime => { :name => "timestamp" }, 72 :timestamp => { :name => "timestamp" }, 73 :time => { :name => "timestamp" }, 74 :date => { :name => "date" }, 75 :binary => { :name => "bytea" }, 76 :boolean => { :name => "boolean"} 77 } 78 end 79 80 def supports_migrations? 81 true 82 end 83 63 84 def select_all(sql, name = nil) 64 85 select(sql, name) 65 86 end -
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
old new 63 63 "Lost connection to MySQL server during query", 64 64 "MySQL server has gone away" 65 65 ] 66 67 def supports_migrations? 68 true 69 end 66 70 67 71 def native_database_types 68 72 { … … 89 93 'MySQL' 90 94 end 91 95 92 93 96 def select_all(sql, name = nil) 94 97 select(sql, name) 95 98 end -
activerecord/lib/active_record/migration.rb
old new 17 17 class Migrator#:nodoc: 18 18 class << self 19 19 def up(migrations_path, target_version = nil) 20 new(:up, migrations_path, target_version).migrate20 self.new(:up, migrations_path, target_version).migrate 21 21 end 22 22 23 23 def down(migrations_path, target_version = nil) 24 new(:down, migrations_path, target_version).migrate24 self.new(:down, migrations_path, target_version).migrate 25 25 end 26 26 27 27 def current_version … … 30 30 end 31 31 32 32 def initialize(direction, migrations_path, target_version = nil) 33 raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations? 33 34 @direction, @migrations_path, @target_version = direction, migrations_path, target_version 34 35 Base.connection.initialize_schema_information 35 36 end