DatabaseCleaner / database_cleaner-active_record

Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing.
MIT License
64 stars 63 forks source link

Truncate failing for different adapters #14

Open railsmith opened 8 years ago

railsmith commented 8 years ago

I have a remote SQL Server and a Postgres database. Truncate commands are executed in the PostgreSqlAdapter syntax even for SQL Server models and therefore data is not being truncated for SQL Server. The truncate commands is in the format truncate table "public"."Table1"; which is obviously not going to work for SQL Server. I don't know what to do. This is my configuration.

The same configuration was working fine in a previous project where I used multiple MariaDB databases but here the adapter is different.

RSpec.configure do |config|
    config.before(:suite) do
      DatabaseCleaner.add_cleaner(:active_record, SQL_SERVER)
      DatabaseCleaner.clean_with(:truncation)
      DatabaseCleaner.strategy = :transaction
   end

   config.before(:each) do
      DatabaseCleaner.start
   end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end
railsmith commented 8 years ago

The problem is with the Truncation class which loads the default connection from database.yml rather than preferring the connection from the individual models. Ideally the problem is in the below code:

    # lib/database_cleaner/active_record/truncation.rb

    class Truncation
       include ::DatabaseCleaner::ActiveRecord::Base
       include ::DatabaseCleaner::Generic::Truncation

      def clean
         connection = connection_class.connection
         .... 

After changing it to this.

      # lib/database_cleaner/base.rb

     def clean_with(*args)
      opts = args.pop
      strategy = create_strategy(*args)
      set_strategy_db strategy, self.db

      strategy.clean(opts)
      strategy
    end

     # lib/database_cleaner/active_record/truncation.rb

     def clean(opts = {})
      connection = ActiveRecord::Base.establish_connection(opts).connection
      .....

and changing the configuration into this it is working perfectly fine.

    RSpec.configure do |config|

        config.before(:suite) do
          cleaner = DatabaseCleaner.add_cleaner(:active_record, SQL_SERVER)
          cleaner.clean_with(:truncation, SQL_SERVER)
          DatabaseCleaner.clean_with(:truncation, Rails.configuration.database_configuration[Rails.env])
          DatabaseCleaner.strategy = :transaction
        end

       config.before(:each) do
         DatabaseCleaner.start
       end

      config.after(:each) do
        DatabaseCleaner.clean
      end
   end
etagwerker commented 8 years ago

@railsmith I understand the problem. Here is an alternative solution: https://gist.github.com/mgreenly/1109325

I'm not convinced that your solution is the best solution.

If you see the documentation, you can specify a single connection like this: https://github.com/DatabaseCleaner/database_cleaner#how-to-use-with-multiple-orms

I'd be more interested in adding a feature like this:

# for a single connection
DatabaseCleaner[:active_record, { :connection => :two }]

# for multiple connections (this is my proposed solution)
DatabaseCleaner[:active_record, { :connection => [:one, :two] }]
railsmith commented 8 years ago

@etagwerker

I did not intend to provide the best solution otherwise I would have opened a pull request. I posted the above solution for someone who wants a quick fix by forking the repo and using that fork in an ongoing project. I will open a pull request sometime later.

Thank you for your suggestion.