database_cleaner-active_record icon indicating copy to clipboard operation
database_cleaner-active_record copied to clipboard

When multiple Database backends are used in ActiveRecord, DatabaseCleaner fails to truncate tables

Open DFrenkel opened this issue 11 years ago • 7 comments

When both MySQL and Postgres ActiveRecord models are used, and when testing MySQL-backed code, Truncation strategy includes BOTH ::DatabaseCleaner::ActiveRecord::MysqlAdapter and ::DatabaseCleaner::ActiveRecord::PostgreSQLAdapter into ::DatabaseCleaner::ActiveRecord::AbstractAdapter, which, in turn, gets included into ActiveRecord::AbstractAdapter.

Since PostgreSQLAdapter gets included last, even when ActiveRecord::Base.connection is a MySQL connection, DatabaseCleaner attempts to execute Postgres-specific truncations, resulting in errors:

NameError: undefined local variable or method `postgresql_version' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x0000000692feb8>
  occurred at /usr/local/rvm/gems/ruby-1.9.2-p290/gems/database_cleaner-1.1.1/lib/database_cleaner/active_record/truncation.rb:123:in `db_version'

DFrenkel avatar Aug 02 '13 20:08 DFrenkel

I am getting same issue here. it seems like postgresql_version method isn't existed anywhere.

bry4n avatar Aug 05 '13 19:08 bry4n

Downgraded to 1.0.1 fixed the problem. -- https://github.com/bmabey/database_cleaner/issues/224#issuecomment-22031253

bry4n avatar Aug 05 '13 19:08 bry4n

I saw the same error, but instead I was using the OracleEnhanced adapter only. When it called truncate_tables, it used the PostgreSQLAdapter version as that method doesn't exist in the OracleEnhnacedAdapter, which led to the same error regarding an undefined postgresql_version method.

Downgrading to 1.0.1 fixed the problem for me as well.

sockmonk avatar Aug 16 '13 17:08 sockmonk

9bf038d5c287cca98612fb0a76e58147d3694868 is the culprit, cc @bmabey @tommeier

fredwu avatar Aug 22 '13 09:08 fredwu

Awaiting testers on https://github.com/bmabey/database_cleaner/pull/221

tommeier avatar Aug 22 '13 09:08 tommeier

Was this fixed by DatabaseCleaner/database_cleaner#241?

fabiokr avatar Mar 11 '14 20:03 fabiokr

Problem there:

class Deletion < Truncation
    if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
      include SelectiveTruncation
    end
    ...
end

Tab10id avatar Jun 29 '16 11:06 Tab10id