All Projects → amatsuda → Database_rewinder

amatsuda / Database_rewinder

Licence: mit
minimalist's tiny and ultra-fast database cleaner

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Database rewinder

Graphjin
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.
Stars: ✭ 1,264 (+84.53%)
Mutual labels:  database, mysql, postgresql, rails
Denodb
MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
Stars: ✭ 498 (-27.3%)
Mutual labels:  database, mysql, postgresql, sqlite3
Mikro Orm
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite databases.
Stars: ✭ 3,874 (+465.55%)
Mutual labels:  database, mysql, postgresql, sqlite3
Qb
The database toolkit for go
Stars: ✭ 524 (-23.5%)
Mutual labels:  database, mysql, postgresql, sqlite3
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (+84.96%)
Mutual labels:  database, mysql, postgresql, sqlite3
Sqlcheck
Automatically identify anti-patterns in SQL queries
Stars: ✭ 2,062 (+201.02%)
Mutual labels:  database, mysql, postgresql, sqlite3
Sqlboiler
Generate a Go ORM tailored to your database schema.
Stars: ✭ 4,497 (+556.5%)
Mutual labels:  database, mysql, postgresql, sqlite3
Mormot
Synopse mORMot ORM/SOA/MVC framework
Stars: ✭ 607 (-11.39%)
Mutual labels:  mysql, postgresql, sqlite3
Backup
Easy full stack backup operations on UNIX-like systems.
Stars: ✭ 4,682 (+583.5%)
Mutual labels:  database, mysql, postgresql
Beekeeper Studio
Modern and easy to use SQL client for MySQL, Postgres, SQLite, SQL Server, and more. Linux, MacOS, and Windows.
Stars: ✭ 8,053 (+1075.62%)
Mutual labels:  database, mysql, postgresql
Dbshield
Database firewall written in Go
Stars: ✭ 620 (-9.49%)
Mutual labels:  database, mysql, postgresql
Phpmyfaq
phpMyFAQ - Open Source FAQ web application for PHP and MySQL, PostgreSQL and other databases
Stars: ✭ 494 (-27.88%)
Mutual labels:  database, mysql, postgresql
Openrecord
Make ORMs great again!
Stars: ✭ 474 (-30.8%)
Mutual labels:  mysql, postgresql, sqlite3
Adminer
Database management in a single PHP file
Stars: ✭ 4,999 (+629.78%)
Mutual labels:  database, mysql, postgresql
Easydb
Easy-to-use PDO wrapper for PHP projects.
Stars: ✭ 624 (-8.91%)
Mutual labels:  database, mysql, postgresql
Lucid
AdonisJS official SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
Stars: ✭ 613 (-10.51%)
Mutual labels:  mysql, postgresql, sqlite3
Evolve
Database migration tool for .NET and .NET Core projects. Inspired by Flyway.
Stars: ✭ 477 (-30.36%)
Mutual labels:  database, mysql, postgresql
Blog
Everything about database,business.(Most for PostgreSQL).
Stars: ✭ 6,330 (+824.09%)
Mutual labels:  database, mysql, postgresql
Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (-21.31%)
Mutual labels:  database, mysql, postgresql
Laravel Db Snapshots
Quickly dump and load databases
Stars: ✭ 650 (-5.11%)
Mutual labels:  database, mysql, postgresql

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.0, 4.1, 4.2, 5.0, 5.1, 5.2, 6.0 (edge)

  • Ruby 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6 (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_examples = 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].