diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-21 11:19:14 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-21 11:20:45 -0700 |
commit | ea8fcfb729c0bca528d2e8522585cd6d0e98ebc7 (patch) | |
tree | 4128acc316c3965c13e6f18b3a45bf0888b0f3a6 | |
parent | 96b9fc44000e5f40ba463ff0e893db8c4fd33b85 (diff) | |
download | rails-ea8fcfb729c0bca528d2e8522585cd6d0e98ebc7.tar.gz rails-ea8fcfb729c0bca528d2e8522585cd6d0e98ebc7.tar.bz2 rails-ea8fcfb729c0bca528d2e8522585cd6d0e98ebc7.zip |
schemas set by set_table_name are respected by the mysql adapter. [#5322 state:resolved]
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 18 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/mysql/schema_test.rb | 36 |
2 files changed, 52 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 368c5b2023..e1186209d3 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -504,14 +504,28 @@ module ActiveRecord show_variable 'collation_database' end - def tables(name = nil) #:nodoc: + def tables(name = nil, database = nil) #:nodoc: tables = [] - result = execute("SHOW TABLES", name) + result = execute(["SHOW TABLES", database].compact.join(' IN '), name) result.each { |field| tables << field[0] } result.free tables end + def table_exists?(name) + return true if super + + name = name.to_s + schema, table = name.split('.', 2) + + unless table # A table was provided without a schema + table = schema + schema = nil + end + + tables(nil, schema).include? table + end + def drop_table(table_name, options = {}) super(table_name, options) end diff --git a/activerecord/test/cases/adapters/mysql/schema_test.rb b/activerecord/test/cases/adapters/mysql/schema_test.rb new file mode 100644 index 0000000000..c6c1d1dad5 --- /dev/null +++ b/activerecord/test/cases/adapters/mysql/schema_test.rb @@ -0,0 +1,36 @@ +require "cases/helper" +require 'models/post' +require 'models/comment' + +module ActiveRecord + module ConnectionAdapters + class MysqlSchemaTest < ActiveRecord::TestCase + fixtures :posts + + def setup + @connection = ActiveRecord::Base.connection + db = Post.connection_pool.spec.config[:database] + table = Post.table_name + @db_name = db + + @omgpost = Class.new(Post) do + set_table_name "#{db}.#{table}" + def self.name; 'Post'; end + end + end + + def test_schema + assert @omgpost.find(:first) + end + + def test_table_exists? + name = @omgpost.table_name + assert @connection.table_exists?(name), "#{name} table should exist" + end + + def test_table_exists_wrong_schema + assert(!@connection.table_exists?("#{@db_name}.zomg"), "table should not exist") + end + end if current_adapter?(:MysqlAdapter) + end +end |