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

Ticket #9344: refactor_table_exists_to_adapters.patch

File refactor_table_exists_to_adapters.patch, 10.9 kB (added by tarmo, 11 months ago)
  • activerecord/test/schema_dumper_test.rb

    old new  
    22require "#{File.dirname(__FILE__)}/../lib/active_record/schema_dumper" 
    33require 'stringio' 
    44 
    5 if ActiveRecord::Base.connection.respond_to?(:tables) 
     5class SchemaDumperTest < Test::Unit::TestCase 
     6  def standard_dump 
     7    stream = StringIO.new 
     8    ActiveRecord::SchemaDumper.ignore_tables = [] 
     9    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
     10    stream.string 
     11  end 
     12   
     13  def test_schema_dump 
     14    output = standard_dump 
     15    assert_match %r{create_table "accounts"}, output 
     16    assert_match %r{create_table "authors"}, output 
     17    assert_no_match %r{create_table "schema_info"}, output 
     18  end 
     19   
     20  def test_schema_dump_excludes_sqlite_sequence 
     21    output = standard_dump 
     22    assert_no_match %r{create_table "sqlite_sequence"}, output 
     23  end 
    624 
    7   class SchemaDumperTest < Test::Unit::TestCase 
    8     def standard_dump 
    9       stream = StringIO.new 
    10       ActiveRecord::SchemaDumper.ignore_tables = [] 
    11       ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
    12       stream.string 
    13     end 
    14      
    15     def test_schema_dump 
    16       output = standard_dump 
    17       assert_match %r{create_table "accounts"}, output 
    18       assert_match %r{create_table "authors"}, output 
    19       assert_no_match %r{create_table "schema_info"}, output 
    20     end 
    21      
    22     def test_schema_dump_excludes_sqlite_sequence 
    23       output = standard_dump 
    24       assert_no_match %r{create_table "sqlite_sequence"}, output 
    25     end 
     25  def assert_line_up(lines, pattern, required = false) 
     26    return assert(true) if lines.empty? 
     27    matches = lines.map { |line| line.match(pattern) } 
     28    assert matches.all? if required 
     29    matches.compact! 
     30    return assert(true) if matches.empty? 
     31    assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length 
     32  end 
    2633 
    27     def assert_line_up(lines, pattern, required = false) 
    28       return assert(true) if lines.empty? 
    29       matches = lines.map { |line| line.match(pattern) } 
    30       assert matches.all? if required 
    31       matches.compact! 
    32       return assert(true) if matches.empty? 
    33       assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length 
    34     end 
     34  def column_definition_lines(output = standard_dump) 
     35    output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) } 
     36  end 
    3537 
    36     def column_definition_lines(output = standard_dump) 
    37       output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) } 
    38     end 
     38  def test_types_line_up 
     39    column_definition_lines.each do |column_set| 
     40      next if column_set.empty? 
    3941 
    40     def test_types_line_up 
    41       column_definition_lines.each do |column_set| 
    42         next if column_set.empty? 
    43  
    44         lengths = column_set.map do |column|  
    45           if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/) 
    46             match[0].length 
    47           end 
     42      lengths = column_set.map do |column|  
     43        if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/) 
     44          match[0].length 
    4845        end 
    49  
    50         assert_equal 1, lengths.uniq.length 
    5146      end 
     47 
     48      assert_equal 1, lengths.uniq.length 
    5249    end 
    53      
    54     def test_arguments_line_up 
    55       column_definition_lines.each do |column_set| 
    56         assert_line_up(column_set, /:default => /) 
    57         assert_line_up(column_set, /:limit => /) 
    58         assert_line_up(column_set, /:null => /) 
    59       end 
     50  end 
     51   
     52  def test_arguments_line_up 
     53    column_definition_lines.each do |column_set| 
     54      assert_line_up(column_set, /:default => /) 
     55      assert_line_up(column_set, /:limit => /) 
     56      assert_line_up(column_set, /:null => /) 
    6057    end 
     58  end 
     59   
     60  def test_no_dump_errors 
     61    output = standard_dump 
     62    assert_no_match %r{\# Could not dump table}, output 
     63  end 
     64   
     65  def test_schema_dump_includes_not_null_columns 
     66    stream = StringIO.new 
    6167     
    62     def test_no_dump_errors 
    63       output = standard_dump 
    64       assert_no_match %r{\# Could not dump table}, output 
    65     end 
     68    ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/] 
     69    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
     70    output = stream.string 
     71    assert_match %r{:null => false}, output 
     72  end 
     73 
     74  def test_schema_dump_with_string_ignored_table 
     75    stream = StringIO.new 
    6676     
    67     def test_schema_dump_includes_not_null_columns 
    68       stream = StringIO.new 
    69        
    70       ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/] 
    71       ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
    72       output = stream.string 
    73       assert_match %r{:null => false}, output 
    74     end 
     77    ActiveRecord::SchemaDumper.ignore_tables = ['accounts']       
     78    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
     79    output = stream.string 
     80    assert_no_match %r{create_table "accounts"}, output 
     81    assert_match %r{create_table "authors"}, output 
     82    assert_no_match %r{create_table "schema_info"}, output 
     83  end 
    7584 
    76     def test_schema_dump_with_string_ignored_table 
    77       stream = StringIO.new 
    78        
    79       ActiveRecord::SchemaDumper.ignore_tables = ['accounts']       
    80       ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
    81       output = stream.string 
    82       assert_no_match %r{create_table "accounts"}, output 
    83       assert_match %r{create_table "authors"}, output 
    84       assert_no_match %r{create_table "schema_info"}, output 
    85     end 
    8685 
     86  def test_schema_dump_with_regexp_ignored_table 
     87    stream = StringIO.new 
     88     
     89    ActiveRecord::SchemaDumper.ignore_tables = [/^account/]       
     90    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
     91    output = stream.string 
     92    assert_no_match %r{create_table "accounts"}, output 
     93    assert_match %r{create_table "authors"}, output 
     94    assert_no_match %r{create_table "schema_info"}, output 
     95  end 
    8796 
    88     def test_schema_dump_with_regexp_ignored_table 
    89       stream = StringIO.new 
    90        
    91       ActiveRecord::SchemaDumper.ignore_tables = [/^account/]       
     97 
     98  def test_schema_dump_illegal_ignored_table_value 
     99    stream = StringIO.new       
     100    ActiveRecord::SchemaDumper.ignore_tables = [5]       
     101    assert_raise(StandardError) do 
    92102      ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
    93       output = stream.string 
    94       assert_no_match %r{create_table "accounts"}, output 
    95       assert_match %r{create_table "authors"}, output 
    96       assert_no_match %r{create_table "schema_info"}, output 
    97103    end 
     104  end 
    98105 
    99  
    100     def test_schema_dump_illegal_ignored_table_value 
    101       stream = StringIO.new       
    102       ActiveRecord::SchemaDumper.ignore_tables = [5]       
    103       assert_raise(StandardError) do 
    104         ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
    105       end 
     106  if current_adapter?(:MysqlAdapter) 
     107    def test_schema_dump_should_not_add_default_value_for_mysql_text_field 
     108      output = standard_dump 
     109      assert_match %r{t.text\s+"body",\s+:null => false$}, output 
    106110    end 
    107  
    108     if current_adapter?(:MysqlAdapter) 
    109       def test_schema_dump_should_not_add_default_value_for_mysql_text_field 
    110         output = standard_dump 
    111         assert_match %r{t.text\s+"body",\s+:null => false$}, output 
    112       end 
    113     end 
    114  
    115     def test_schema_dump_includes_decimal_options 
    116       stream = StringIO.new       
    117       ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/] 
    118       ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
    119       output = stream.string 
    120       assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output 
    121     end 
    122111  end 
    123112 
     113  def test_schema_dump_includes_decimal_options 
     114    stream = StringIO.new       
     115    ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/] 
     116    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 
     117    output = stream.string 
     118    assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output 
     119  end 
    124120end 
  • activerecord/test/adapter_test.rb

    old new  
    66  end 
    77 
    88  def test_tables 
    9     if @connection.respond_to?(:tables) 
    10       tables = @connection.tables 
    11       assert tables.include?("accounts") 
    12       assert tables.include?("authors") 
    13       assert tables.include?("tasks") 
    14       assert tables.include?("topics") 
    15     else 
    16       warn "#{@connection.class} does not respond to #tables" 
    17     end 
     9    tables = @connection.tables 
     10    assert tables.include?("accounts") 
     11    assert tables.include?("authors") 
     12    assert tables.include?("tasks") 
     13    assert tables.include?("topics") 
    1814  end 
    1915 
     16  def test_table_exists? 
     17    assert @connection.table_exists?("accounts") 
     18    assert !@connection.table_exists?("nonexistingtable") 
     19  end 
     20 
    2021  def test_indexes 
    2122    idx_name = "accounts_idx" 
    2223 
  • activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    old new  
    2020 
    2121      # def tables(name = nil) end 
    2222 
     23      def table_exists?(table_name) 
     24        tables.include? table_name 
     25      end 
     26 
    2327      # Returns an array of indexes for the given table. 
    2428      # def indexes(table_name, name = nil) end 
    2529 
     
    9397 
    9498        yield table_definition 
    9599 
    96         if options[:force] 
    97           drop_table(name, options) rescue nil 
     100        if options[:force] and table_exists?(name) 
     101          drop_table(name, options) 
    98102        end 
    99103 
    100104        create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE " 
  • activerecord/lib/active_record/base.rb

    old new  
    821821 
    822822      # Indicates whether the table associated with this class exists 
    823823      def table_exists? 
    824         if connection.respond_to?(:tables) 
    825           connection.tables.include? table_name 
    826         else 
    827           # if the connection adapter hasn't implemented tables, there are two crude tests that can be 
    828           # used - see if getting column info raises an error, or if the number of columns returned is zero 
    829           begin 
    830             reset_column_information 
    831             columns.size > 0 
    832           rescue ActiveRecord::StatementInvalid 
    833             false 
    834           end           
    835         end 
     824        connection.table_exists?(table_name) 
    836825      end 
    837826 
    838827      # Returns an array of column objects for the table associated with this class.