Skip to content

Commit

Permalink
schemas set by set_table_name are respected by the mysql adapter. [ra…
Browse files Browse the repository at this point in the history
…ils#5322 state:resolved]
  • Loading branch information
tenderlove committed Mar 21, 2011
1 parent 96b9fc4 commit ea8fcfb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,28 @@ def collation
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
Expand Down
36 changes: 36 additions & 0 deletions activerecord/test/cases/adapters/mysql/schema_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ea8fcfb

Please sign in to comment.