If I establish a new connection with a new adapter (ie. switch from mysql to postgresl) the generated sql is still mysql specific. I tried all sorts of reset methods on AR::Base but to no avail. There must be something being memoized on ActiveRecord base that has to do with the adapter and it doesn’t get cleared when you establish a connection with a new adapter.
I know this isn’t standard behaviour, in fact the only reason I need this is because I’m writing a multi-tenancy gem for rails that supports many different database adapters. My tests run through these adapters, but the 2nd adapter to be tested always fails with invalid sql.
Here’s an example:
ActiveRecord::Base.establish_connection :adapter => ‘mysql2’, :database => ‘my_test_db’, :username => ‘root’, :password => ‘’
User.scoped.to_sql #=> “SELECT users
.* FROM users
”
ActiveRecord::Base.establish_connection :adapter => ‘postgresql’, :database => ‘my_test_db’, :username => ‘root’, :password => ‘’
User.scoped.to_sql #=> “SELECT users
.* FROM users
”
These back ticks on column names are mysql specific and are invalid for postgresql, so it throws an error.
Does anyone know how to fully clear out whatever it is that is caching this sql being generated??
I searched through the source and was able to unset @quoted_table_name on the user model, this regenerated the table name properly, however the query now looks like:
User.scoped.to_sql #=> “SELECT "users".* FROM users
”
Still not perfect. And I also don’t want to have to go through each model and unset a bunch of vars. Any simpler way to reload these?