From 323b7585e106ca9ca26db5a886801239c725a95a Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Tue, 3 May 2011 21:14:27 +0200 Subject: [PATCH] added the mysql schema test to mysql2 adapter, and fixed the corresponding failures --- .../connection_adapters/mysql2_adapter.rb | 19 ++++++++-- .../test/cases/adapters/mysql/schema_test.rb | 2 +- .../test/cases/adapters/mysql2/schema_test.rb | 36 +++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 activerecord/test/cases/adapters/mysql2/schema_test.rb diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index 98a8dd6453..42eeacc173 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -410,12 +410,27 @@ def collation show_variable 'collation_database' end - def tables(name = nil) - execute("SHOW TABLES", 'SCHEMA').collect do |field| + def tables(name = nil, database = nil) #:nodoc: + sql = ["SHOW TABLES", database].compact.join(' IN ') + execute(sql, 'SCHEMA').collect do |field| field.first end 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 index c6c1d1dad5..a2155d1dd1 100644 --- a/activerecord/test/cases/adapters/mysql/schema_test.rb +++ b/activerecord/test/cases/adapters/mysql/schema_test.rb @@ -31,6 +31,6 @@ def test_table_exists? 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 end diff --git a/activerecord/test/cases/adapters/mysql2/schema_test.rb b/activerecord/test/cases/adapters/mysql2/schema_test.rb new file mode 100644 index 0000000000..858d1da2dd --- /dev/null +++ b/activerecord/test/cases/adapters/mysql2/schema_test.rb @@ -0,0 +1,36 @@ +require "cases/helper" +require 'models/post' +require 'models/comment' + +module ActiveRecord + module ConnectionAdapters + class Mysql2SchemaTest < 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 + end +end -- GitLab