Changing adapters in ActiveRecord does not change the generated sql

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?

You could try calling reset_column_information (although it looks like this doesn't clear out quoted table name)

Fred

ya that doesn’t appear to help. especially if I’m just selecting *

I started looking into the reload! method as I figured maybe manually reloading the classes in between testing adapters would help.

In the dev console doing a reload appears to be similar to resetting the quoted_table_name, I get the same:

User.scoped.to_sql #=> “SELECT "users".* FROM users

So it’s caching that table_name (in the FROM clause) somewhere else (that isn’t reloaded)

Does anyone have any insight into this? I’m digging into the source but I’m not having a ton of luck.

I’ve found out something very interesting. If I’m first connected to Postgresql, then switch to Mysql, I can reset both table and column names properly using

Model.reset_column_information

and

Model.reset_table_name

This gives me the proper backquoted columns/table names for Mysql.

User.scoped.to_sql #=> “SELECT users.* FROM users

If however, I switch back to Postgresql, and run the same commands, it doesn’t actually reset the column information properly, only the table_name.

User.scoped.to_sql #=> “SELECT "users".* FROM users

I’ve found that it actually doesn’t matter in which order this happens. The first switch to one adapter allows me to reset properly. The second switch however does NOT.