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

Ticket #5940: sql_server_adapter_timestamp_fix.diff

File sql_server_adapter_timestamp_fix.diff, 6.6 kB (added by steven@mdlogix.com, 2 years ago)
  • activerecord/test/adapter_test_sqlserver.rb

    old new  
    1 require 'abstract_unit' 
    2 require 'fixtures/default' 
    3 require 'fixtures/post' 
    4 require 'fixtures/task' 
    5  
    6 class SqlServerAdapterTest < Test::Unit::TestCase 
    7   fixtures :posts, :tasks 
    8  
    9   def setup 
    10     @connection = ActiveRecord::Base.connection 
    11   end 
    12  
    13   def teardown 
    14     @connection.execute("SET LANGUAGE us_english") 
    15   end 
    16  
    17   # SQL Server 2000 has a bug where some unambiguous date formats are not  
    18   # correctly identified if the session language is set to german 
    19   def test_date_insertion_when_language_is_german 
    20     @connection.execute("SET LANGUAGE deutsch") 
    21  
    22     assert_nothing_raised do 
    23       Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31)) 
    24     end 
    25   end 
    26  
    27   def test_execute_without_block_closes_statement 
    28     assert_all_statements_used_are_closed do 
    29       @connection.execute("SELECT 1") 
    30     end 
    31   end 
    32    
    33   def test_execute_with_block_closes_statement 
    34     assert_all_statements_used_are_closed do 
    35       @connection.execute("SELECT 1") do |sth| 
    36         assert !sth.finished?, "Statement should still be alive within block" 
    37       end 
    38     end 
    39   end 
    40    
    41   def test_insert_with_identity_closes_statement 
    42     assert_all_statements_used_are_closed do 
    43       @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)") 
    44     end 
    45   end 
    46    
    47   def test_insert_without_identity_closes_statement 
    48     assert_all_statements_used_are_closed do 
    49       @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)") 
    50     end 
    51   end 
    52    
    53   def test_active_closes_statement 
    54     assert_all_statements_used_are_closed do 
    55       @connection.active? 
    56     end 
    57   end  
    58  
    59   def assert_all_statements_used_are_closed(&block) 
    60     existing_handles = [] 
    61     ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle} 
    62     GC.disable 
    63      
    64     yield 
    65      
    66     used_handles = [] 
    67     ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle} 
    68           
    69     assert_block "No statements were used within given block" do 
    70       used_handles.size > 0 
    71     end 
    72      
    73     ObjectSpace.each_object(DBI::StatementHandle) do |handle| 
    74       assert_block "Statement should have been closed within given block" do  
    75         handle.finished? 
    76       end       
    77     end 
    78   ensure 
    79     GC.enable 
    80   end 
    81 end 
     1require 'abstract_unit' 
     2require 'fixtures/default' 
     3require 'fixtures/post' 
     4require 'fixtures/task' 
     5 
     6class SqlServerAdapterTest < Test::Unit::TestCase 
     7  fixtures :posts, :tasks 
     8 
     9  def setup 
     10    @connection = ActiveRecord::Base.connection 
     11  end 
     12 
     13  def teardown 
     14    @connection.execute("SET LANGUAGE us_english") 
     15  end 
     16 
     17  # SQL Server 2000 has a bug where some unambiguous date formats are not  
     18  # correctly identified if the session language is set to german 
     19  def test_date_insertion_when_language_is_german 
     20    @connection.execute("SET LANGUAGE deutsch") 
     21 
     22    assert_nothing_raised do 
     23      Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31)) 
     24    end 
     25  end 
     26 
     27  def test_execute_without_block_closes_statement 
     28    assert_all_statements_used_are_closed do 
     29      @connection.execute("SELECT 1") 
     30    end 
     31  end 
     32   
     33  def test_execute_with_block_closes_statement 
     34    assert_all_statements_used_are_closed do 
     35      @connection.execute("SELECT 1") do |sth| 
     36        assert !sth.finished?, "Statement should still be alive within block" 
     37      end 
     38    end 
     39  end 
     40   
     41  def test_insert_with_identity_closes_statement 
     42    assert_all_statements_used_are_closed do 
     43      @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)") 
     44    end 
     45  end 
     46   
     47  def test_insert_without_identity_closes_statement 
     48    assert_all_statements_used_are_closed do 
     49      @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)") 
     50    end 
     51  end 
     52   
     53  def test_active_closes_statement 
     54    assert_all_statements_used_are_closed do 
     55      @connection.active? 
     56    end 
     57  end  
     58 
     59  def assert_all_statements_used_are_closed(&block) 
     60    existing_handles = [] 
     61    ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle} 
     62    GC.disable 
     63     
     64    yield 
     65     
     66    used_handles = [] 
     67    ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle} 
     68          
     69    assert_block "No statements were used within given block" do 
     70      used_handles.size > 0 
     71    end 
     72     
     73    ObjectSpace.each_object(DBI::StatementHandle) do |handle| 
     74      assert_block "Statement should have been closed within given block" do  
     75        handle.finished? 
     76      end       
     77    end 
     78  ensure 
     79    GC.enable 
     80  end 
     81end 
  • activerecord/test/fixtures/db_definitions/sqlserver.drop.sql

    old new  
    3030DROP TABLE keyboards; 
    3131DROP TABLE legacy_things; 
    3232DROP TABLE numeric_data; 
     33DROP TABLE test_timestamp; 
  • activerecord/test/fixtures/db_definitions/sqlserver.sql

    old new  
    228228  my_house_population decimal(2), 
    229229  decimal_number_with_default decimal(3,2) DEFAULT 2.78 
    230230); 
     231 
     232 
     233CREATE TABLE test_timestamp ( 
     234  id INTEGER NOT NULL PRIMARY KEY, 
     235  row_timestamp TIMESTAMP 
     236); 
  • activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb

    old new  
    280280            cols.NUMERIC_SCALE as Scale  
    281281          FROM INFORMATION_SCHEMA.COLUMNS cols  
    282282          WHERE cols.TABLE_NAME = '#{table_name}'    
     283          AND DATA_TYPE <> 'timestamp' 
    283284        } 
    284285        # Comment out if you want to have the Columns select statment logged. 
    285286        # Personally, I think it adds unnecessary bloat to the log.