public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Added AbstractAdapter#table_exists? and made AbstractAdapter#table 
implementation non-optional

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

Comments

    No one has commented yet.