Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate failing for different adapters #14

Open
railsmith opened this issue May 26, 2016 · 3 comments
Open

Truncate failing for different adapters #14

railsmith opened this issue May 26, 2016 · 3 comments

Comments

@railsmith
Copy link

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 railsmith changed the title Truncate failing for multiple adaptors Truncate failing for different adapters May 26, 2016
@railsmith
Copy link
Author

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
Copy link
Member

@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
Copy link
Author

@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.

@botandrose botandrose transferred this issue from DatabaseCleaner/database_cleaner Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants