aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarmo Tänav <tarmo@itech.ee>2008-05-07 02:08:23 +0300
committerMichael Koziarski <michael@koziarski.com>2008-05-07 13:01:46 +1200
commit8877ab5852d9a1133eb9a730ae47dde214bafe55 (patch)
treeb712ec73b9a7f0c76304a6857f40bd7d4d2317e6
parente520fd5db7cb839b862c03647effee50f9223d98 (diff)
downloadrails-8877ab5852d9a1133eb9a730ae47dde214bafe55.tar.gz
rails-8877ab5852d9a1133eb9a730ae47dde214bafe55.tar.bz2
rails-8877ab5852d9a1133eb9a730ae47dde214bafe55.zip
Added AbstractAdapter#table_exists? and made AbstractAdapter#table implementation non-optional
Signed-off-by: Michael Koziarski <michael@koziarski.com>
-rwxr-xr-xactiverecord/lib/active_record/base.rb13
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb4
-rw-r--r--activerecord/test/cases/adapter_test.rb19
-rw-r--r--activerecord/test/cases/schema_dumper_test.rb211
4 files changed, 119 insertions, 128 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index ffefc3cef3..12234184c1 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1118,18 +1118,7 @@ module ActiveRecord #:nodoc:
# Indicates whether the table associated with this class exists
def table_exists?
- if connection.respond_to?(:tables)
- connection.tables.include? table_name
- else
- # if the connection adapter hasn't implemented tables, there are two crude tests that can be
- # used - see if getting column info raises an error, or if the number of columns returned is zero
- begin
- reset_column_information
- columns.size > 0
- rescue ActiveRecord::StatementInvalid
- false
- end
- end
+ connection.table_exists?(table_name)
end
# Returns an array of column objects for the table associated with this class.
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index b556516572..e2b8896d42 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -20,6 +20,10 @@ module ActiveRecord
# def tables(name = nil) end
+ def table_exists?(table_name)
+ tables.include?(table_name.to_s)
+ end
+
# Returns an array of indexes for the given table.
# def indexes(table_name, name = nil) end
diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb
index 8a74b6d6f5..91504af901 100644
--- a/activerecord/test/cases/adapter_test.rb
+++ b/activerecord/test/cases/adapter_test.rb
@@ -6,15 +6,16 @@ class AdapterTest < ActiveRecord::TestCase
end
def test_tables
- if @connection.respond_to?(:tables)
- tables = @connection.tables
- assert tables.include?("accounts")
- assert tables.include?("authors")
- assert tables.include?("tasks")
- assert tables.include?("topics")
- else
- warn "#{@connection.class} does not respond to #tables"
- end
+ tables = @connection.tables
+ assert tables.include?("accounts")
+ assert tables.include?("authors")
+ assert tables.include?("tasks")
+ assert tables.include?("topics")
+ end
+
+ def test_table_exists?
+ assert @connection.table_exists?("accounts")
+ assert !@connection.table_exists?("nonexistingtable")
end
def test_indexes
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index ba8bff3b44..c42b0efba0 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -2,140 +2,137 @@ require "cases/helper"
require 'active_record/schema_dumper'
require 'stringio'
-if ActiveRecord::Base.connection.respond_to?(:tables)
- class SchemaDumperTest < ActiveRecord::TestCase
- def standard_dump
- stream = StringIO.new
- ActiveRecord::SchemaDumper.ignore_tables = []
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
- stream.string
- end
+class SchemaDumperTest < ActiveRecord::TestCase
+ def standard_dump
+ stream = StringIO.new
+ ActiveRecord::SchemaDumper.ignore_tables = []
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
+ stream.string
+ end
- def test_schema_dump
- output = standard_dump
- assert_match %r{create_table "accounts"}, output
- assert_match %r{create_table "authors"}, output
- assert_no_match %r{create_table "schema_migrations"}, output
- end
+ def test_schema_dump
+ output = standard_dump
+ assert_match %r{create_table "accounts"}, output
+ assert_match %r{create_table "authors"}, output
+ assert_no_match %r{create_table "schema_migrations"}, output
+ end
- def test_schema_dump_excludes_sqlite_sequence
- output = standard_dump
- assert_no_match %r{create_table "sqlite_sequence"}, output
- end
+ def test_schema_dump_excludes_sqlite_sequence
+ output = standard_dump
+ assert_no_match %r{create_table "sqlite_sequence"}, output
+ end
- def assert_line_up(lines, pattern, required = false)
- return assert(true) if lines.empty?
- matches = lines.map { |line| line.match(pattern) }
- assert matches.all? if required
- matches.compact!
- return assert(true) if matches.empty?
- assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
- end
+ def assert_line_up(lines, pattern, required = false)
+ return assert(true) if lines.empty?
+ matches = lines.map { |line| line.match(pattern) }
+ assert matches.all? if required
+ matches.compact!
+ return assert(true) if matches.empty?
+ assert_equal 1, matches.map{ |match| match.offset(0).first }.uniq.length
+ end
- def column_definition_lines(output = standard_dump)
- output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
- end
+ def column_definition_lines(output = standard_dump)
+ output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
+ end
- def test_types_line_up
- column_definition_lines.each do |column_set|
- next if column_set.empty?
+ def test_types_line_up
+ column_definition_lines.each do |column_set|
+ next if column_set.empty?
- lengths = column_set.map do |column|
- if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
- match[0].length
- end
+ lengths = column_set.map do |column|
+ if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
+ match[0].length
end
-
- assert_equal 1, lengths.uniq.length
end
- end
- def test_arguments_line_up
- column_definition_lines.each do |column_set|
- assert_line_up(column_set, /:default => /)
- assert_line_up(column_set, /:limit => /)
- assert_line_up(column_set, /:null => /)
- end
+ assert_equal 1, lengths.uniq.length
end
+ end
- def test_no_dump_errors
- output = standard_dump
- assert_no_match %r{\# Could not dump table}, output
+ def test_arguments_line_up
+ column_definition_lines.each do |column_set|
+ assert_line_up(column_set, /:default => /)
+ assert_line_up(column_set, /:limit => /)
+ assert_line_up(column_set, /:null => /)
end
+ end
- def test_schema_dump_includes_not_null_columns
- stream = StringIO.new
+ def test_no_dump_errors
+ output = standard_dump
+ assert_no_match %r{\# Could not dump table}, output
+ end
- ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
- output = stream.string
- assert_match %r{:null => false}, output
- end
+ def test_schema_dump_includes_not_null_columns
+ stream = StringIO.new
- def test_schema_dump_with_string_ignored_table
- stream = StringIO.new
+ ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
+ output = stream.string
+ assert_match %r{:null => false}, output
+ end
- ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
- output = stream.string
- assert_no_match %r{create_table "accounts"}, output
- assert_match %r{create_table "authors"}, output
- assert_no_match %r{create_table "schema_migrations"}, output
- end
+ def test_schema_dump_with_string_ignored_table
+ stream = StringIO.new
+
+ ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
+ output = stream.string
+ assert_no_match %r{create_table "accounts"}, output
+ assert_match %r{create_table "authors"}, output
+ assert_no_match %r{create_table "schema_migrations"}, output
+ end
+
+ def test_schema_dump_with_regexp_ignored_table
+ stream = StringIO.new
- def test_schema_dump_with_regexp_ignored_table
- stream = StringIO.new
+ ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
+ output = stream.string
+ assert_no_match %r{create_table "accounts"}, output
+ assert_match %r{create_table "authors"}, output
+ assert_no_match %r{create_table "schema_migrations"}, output
+ end
- ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
+ def test_schema_dump_illegal_ignored_table_value
+ stream = StringIO.new
+ ActiveRecord::SchemaDumper.ignore_tables = [5]
+ assert_raise(StandardError) do
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
- output = stream.string
- assert_no_match %r{create_table "accounts"}, output
- assert_match %r{create_table "authors"}, output
- assert_no_match %r{create_table "schema_migrations"}, output
end
+ end
- def test_schema_dump_illegal_ignored_table_value
- stream = StringIO.new
- ActiveRecord::SchemaDumper.ignore_tables = [5]
- assert_raise(StandardError) do
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
- end
+ if current_adapter?(:MysqlAdapter)
+ def test_schema_dump_should_not_add_default_value_for_mysql_text_field
+ output = standard_dump
+ assert_match %r{t.text\s+"body",\s+:null => false$}, output
end
- if current_adapter?(:MysqlAdapter)
- def test_schema_dump_should_not_add_default_value_for_mysql_text_field
- output = standard_dump
- assert_match %r{t.text\s+"body",\s+:null => false$}, output
- end
-
- def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
- output = standard_dump
- match = output.match(%r{create_table "movies"(.*)do})
- assert_not_nil(match, "nonstandardpk table not found")
- assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
- end
-
- def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
- output = standard_dump
- assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
- assert_match %r{t.binary\s+"normal_blob"$}, output
- assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
- assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
- assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
- assert_match %r{t.text\s+"normal_text"$}, output
- assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
- assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
- end
+ def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
+ output = standard_dump
+ match = output.match(%r{create_table "movies"(.*)do})
+ assert_not_nil(match, "nonstandardpk table not found")
+ assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
end
- def test_schema_dump_includes_decimal_options
- stream = StringIO.new
- ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
- output = stream.string
- assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
+ def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
+ output = standard_dump
+ assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
+ assert_match %r{t.binary\s+"normal_blob"$}, output
+ assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
+ assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
+ assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
+ assert_match %r{t.text\s+"normal_text"$}, output
+ assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
+ assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
end
end
+ def test_schema_dump_includes_decimal_options
+ stream = StringIO.new
+ ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
+ output = stream.string
+ assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
+ end
end