Coder Social home page Coder Social logo

database_rewinder's Introduction

DatabaseRewinder

Build Status

database_rewinder is a minimalist's tiny and ultra-fast database cleaner.

Features

  • Cleans up tables via DELETE SQL. No other strategies are implemented ATM
  • Supports multiple databases
  • Runs extremely fast ๐Ÿ’จ

Why is it fast?

database_rewinder memorizes every table name into which INSERT SQL was performed during each test case. Then it executes DELETE SQL only against these tables when cleaning. So, the more number of tables you have in your database, the more benefit you will get. Also, database_rewinder joins all DELETE SQL statements and casts it in one DB server call.

Credit

This strategy was originally devised and implemented by Shingo Morita (@eudoxa) at COOKPAD Inc.

Supported versions

  • ActiveRecord 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 7.2 (edge)

  • Ruby 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3 (trunk)

Installation

Add this line to your Gemfile's :test group:

gem 'database_rewinder'

And then execute:

$ bundle

Usage

Basic configuration

Do clean in after(:each). And do clean_all or clean_with in before(:suite) if you'd like to.

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseRewinder.clean_all
    # or
    # DatabaseRewinder.clean_with :any_arg_that_would_be_actually_ignored_anyway
  end

  config.after(:each) do
    DatabaseRewinder.clean
  end
end

Dealing with multiple DBs

You can configure multiple DB connections to tell DatabaseRewinder to cleanup all of them after each test. In order to add another connection, use DatabaseRewinder[] method.

RSpec.configure do |config|
  config.before(:suite) do
    # simply give the DB connection names that are written in config/database.yml
    DatabaseRewinder['test']
    DatabaseRewinder['another_test_db']

    # you could give the DB name with connection: key if you like
    DatabaseRewinder[connection: 'yet_another_test_db']

    # or with a meaningless something first, then {connection: DB_NAME} as the second argument (DatabaseCleaner compatible)
    DatabaseRewinder[:active_record, connection: 'an_active_record_db']

    DatabaseRewinder.clean_all
  end

  config.after(:each) do
    DatabaseRewinder.clean
  end
end

MySQL + use_transactional_tests Specific Problems

database_rewinder tries to create a new DB connection for deletion when you're running tests on MySQL. You would occasionally hit some weird errors (e.g. query execution timeout) because of this, especially when your tests are run with the use_transactional_tests option enabled (which is Rails' default).

1. Properly understand what use_transactional_tests means, and consider turning it off

use_transactional_tests is the option that surrounds each of your test case with a DB transaction to roll back all your test data after each test run. So far as this works properly, you won't really need to use database_rewinder. However, this simple mechanism doesn't work well when you're running integration tests with capybara + js mode. In cases of this situation, bundle database_rewinder and add the following configuration.

RSpec.configure do |config|
  config.use_transactional_tests = false

  ...
end

2. Cleaning with multiple: false option

If you're really sure you need to keep using transactional tests + database_rewinder for some reason, then explicitly pass in multiple: false option to DatabaseRewinder.clean_all and DatabaseRewinder.clean invocations as follows. Note that you won't be able to get full performance merit that database_rewinder provides though.

RSpec.configure do |config|
  config.before :suite do
    DatabaseRewinder.clean_all multiple: false
  end

  config.after :each do
    DatabaseRewinder.clean multiple: false
  end
end

Pro Tip

database_rewinder is designed to be almost compatible with database_cleaner. So the following code will probably let your existing app work under database_rewinder without making any change on your configuration.

DatabaseCleaner = DatabaseRewinder

Contributing

Send me your pull requests.

database_rewinder's People

Contributors

aeroastro avatar akihiro17 avatar amatsuda avatar biow0lf avatar deeeki avatar eagletmt avatar eitoball avatar hirocaster avatar hsbt avatar joker1007 avatar juanitofatas avatar kmasuda-aiming avatar koic avatar kunitoo avatar kyohsuke avatar m-nakamura145 avatar nakanishi21 avatar nalabjp avatar okuramasafumi avatar onk avatar r7kamura avatar rosylilly avatar sachin21 avatar shioyama avatar sue445 avatar tomykaira avatar tricknotes avatar y-yagi avatar ybiquitous avatar yui-knk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

database_rewinder's Issues

[BUG REPORT] NoMethodError: undefined method `query' for nil:NilClass

An error occurred when factory_girl's create method is executed.
It looks like factory_girl's problem but when I am using database_cleaner, RSpec execution all is well so I think database_rewinder's problem.

Environment information

  • Ruby: 2.3.1
  • Rails: 4.2.6
  • database_rewinder: 0.6.5
  • factory_girl: 4.7.0

What

A configuration file for database_rewinder

I tried these configuration codes

Case 1: Replaced DatabaseCleaner with DatabaseRewinder

# spec/support/database_rewinder.rb 
RSpec.configure do |config|
  config.before(:suite) do
    DatabaseRewinder.strategy = :transaction
    DatabaseRewinder.clean_with :truncation
  end

  config.before do |ex|
    DatabaseRewinder.strategy = if ex.metadata[:js]
                                 :truncation
                               else
                                 :transaction
                               end
  end

  config.before do
    DatabaseRewinder.start
  end

  config.append_after do
    Capybara.reset_session!
    DatabaseRewinder.clean
  end
end

Case 2: Syntax accourding to database_cleaner

# spec/support/database_rewinder.rb  
RSpec.configure do |config|
  config.before :suite do
    DatabaseRewinder.clean_with :truncation
    DatabaseRewinder.strategy = :transaction
  end

  config.before do |example|
    DatabaseRewinder.strategy = if example.metadata[:js]
                                 :truncation
                               else
                                 :transaction
                               end
  end

  config.around do |example|
    DatabaseRewinder.cleaning do
      example.run
    end
  end
end

However, those will occur the following error.

Error message

/home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305:in `block in execute': NoMethodError: undefined method `query' for nil:NilClass (ActiveRecord::StatementInvalid)
Did you mean?  to_query: SHOW FULL FIELDS FROM `something_table`
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305:in `execute'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/database_rewinder-0.6.5/lib/database_rewinder/active_record_monkey.rb:5:in `execute'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:231:in `execute'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:312:in `execute_and_free'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:459:in `columns'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/attributes.rb:93:in `columns'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/attributes.rb:98:in `columns_hash'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/inheritance.rb:205:in `subclass_from_attributes?'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/inheritance.rb:54:in `new'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.6/lib/active_record/persistence.rb:33:in `create'
        from /home/sachin21dev/Projects/github.com/username/product_name/db/seeds/fixtures/something_table.rb:1:in `<top (required)>'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
        from /home/sachin21dev/Projects/github.com/username/product_name/spec/support/database_rewinder.rb:6:in `block (2 levels) in <top (required)>'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:425:in `instance_exec'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/example.rb:425:in `instance_exec'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/hooks.rb:357:in `run'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1724:in `block in run_hooks_with'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1724:in `each'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1724:in `run_hooks_with'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1679:in `with_suite_hooks'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:118:in `block in run_specs'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/reporter.rb:77:in `report'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:117:in `run_specs'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:93:in `run'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
        from /home/sachin21dev/Projects/github.com/username/product_name/vendor/bundle/ruby/2.3.0/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
        from /home/sachin21dev/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /home/sachin21dev/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

Rubinius support

The main issue is caused by the lack of keyword arguments support in Rubinius (rubinius/rubinius#2669).

@amatsuda I could make a patch but I need to know if you will be interested in this since it means giving up Ruby 2.0 syntax.

No tables are cleaned in Rails 7 + MySQL

Setup: Ruby 3.0.3, Rails 7.0.1, mysql2 0.5.3, MySQL 5.7.28

DatabaseRewinder.clean has no effect (no DELETE queries are executed) in Rails 7.
After some investigation, I have found out that tables_inserted is always empty, which results in no tables being cleaned.

I strongly suspect that this is caused by rails/rails@5519921, affecting database_rewinder's monkey patches in some way (in fact, database_rewinder works perfect before this commit).
This commit removes alias_method :raw_execute, :execute in DatabaseStatements, which effectively removes the call to ConnectionAdapters#execute patched by database_rewinder. As a result, no tables are registered as dirty.

`DatabaseRewinder.clean_all` is not working at rails 7.1.0

spec/support/database_rewrider.rb

# frozen_string_literal: true

require 'rake'

RSpec.configure do |config|
  except_tables = %w[
   hoge
  ]

  config.before(:suite) do
    DatabaseRewinder.strategy = [except: except_tables]
    DatabaseRewinder.clean_all
  end

  config.after do
    DatabaseRewinder.clean
  end
end
# bundle exec rspec           

An error occurred in a `before(:suite)` hook.
Failure/Error: DatabaseRewinder.clean_all

NoMethodError:
  undefined method `table_name' for ActiveRecord::SchemaMigration:Class
# ./spec/support/database_rewinder.rb:12:in `block (2 levels) in <main>'

Active Record version is 7.1.0

irb(main):013> ActiveRecord::VERSION::STRING
=> "7.1.0"

ruby and rails version

$ ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]
$ bundle exec rails -v
Rails 7.1.0

This code was working at rails 7.0.8 .
But not working when update rails 7.1.0 .
I hope this code work at rails 7.1.0 .

0.7.0 foreign key error occurred

hello, I like this gem.
Just updated to version from 0.6.5 to 0.7.0 and got this error message.
Do you have any information?

An error occurred in a `before(:suite)` hook.
Failure/Error: DatabaseRewinder.clean_with(:truncation)

Mysql2::Error:
  Cannot delete or update a parent row: a foreign key constraint fails (`test`.`comments`, CONSTRAINT `fk_rails_78d9619ae2` FOREIGN KEY (`report_id`) REFERENCES `reports` (`report_id`))
# ./spec/spec_helper.rb:50:in `block (2 levels) in '

How to use with capybara-webkit

Hi!

I'm trying the gem and find issues with capybara-webkit tests, where deletion starts before the test has finished. With DatabaseCleaner there are different strategies to deal with the issue, such as this of rspectacular.

Is this possible with database_rewinder?

Thanks for the great work!

Database Support Outside Postgres?

On https://github.com/DatabaseCleaner/database_cleaner they list the following types...

ORM Truncation Transaction Deletion
ActiveRecord Yes Yes Yes
DataMapper Yes Yes No
CouchPotato Yes No No
MongoMapper Yes No No
Mongoid Yes No No
Sequel Yes Yes No
Redis Yes No No
Ohm Yes No No
Neo4j Yes Yes* Yes*

When reviewing this file https://github.com/amatsuda/database_rewinder/blob/master/lib/database_rewinder/active_record_monkey.rb my pair and I discovered it only supports Postgres, SQL Lite 3, and Mysql.

Do you have plans to support other databases i.e. MongoDB?

Share solution

When I change to database rewinder our tests become slower, because in many cases we used transaction strategy.

Here is solution that worked for us:
Use DatabaseRewinder only for capybara+js examples, for others - DatabaseCleaner with db transactions

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseRewinder.clean_all
  end

  config.before(:each) do |example|
    if example.metadata[:js]
      # nothing
    else
      DatabaseCleaner.start
    end
  end

  config.after(:each) do |example|
    if example.metadata[:js]
      DatabaseRewinder.clean
    else
      DatabaseCleaner.clean
      DatabaseRewinder.cleaners.each {|c| c.send(:reset) }
    end
  end
end

Support for Rails 6.1.0

Hi Akira. Thank you for creating this gem.

database_rewinder currently doesn't work on rails 6.1.0 because ActiveRecord::Base.configurations returns a hash with symbols as keys. There are 2 PRs that solve this problem and #70 also removes the rails 6.2 deprecation warnings.

@JuanitoFatas Does your PR need more changes?

Can't delete referenced records on Heroku CI

Heroku CI is an auto-testing environment like Circle CI.
I am using Rspec with database_rewinder for refreshing testing database on PostgreSQL(a heroku add-on named heroku postgres).
And when it runs for deleting records which is referenced other records, an error occurs like this;

An error occurred in a `before(:suite)` hook.
Failure/Error: DatabaseRewinder.clean_all
ActiveRecord::InvalidForeignKey:
  PG::ForeignKeyViolation: ERROR:  update or delete on table "images" violates foreign key constraint "fk_rails_2c502f515d" on table "user_images"
  DETAIL:  Key (uuid)=(e88bd165-881f-4ac0-890c-729b1759d804) is still referenced from table "user_images".

I heard ( but not convinced) that database_rewinder needs SUPERUSER permission on PostgreSQL to delete referenced records, but it is impossible on heroku postgres.
Do I have any way to resolve this problem?

Incompatible with multiverse gem after upgrading to v0.9.6 (assumption)

In my model there was statement like:

establish_connection "my_connection_#{ Rails.env }"

In this case there was used multiverse gem to configure 2nd DB in rails 4 project

after upgrading to v0.9.6 now it fails because of

NameError:
  undefined method `execute' for class `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
gems/database_rewinder-0.9.6/lib/database_rewinder/active_record_monkey.rb:39:in `instance_method'
gems/database_rewinder-0.9.6/lib/database_rewinder/active_record_monkey.rb:39:in `prepended'
gems/database_rewinder-0.9.6/lib/database_rewinder/active_record_monkey.rb:66:in `prepend'
gems/database_rewinder-0.9.6/lib/database_rewinder/active_record_monkey.rb:66:in `inherited'
gems/activerecord-xxxx/lib/active_record/connection_adapters/postgresql_adapter.rb:76:in `<module:ConnectionAdapters>'
gems/activerecord-xxxx/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `<module:ActiveRecord>'
gems/activerecord-xxxx/lib/active_record/connection_adapters/postgresql_adapter.rb:21:in `<top (required)>'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:274:in `require'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:274:in `block in require'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:240:in `load_dependency'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:274:in `require'
gems/activerecord-xxxx/lib/active_record/connection_adapters/connection_specification.rb:175:in `spec'
gems/activerecord-xxxx/lib/active_record/connection_handling.rb:50:in `establish_connection'
my_record.rb:3:in `<class:MyRecord>'
my_record.rb:1:in `<top (required)>'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:274:in `require'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:274:in `block in require'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:240:in `load_dependency'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:274:in `require'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:360:in `require_or_load'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:494:in `load_missing_constant'
gems/activesupport-xxxx/lib/active_support/dependencies.rb:184:in `const_missing'

Just saw that there were some changes around that in the gem.
I hope it can help with debugging issues like that ๐Ÿค”

Tables created in complex queries are not cleaned

This is an example of a query we have, using PostGreSQL's WITH statement:

WITH updated_records AS (
  UPDATE records SET updated_at = NOW(), foo = 'bar'
  WHERE type = 'custom_type'
  RETURNING id
)
INSERT INTO record_histories (record_id, action)
SELECT
  id AS record_id,
  'set_to_bar' AS action
FROM updated_records

Breaking it down into one UPDATE records... and one INSERT INTO record_histories.. queries would solve the issue but we'd like to avoid this solution if possible and keep our current codebase as is.

For minimal break of codebase, possible solutions rely on modifying the matcher regexp to either remove the leading \A (but then every test query that includes INSERT INTO xxxxx will add xxxxx to the tables to clean) or adding support for WITH xxxxx AS (...) INSERT INTO...-style queries to the regexp (which sounds dauting enough).

What would be your thoughts?

Minitest + capybara + fixtures compatibility?

I'm trying to use database rewinder with my minitest, capybara, fixtures based test stack but I can't get it to work. It appears to remove all my fixtures and then load them again for every test at the moment.

If anyone's got any experience of using using database rewinder with this stack could you provide an example?

0.7.1 my test started freezing

Hello, I like and use this gem.
Just updated to version from 0.6.5 to 0.7.1 and my test started freezing.

image

select t_b.trx_mysql_thread_id blocking_id,
       t_w.trx_mysql_thread_id requesting_id,
       p_b.HOST blocking_host,
       p_w.HOST requesting_host,
       l.lock_table lock_table,
       l.lock_index lock_index,
       l.lock_mode lock_mode,
       p_w.TIME seconds,
       p_b.INFO blocking_info,
       p_w.INFO requesting_info
from information_schema.INNODB_LOCK_WAITS w,
     information_schema.INNODB_LOCKS l,
     information_schema.INNODB_TRX t_b,
     information_schema.INNODB_TRX t_w,
     information_schema.PROCESSLIST p_b,
     information_schema.PROCESSLIST p_w
where w.blocking_lock_id = l.lock_id
  and w.blocking_trx_id = t_b.trx_id
  and w.requesting_trx_id = t_w.trx_id
  and t_b.trx_mysql_thread_id = p_b.ID
  and t_w.trx_mysql_thread_id = p_w.ID
order by requesting_id,
         blocking_id
\G
*************************** 1. row ***************************
    blocking_id: 1652
  requesting_id: 1654
  blocking_host: localhost
requesting_host: localhost
     lock_table: `foo_test`.`bars`
     lock_index: PRIMARY
      lock_mode: X
        seconds: 4
  blocking_info: NULL
requesting_info: DELETE FROM bars
1 row in set (0.01 sec)

freezing part code.

_result = log(sql) { client.query sql }

environment

I use rails 5.0.0.1 and ruby 2.3.1 and MySQL 5.6.25.

solution(temporary)

config.use_transactional_fixtures = false

references:
http://mrdanadams.com/2012/rails-rspec-capybara-mysql-deadlocks

I suppose database deadlocks ploblem.

Thank you for always good job.

Support activerecord-import gem

Hi, any plan to support activerecord-import gem?

We use activerecord-import in production code, and use DatabaseRewinder.clean in our tests.

When we run tests on activerecord-import related codes to perform bulk inserts, DatabaseRewinder#record_inserted_table is not being called at all.

Do you think it's possible to track bulk INSERT SQL statement executed by activerecord-import gem? I'm happy to submit a PR but I need some guides to figure out which part needs to be changed ๐Ÿ™‡

Object's created in before(:all) are removed

It seems like a common pattern for DatabaseCleaner to fasten up your tests.
Some objects that stay immutable during testing, get pulled into before(all) and reused in tests, so they don't get recreated all the time.

This pattern doesn't seem to be working with DatabaseRewinder gem, i've noticed that object in before(:all) get cleaned after first test run.

Any alternatives to this?

retain existing data in tables

Hi,

Is there a configuration option to retain records existing before the test run? Say, table accounts has 3 records with ids: 1, 2, 3. During a js: true test, new records with ids 4 and 5 get inserted in accounts table. After the test complete, can the db rewinder delete all records in accounts with id > 3?

Any compatibility with 1.9x?

Just wondering if the listed supported Ruby versions is just Ruby versions that have been tested, or if the exclusion of 1.9x means that it won't work at all with 1.9?

postgresql error

Just updated to version 0.6.2 and got this error message.

/Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec': PG::ObjectNotInPrerequisiteState: ERROR:  cannot delete from view "pg_stat_statements" (ActiveRecord::StatementInvalid)
DETAIL:  Views that do not select from a single table or view are not automatically updatable.
HINT:  To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.
: DELETE FROM "pg_stat_statements";
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/active_record_monkey.rb:5:in `execute'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/cleaner.rb:49:in `block (2 levels) in delete_all'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/cleaner.rb:48:in `each'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/cleaner.rb:48:in `block in delete_all'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/referential_integrity.rb:22:in `disable_referential_integrity'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/cleaner.rb:47:in `delete_all'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/cleaner.rb:34:in `block in clean_all'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/dummy_model.rb:6:in `with_temporary_connection'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder/cleaner.rb:33:in `clean_all'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder.rb:69:in `each'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/database_rewinder-0.6.2/lib/database_rewinder.rb:69:in `clean_all'
        from /Users/rainerborene/Projects/mailkiq/spec/support/database_rewinder.rb:3:in `block (2 levels) in <top (required)>'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/example.rb:441:in `instance_exec'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/example.rb:441:in `instance_exec'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/hooks.rb:350:in `run'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/configuration.rb:1880:in `block in run_hooks_with'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/configuration.rb:1880:in `each'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/configuration.rb:1880:in `run_hooks_with'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/configuration.rb:1836:in `with_suite_hooks'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/runner.rb:112:in `block in run_specs'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/reporter.rb:77:in `report'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/runner.rb:111:in `run_specs'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/runner.rb:87:in `run'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/runner.rb:71:in `run'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/lib/rspec/core/runner.rb:45:in `invoke'
        from /Users/rainerborene/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.0/exe/rspec:4:in `<top (required)>'
        from /Users/rainerborene/.rbenv/versions/2.3.1/bin/rspec:22:in `load'
        from /Users/rainerborene/.rbenv/versions/2.3.1/bin/rspec:22:in `<main>'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.