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

Changeset 9014

Show
Ignore:
Timestamp:
03/13/08 02:17:04 (2 months ago)
Author:
david
Message:

Added add/remove_timestamps to the schema statements for adding the created_at/updated_at columns on existing tables (closes #11129) [jramirez]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r9012 r9014  
    11*SVN* 
     2 
     3* Added add/remove_timestamps to the schema statements for adding the created_at/updated_at columns on existing tables #11129 [jramirez] 
    24 
    35* Added ActiveRecord::Base.find(:last) #11338 [miloops] 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    r8439 r9014  
    299299      end 
    300300 
     301      # Adds timestamps (created_at and updated_at) columns to the named table. 
     302      # ===== Examples 
     303      #  add_timestamps(:suppliers) 
     304      def add_timestamps(table_name) 
     305        add_column table_name, :created_at, :datetime 
     306        add_column table_name, :updated_at, :datetime     
     307      end 
     308       
     309      # Removes the timestamp columns (created_at and updated_at) from the table definition. 
     310      # ===== Examples 
     311      #  remove_timestamps(:suppliers) 
     312      def remove_timestamps(table_name) 
     313        remove_column table_name, :updated_at    
     314        remove_column table_name, :created_at        
     315      end 
     316       
    301317      protected 
    302318        def options_include_default?(options) 
  • trunk/activerecord/test/cases/active_schema_test_mysql.rb

    r8681 r9014  
    4040  end 
    4141 
     42  def test_add_timestamps  
     43    #we need to actually modify some data, so we make execute to point to the original method 
     44    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do       
     45      alias_method :execute_with_stub, :execute 
     46      alias_method :execute, :execute_without_stub 
     47    end   
     48    ActiveRecord::Base.connection.create_table :delete_me do |t|         
     49    end 
     50    ActiveRecord::Base.connection.add_timestamps :delete_me 
     51    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='updated_at' AND TYPE='datetime'").num_rows, 1 
     52    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='created_at' AND TYPE='datetime'").num_rows, 1 
     53  ensure     
     54    ActiveRecord::Base.connection.drop_table :delete_me rescue nil   
     55    #before finishing, we restore the alias to the mock-up method 
     56    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do       
     57      alias_method :execute, :execute_with_stub 
     58    end 
     59  end 
     60   
     61  def test_remove_timestamps  
     62    #we need to actually modify some data, so we make execute to point to the original method 
     63    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do       
     64      alias_method :execute_with_stub, :execute 
     65      alias_method :execute, :execute_without_stub 
     66    end   
     67    ActiveRecord::Base.connection.create_table :delete_me do |t|         
     68      t.timestamps 
     69    end 
     70    ActiveRecord::Base.connection.remove_timestamps :delete_me 
     71    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='updated_at' AND TYPE='datetime'").num_rows, 0 
     72    assert_equal ActiveRecord::Base.connection.execute("SHOW FIELDS FROM delete_me where FIELD='created_at' AND TYPE='datetime'").num_rows, 0 
     73  ensure     
     74    ActiveRecord::Base.connection.drop_table :delete_me rescue nil   
     75    #before finishing, we restore the alias to the mock-up method 
     76    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do       
     77      alias_method :execute, :execute_with_stub 
     78    end 
     79  end 
     80 
     81 
    4282  private 
    4383    def method_missing(method_symbol, *arguments)