brianmario/mysql2 — Issue #235: rails connection_adapter: table_exists? fails when set_table_name used
Author: bryanlarsen
Created: 2012-01-09 18:05:35+00:00
State: open

class Organization < ActiveRecord::Base
set_table_name "reference.organizations"
end

>> Organization.table_exists?
=> false

solution:

monkey patch Mysql2Adapter with:

module ActiveRecord
module ConnectionAdapters
class Mysql2Adapter
def tables(name = nil, database = nil) #:nodoc:
tables = []
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
end
end
end

These functions were stolen from mysql_adapter.rb in rails 3.0.11 except that I was the one that commented out `result.free`. Which is the broad clue that I'm just blindly cutting and pasting without understanding the underlying code. But it's working for me, for now.