You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found that in my test, device model's connection will point to secondary_test database. And I will have following error as my secondary_test database don't have a table call devices:
ActiveRecord::StatementInvalid:
PG::UndefinedTable: ERROR: relation "devices" does not exist
LINE 5: WHERE a.attrelid = '"devices"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"devices"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
However, since ::ActiveRecord::Base.descendants is blank when lookup_from_connection_pool is called, lookup_from_connection_pool would return nil, and establish_connection will be called. Unfortunately, ActiveRecord::Base.establish_connection(connection_hash) has a huge side effect in Rails 4+. It will set connection to specify one for ALL models that inherit from ActiveRecord::Base class. This can be reproduced by following code:
will first set ALLActiveRecord::Base descendants' connections to test db, then it will set ALL their connection to secondary_test. Therefore, when I try to use Device in my spec, I will have PG::UndefinedTable: ERROR
Here is the similar issues that cause by the side effect for establish_connection:
I've fixed this by adding Rails.application.eager_load! to spec helper
RSpec.configuredo |config|
config.use_transactional_fixtures=falseRails.application.eager_load!%i(db_test1db_test2db_test3).eachdo |connection|
config.before:suitedo# transaction strategy is not working with multiple databasesDatabaseCleaner[:active_record,{connection: connection}].strategy=:deletionDatabaseCleaner[:active_record,{connection: connection}].clean_with:truncationendconfig.around:eachdo |example|
DatabaseCleaner[:active_record,{connection: connection}].cleaning &exampleendendend
I have two database, one with a model call
Device
, and the other has a model callUser
.When I set up
database_cleaner
according to readme describe:I found that in my test,
device
model's connection will point tosecondary_test
database. And I will have following error as my secondary_test database don't have a table calldevices
:I dig in the code, and found that it is cause by
#connection_class
However, since
::ActiveRecord::Base.descendants
is blank whenlookup_from_connection_pool
is called,lookup_from_connection_pool
would return nil, andestablish_connection
will be called. Unfortunately,ActiveRecord::Base.establish_connection(connection_hash)
has a huge side effect in Rails 4+. It will set connection to specify one for ALL models that inherit from ActiveRecord::Base class. This can be reproduced by following code:In my case, following code:
will first set ALL
ActiveRecord::Base
descendants' connections totest
db, then it will set ALL their connection tosecondary_test
. Therefore, when I try to useDevice
in my spec, I will havePG::UndefinedTable: ERROR
Here is the similar issues that cause by the side effect for
establish_connection
:The text was updated successfully, but these errors were encountered: