Skip to content
This repository
Browse code

Added AbstractAdapter#table_exists? and made AbstractAdapter#table im…

…plementation non-optional

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 8877ab5852d9a1133eb9a730ae47dde214bafe55 1 parent e520fd5
Tarmo Tänav authored NZKoz committed
13  activerecord/lib/active_record/base.rb
@@ -1118,18 +1118,7 @@ def class_name(table_name = table_name) # :nodoc:
1118 1118
 
1119 1119
       # Indicates whether the table associated with this class exists
1120 1120
       def table_exists?
1121  
-        if connection.respond_to?(:tables)
1122  
-          connection.tables.include? table_name
1123  
-        else
1124  
-          # if the connection adapter hasn't implemented tables, there are two crude tests that can be
1125  
-          # used - see if getting column info raises an error, or if the number of columns returned is zero
1126  
-          begin
1127  
-            reset_column_information
1128  
-            columns.size > 0
1129  
-          rescue ActiveRecord::StatementInvalid
1130  
-            false
1131  
-          end
1132  
-        end
  1121
+        connection.table_exists?(table_name)
1133 1122
       end
1134 1123
 
1135 1124
       # Returns an array of column objects for the table associated with this class.
4  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -20,6 +20,10 @@ def table_alias_for(table_name)
20 20
 
21 21
       # def tables(name = nil) end
22 22
 
  23
+      def table_exists?(table_name)
  24
+        tables.include?(table_name.to_s)
  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
 
19  activerecord/test/cases/adapter_test.rb
@@ -6,15 +6,16 @@ def setup
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")
  14
+  end
  15
+
  16
+  def test_table_exists?
  17
+    assert @connection.table_exists?("accounts")
  18
+    assert !@connection.table_exists?("nonexistingtable")
18 19
   end
19 20
 
20 21
   def test_indexes
211  activerecord/test/cases/schema_dumper_test.rb
@@ -2,140 +2,137 @@
2 2
 require 'active_record/schema_dumper'
3 3
 require 'stringio'
4 4
 
5  
-if ActiveRecord::Base.connection.respond_to?(:tables)
6 5
 
7  
-  class SchemaDumperTest < ActiveRecord::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
  6
+class SchemaDumperTest < ActiveRecord::TestCase
  7
+  def standard_dump
  8
+    stream = StringIO.new
  9
+    ActiveRecord::SchemaDumper.ignore_tables = []
  10
+    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
  11
+    stream.string
  12
+  end
14 13
 
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_migrations"}, output
20  
-    end
  14
+  def test_schema_dump
  15
+    output = standard_dump
  16
+    assert_match %r{create_table "accounts"}, output
  17
+    assert_match %r{create_table "authors"}, output
  18
+    assert_no_match %r{create_table "schema_migrations"}, output
  19
+  end
21 20
 
22  
-    def test_schema_dump_excludes_sqlite_sequence
23  
-      output = standard_dump
24  
-      assert_no_match %r{create_table "sqlite_sequence"}, output
25  
-    end
  21
+  def test_schema_dump_excludes_sqlite_sequence
  22
+    output = standard_dump
  23
+    assert_no_match %r{create_table "sqlite_sequence"}, output
  24
+  end
26 25
 
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
  26
+  def assert_line_up(lines, pattern, required = false)
  27
+    return assert(true) if lines.empty?
  28
+    matches = lines.map { |line| line.match(pattern) }
  29
+    assert matches.all? if required
  30
+    matches.compact!
  31
+    return assert(true) if matches.empty?
  32
+    assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
  33
+  end
35 34
 
36  
-    def column_definition_lines(output = standard_dump)
37  
-      output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
38  
-    end
  35
+  def column_definition_lines(output = standard_dump)
  36
+    output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
  37
+  end
39 38
 
40  
-    def test_types_line_up
41  
-      column_definition_lines.each do |column_set|
42  
-        next if column_set.empty?
  39
+  def test_types_line_up
  40
+    column_definition_lines.each do |column_set|
  41
+      next if column_set.empty?
43 42
 
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
  43
+      lengths = column_set.map do |column|
  44
+        if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
  45
+          match[0].length
48 46
         end
49  
-
50  
-        assert_equal 1, lengths.uniq.length
51 47
       end
52  
-    end
53 48
 
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
  49
+      assert_equal 1, lengths.uniq.length
60 50
     end
  51
+  end
61 52
 
62  
-    def test_no_dump_errors
63  
-      output = standard_dump
64  
-      assert_no_match %r{\# Could not dump table}, output
  53
+  def test_arguments_line_up
  54
+    column_definition_lines.each do |column_set|
  55
+      assert_line_up(column_set, /:default => /)
  56
+      assert_line_up(column_set, /:limit => /)
  57
+      assert_line_up(column_set, /:null => /)
65 58
     end
  59
+  end
66 60
 
67  
-    def test_schema_dump_includes_not_null_columns
68  
-      stream = StringIO.new
  61
+  def test_no_dump_errors
  62
+    output = standard_dump
  63
+    assert_no_match %r{\# Could not dump table}, output
  64
+  end
69 65
 
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
  66
+  def test_schema_dump_includes_not_null_columns
  67
+    stream = StringIO.new
75 68
 
76  
-    def test_schema_dump_with_string_ignored_table
77  
-      stream = StringIO.new
  69
+    ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
  70
+    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
  71
+    output = stream.string
  72
+    assert_match %r{:null => false}, output
  73
+  end
78 74
 
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_migrations"}, output
85  
-    end
  75
+  def test_schema_dump_with_string_ignored_table
  76
+    stream = StringIO.new
  77
+
  78
+    ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
  79
+    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
  80
+    output = stream.string
  81
+    assert_no_match %r{create_table "accounts"}, output
  82
+    assert_match %r{create_table "authors"}, output
  83
+    assert_no_match %r{create_table "schema_migrations"}, output
  84
+  end
  85
+
  86
+  def test_schema_dump_with_regexp_ignored_table
  87
+    stream = StringIO.new
86 88
 
87  
-    def test_schema_dump_with_regexp_ignored_table
88  
-      stream = StringIO.new
  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_migrations"}, output
  95
+  end
89 96
 
90  
-      ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
  97
+  def test_schema_dump_illegal_ignored_table_value
  98
+    stream = StringIO.new
  99
+    ActiveRecord::SchemaDumper.ignore_tables = [5]
  100
+    assert_raise(StandardError) do
91 101
       ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
92  
-      output = stream.string
93  
-      assert_no_match %r{create_table "accounts"}, output
94  
-      assert_match %r{create_table "authors"}, output
95  
-      assert_no_match %r{create_table "schema_migrations"}, output
96 102
     end
  103
+  end
97 104
 
98  
-    def test_schema_dump_illegal_ignored_table_value
99  
-      stream = StringIO.new
100  
-      ActiveRecord::SchemaDumper.ignore_tables = [5]
101  
-      assert_raise(StandardError) do
102  
-        ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
103  
-      end
  105
+  if current_adapter?(:MysqlAdapter)
  106
+    def test_schema_dump_should_not_add_default_value_for_mysql_text_field
  107
+      output = standard_dump
  108
+      assert_match %r{t.text\s+"body",\s+:null => false$}, output
104 109
     end
105 110
 
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
110  
-      end
111  
-
112  
-      def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
113  
-        output = standard_dump
114  
-        match = output.match(%r{create_table "movies"(.*)do})
115  
-        assert_not_nil(match, "nonstandardpk table not found")
116  
-        assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
117  
-      end
118  
-
119  
-      def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
120  
-        output = standard_dump
121  
-        assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
122  
-        assert_match %r{t.binary\s+"normal_blob"$}, output
123  
-        assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
124  
-        assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
125  
-        assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
126  
-        assert_match %r{t.text\s+"normal_text"$}, output
127  
-        assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
128  
-        assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
129  
-      end
  111
+    def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
  112
+      output = standard_dump
  113
+      match = output.match(%r{create_table "movies"(.*)do})
  114
+      assert_not_nil(match, "nonstandardpk table not found")
  115
+      assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
130 116
     end
131 117
 
132  
-    def test_schema_dump_includes_decimal_options
133  
-      stream = StringIO.new
134  
-      ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
135  
-      ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
136  
-      output = stream.string
137  
-      assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
  118
+    def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
  119
+      output = standard_dump
  120
+      assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
  121
+      assert_match %r{t.binary\s+"normal_blob"$}, output
  122
+      assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
  123
+      assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
  124
+      assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
  125
+      assert_match %r{t.text\s+"normal_text"$}, output
  126
+      assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
  127
+      assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
138 128
     end
139 129
   end
140 130
 
  131
+  def test_schema_dump_includes_decimal_options
  132
+    stream = StringIO.new
  133
+    ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
  134
+    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
  135
+    output = stream.string
  136
+    assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
  137
+  end
141 138
 end

0 notes on commit 8877ab5

Please sign in to comment.
Something went wrong with that request. Please try again.