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 2 2 require "#{File.dirname(__FILE__)}/../lib/active_record/schema_dumper" 3 3 require 'stringio' 4 4 5 if ActiveRecord::Base.connection.respond_to?(:tables) 5 class 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 6 24 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 26 33 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 35 37 36 def column_definition_lines(output = standard_dump)37 output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }38 end38 def test_types_line_up 39 column_definition_lines.each do |column_set| 40 next if column_set.empty? 39 41 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 48 45 end 49 50 assert_equal 1, lengths.uniq.length51 46 end 47 48 assert_equal 1, lengths.uniq.length 52 49 end 53 54 def test_arguments_line_up55 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 end50 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 => /) 60 57 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 61 67 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 66 76 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 75 84 76 def test_schema_dump_with_string_ignored_table77 stream = StringIO.new78 79 ActiveRecord::SchemaDumper.ignore_tables = ['accounts']80 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)81 output = stream.string82 assert_no_match %r{create_table "accounts"}, output83 assert_match %r{create_table "authors"}, output84 assert_no_match %r{create_table "schema_info"}, output85 end86 85 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 87 96 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 92 102 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) 93 output = stream.string94 assert_no_match %r{create_table "accounts"}, output95 assert_match %r{create_table "authors"}, output96 assert_no_match %r{create_table "schema_info"}, output97 103 end 104 end 98 105 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 106 110 end 107 108 if current_adapter?(:MysqlAdapter)109 def test_schema_dump_should_not_add_default_value_for_mysql_text_field110 output = standard_dump111 assert_match %r{t.text\s+"body",\s+:null => false$}, output112 end113 end114 115 def test_schema_dump_includes_decimal_options116 stream = StringIO.new117 ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]118 ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)119 output = stream.string120 assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output121 end122 111 end 123 112 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 124 120 end -
activerecord/test/adapter_test.rb
old new 6 6 end 7 7 8 8 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") 18 14 end 19 15 16 def test_table_exists? 17 assert @connection.table_exists?("accounts") 18 assert !@connection.table_exists?("nonexistingtable") 19 end 20 20 21 def test_indexes 21 22 idx_name = "accounts_idx" 22 23 -
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
old new 20 20 21 21 # def tables(name = nil) end 22 22 23 def table_exists?(table_name) 24 tables.include? table_name 25 end 26 23 27 # Returns an array of indexes for the given table. 24 28 # def indexes(table_name, name = nil) end 25 29 … … 93 97 94 98 yield table_definition 95 99 96 if options[:force] 97 drop_table(name, options) rescue nil100 if options[:force] and table_exists?(name) 101 drop_table(name, options) 98 102 end 99 103 100 104 create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE " -
activerecord/lib/active_record/base.rb
old new 821 821 822 822 # Indicates whether the table associated with this class exists 823 823 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) 836 825 end 837 826 838 827 # Returns an array of column objects for the table associated with this class.