Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1604: 1604_postgres_migrations.patch

File 1604_postgres_migrations.patch, 18.2 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; end 
    7  
    8 class MigrationTest < Test::Unit::TestCase 
    9   def setup 
    10   end 
    11  
    12   def teardown 
    13     ActiveRecord::Base.connection.initialize_schema_information 
    14     ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" 
    15  
    16     Reminder.connection.drop_table("reminders") rescue nil 
    17     Reminder.reset_column_information 
    18  
    19     Person.connection.remove_column("people", "last_name") rescue nil 
    20     Person.reset_column_information 
    21   end 
    22  
    23   def test_add_remove_single_field 
    24     assert !Person.column_methods_hash.include?(:last_name) 
    25  
    26     PeopleHaveLastNames.up 
    27  
    28     Person.reset_column_information 
    29     assert Person.column_methods_hash.include?(:last_name) 
    30      
    31     PeopleHaveLastNames.down 
    32  
    33     Person.reset_column_information 
    34     assert !Person.column_methods_hash.include?(:last_name) 
    35   end 
    36  
    37   def test_add_table 
    38     assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 
    39      
    40     WeNeedReminders.up 
    41      
    42     assert Reminder.create("content" => "hello world", "remind_at" => Time.now) 
    43     assert "hello world", Reminder.find(:first) 
    44      
    45     WeNeedReminders.down 
    46     assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) } 
    47   end 
    48  
    49   def test_migrator 
    50     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_version 
    56     Person.reset_column_information 
    57     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_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 
    69  
    70   def test_migrator_one_up 
    71     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_information 
    77     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   end 
    86    
    87   def test_migrator_one_down 
    88     ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') 
    89      
    90     ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1) 
    91  
    92     Person.reset_column_information 
    93     assert Person.column_methods_hash.include?(:last_name) 
    94     assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 
    95   end 
    96    
    97   def test_migrator_one_up_one_down 
    98     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   end 
    104 end 
  • activerecord/test/associations_go_eager_test.rb

    old new  
    1111 
    1212  def test_loading_with_one_association 
    1313    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)) 
    1617 
    1718    post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'") 
    1819    assert_equal 2, post.comments.size 
  • activerecord/test/migration_test.rb

    old new  
    33require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names' 
    44require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders' 
    55 
    6 class Reminder < ActiveRecord::Base; end 
     6if ActiveRecord::Base.connection.supports_migrations?  
    77 
    8 class MigrationTest < Test::Unit::TestCase 
    9   def setup 
    10   end 
     8  class Reminder < ActiveRecord::Base; end 
    119 
    12   def teardown 
    13     ActiveRecord::Base.connection.initialize_schema_information 
    14     ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" 
     10  class MigrationTest < Test::Unit::TestCase 
     11    def setup 
     12    end 
    1513 
    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" 
    1817 
    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 
    2220 
    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 
    2558 
    26     PeopleHaveLastNames.up 
     59    def test_add_remove_single_field 
     60      assert !Person.column_methods_hash.include?(:last_name) 
    2761 
    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) 
    3066     
    31     PeopleHaveLastNames.down 
     67      PeopleHaveLastNames.down 
    3268 
    33     Person.reset_column_information 
    34     assert !Person.column_methods_hash.include?(:last_name) 
    35   end 
     69      Person.reset_column_information 
     70      assert !Person.column_methods_hash.include?(:last_name) 
     71    end 
    3672 
    37   def test_add_table 
    38     assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 
     73    def test_add_table 
     74      assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 
    3975     
    40     WeNeedReminders.up 
     76      WeNeedReminders.up 
    4177     
    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 
    4480     
    45     WeNeedReminders.down 
    46     assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) } 
    47   end 
     81      WeNeedReminders.down 
     82      assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) } 
     83    end 
    4884 
    49   def test_migrator 
    50     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 } 
    5288 
    53     ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') 
     89      ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') 
    5490 
    55     assert_equal 2, ActiveRecord::Migrator.current_version 
    56     Person.reset_column_information 
    57     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 
    6096 
     97      ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/') 
    6198 
    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 
    63104 
    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 } 
    69108 
    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) 
    73110 
    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 } 
    75114 
    76     Person.reset_column_information 
    77     assert Person.column_methods_hash.include?(:last_name) 
    78     assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 
    79115 
     116      ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2) 
    80117 
    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 
    86121   
    87   def test_migrator_one_down 
    88     ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') 
     122    def test_migrator_one_down 
     123      ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') 
    89124     
    90     ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1) 
     125      ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1) 
    91126 
    92     Person.reset_column_information 
    93     assert Person.column_methods_hash.include?(:last_name) 
    94     assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 
    95   end 
     127      Person.reset_column_information 
     128      assert Person.column_methods_hash.include?(:last_name) 
     129      assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash } 
     130    end 
    96131   
    97   def test_migrator_one_up_one_down 
    98     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) 
    100135 
    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 
    103139  end 
    104 end 
     140end 
  • activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    old new  
    359359 
    360360      def initialize_schema_information 
    361361        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 #{native_database_types[:integer][:name]}#{native_database_types[:integer][:limit]})" 
    363363          insert "INSERT INTO schema_info (version) VALUES(0)" 
    364364        rescue ActiveRecord::StatementInvalid 
    365365          # Schema has been intialized 
     
    387387      def remove_column(table_name, column_name) 
    388388        execute "ALTER TABLE #{table_name} DROP #{column_name}" 
    389389      end 
     390       
     391      def supports_migrations? 
     392        false 
     393      end       
    390394 
    391  
    392395      protected 
     396               
    393397        def log(sql, name) 
    394398          begin 
    395399            if block_given? 
  • activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

    old new  
    6060    # * <tt>:encoding</tt> -- An optional client encoding that is using in a SET client_encoding TO <encoding> call on connection. 
    6161    # * <tt>:min_messages</tt> -- An optional client min messages that is using in a SET client_min_messages TO <min_messages> call on connection. 
    6262    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       
    6384      def select_all(sql, name = nil) 
    6485        select(sql, name) 
    6586      end 
  • activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

    old new  
    6363        "Lost connection to MySQL server during query", 
    6464        "MySQL server has gone away" 
    6565      ] 
     66       
     67      def supports_migrations? 
     68        true 
     69      end 
    6670 
    6771      def native_database_types 
    6872        { 
     
    8993        'MySQL' 
    9094      end 
    9195 
    92  
    9396      def select_all(sql, name = nil) 
    9497        select(sql, name) 
    9598      end 
  • activerecord/lib/active_record/migration.rb

    old new  
    1717  class Migrator#:nodoc: 
    1818    class << self 
    1919      def up(migrations_path, target_version = nil) 
    20         new(:up, migrations_path, target_version).migrate 
     20        self.new(:up, migrations_path, target_version).migrate 
    2121      end 
    2222       
    2323      def down(migrations_path, target_version = nil) 
    24         new(:down, migrations_path, target_version).migrate 
     24        self.new(:down, migrations_path, target_version).migrate 
    2525      end 
    2626       
    2727      def current_version 
     
    3030    end 
    3131     
    3232    def initialize(direction, migrations_path, target_version = nil) 
     33      raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations? 
    3334      @direction, @migrations_path, @target_version = direction, migrations_path, target_version 
    3435      Base.connection.initialize_schema_information 
    3536    end