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

Ticket #5940: readonly_timestamp_columns_for_sqlserver.diff

File readonly_timestamp_columns_for_sqlserver.diff, 6.0 kB (added by tomafro, 1 year ago)

Experimental timestamp patch, for comment

  • activerecord/test/adapter_test_sqlserver.rb

    old new  
    44require 'fixtures/task' 
    55 
    66class SqlServerAdapterTest < Test::Unit::TestCase 
    7   class TableWithRealColumn < ActiveRecord::Base; end 
     7  class TableWithSqlserverSpecificColumn < ActiveRecord::Base; end 
    88 
    99  fixtures :posts, :tasks 
    1010 
     
    1717  end 
    1818 
    1919  def test_real_column_has_float_type 
    20     assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type 
     20    assert_equal :float, TableWithSqlserverSpecificColumn.columns_hash["real_number"].type 
    2121  end 
     22   
     23  def test_timestamp_column_has_timestamp_type 
     24    assert_equal :timestamp, TableWithSqlserverSpecificColumn.columns_hash["timestamp"].type 
     25  end 
    2226 
     27  def test_create_record_with_timestamp_column 
     28    record = TableWithSqlserverSpecificColumn.create(:real_number => 1.2345) 
     29    assert_nil record.timestamp 
     30    loaded = TableWithSqlserverSpecificColumn.find(record.id) 
     31    assert_not_nil loaded.timestamp 
     32  end 
     33   
     34  def test_update_record_with_timestamp_column 
     35    record = TableWithSqlserverSpecificColumn.create!(:real_number => 1.2345) 
     36    loaded = TableWithSqlserverSpecificColumn.find(record.id) 
     37    loaded.timestamp = "rubbish" 
     38    loaded.real_number = 2.345 
     39    loaded.save! 
     40    loaded.reload 
     41    assert_not_nil loaded.timestamp 
     42    assert_not_equal "rubbish", loaded.timestamp 
     43  end 
     44 
    2345  # SQL Server 2000 has a bug where some unambiguous date formats are not  
    2446  # correctly identified if the session language is set to german 
    2547  def test_date_insertion_when_language_is_german 
     
    6890    assert_all_statements_used_are_closed do 
    6991      @connection.active? 
    7092    end 
    71   end  
     93  end 
    7294 
    7395  def assert_all_statements_used_are_closed(&block) 
    7496    existing_handles = [] 
  • activerecord/test/fixtures/db_definitions/schema.rb

    old new  
    7171   
    7272  # For sqlserver 2000+, ensure real columns can be used 
    7373  if adapter_name.starts_with?("SQLServer") 
    74     create_table :table_with_real_columns, :force => true do |t| 
     74    create_table :table_with_sqlserver_specific_columns, :force => true do |t| 
    7575      t.column :real_number, :real 
     76      t.column :timestamp, :timestamp 
    7677    end 
    7778  end 
    7879end 
  • activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    old new  
    2323 
    2424        @primary = nil 
    2525      end 
     26       
     27      # Is this column read-only?  Some databases (such as SQL Server) provide 
     28      # read only data types.  This method can be overridden by subclasses 
     29      # to indicate the column is readonly 
     30       
     31      def readonly? 
     32        false 
     33      end 
    2634 
    2735      def text? 
    2836        [:string, :text].include? type 
  • activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb

    old new  
    7272          else super 
    7373        end 
    7474      end 
     75       
     76      def readonly? 
     77        case type 
     78          when :timestamp then true 
     79          else super 
     80        end 
     81      end 
    7582 
     83      def type_cast_code(var_name) 
     84        case type 
     85          when :timestamp then nil 
     86          else super 
     87        end 
     88      end 
     89 
    7690      def type_cast(value) 
    7791        return nil if value.nil? 
    7892        case type 
    7993        when :datetime  then cast_to_datetime(value) 
    80         when :timestamp then cast_to_time(value) 
     94        when :timestamp then value 
    8195        when :time      then cast_to_time(value) 
    8296        when :date      then cast_to_datetime(value) 
    8397        when :boolean   then value == true or (value =~ /^t(rue)?$/i) == 0 or value.to_s == '1' 
     
    110124        value 
    111125      end 
    112126       
     127      def self.string_to_timestamp(value) 
     128        value 
     129      end 
     130       
    113131      # TODO: Find less hack way to convert DateTime objects into Times 
    114132       
    115133      def self.string_to_time(value) 
     
    203221          :float       => { :name => "float", :limit => 8 }, 
    204222          :decimal     => { :name => "decimal" }, 
    205223          :datetime    => { :name => "datetime" }, 
    206           :timestamp   => { :name => "datetime" }, 
     224          :timestamp   => { :name => "timestamp" }, 
    207225          :time        => { :name => "datetime" }, 
    208226          :date        => { :name => "datetime" }, 
    209227          :binary      => { :name => "image"}, 
     
    600618          end 
    601619          sql 
    602620        end 
     621         
     622        def options_include_default?(options) 
     623          options[:column].type == :timestamp ? false : super 
     624        end 
    603625 
    604626    end #class SQLServerAdapter < AbstractAdapter 
    605627  end #module ConnectionAdapters 
  • activerecord/lib/active_record/base.rb

    old new  
    22022202      # an SQL statement. 
    22032203      def attributes_with_quotes(include_primary_key = true) 
    22042204        attributes.inject({}) do |quoted, (name, value)| 
    2205           if column = column_for_attribute(name) 
     2205          column = column_for_attribute(name) 
     2206          if column && !column.readonly? 
    22062207            quoted[name] = quote_value(value, column) unless !include_primary_key && column.primary 
    22072208          end 
    22082209          quoted