Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between truncation, transaction and deletion database strategies

What is the difference between truncation, transaction and deletion database strategies when using Rspec? I can't find any resources explaining this. I read the Database Cleaner readme but it doesn't explain what each of these do.

Why do we have to use truncation strategy for Capybara? Do I have to clean up my database when testing or can I disable it. I dont understand why I should clean up my database after each test case, wouldn't it just slow down testing?

like image 812
Ibrahim Muhammad Avatar asked Jun 05 '12 21:06

Ibrahim Muhammad


People also ask

When to use TRUNCATE vs delete?

Differences between the SQL Server DELETE and TRUNCATE Commands. Truncate reseeds identity values, whereas delete doesn't. Truncate removes all records and doesn't fire triggers. Truncate is faster compared to delete as it makes less use of the transaction log.

Which is faster TRUNCATE or delete?

TRUNCATE is faster than DELETE , as it doesn't scan every record before removing it. TRUNCATE TABLE locks the whole table to remove data from a table; thus, this command also uses less transaction space than DELETE .

Is TRUNCATE reversible?

Since TRUNCATE is a DDL (data definition language) statement, it cannot be undone if it is not inside a transaction or if the transaction is COMMITTED.

Can we use TRUNCATE in transaction in SQL Server?

In SQL Server, you can rollback a TRUNCATE from a transaction. It does write page deallocation to the log, as you mentioned. Show activity on this post. In Oracle, TRUNCATE TABLE is a DDL statement that cannot be used in a transaction (or, more accurately, cannot be rolled back).


1 Answers

The database cleaning strategies refer to database terminology. I.e. those terms come from the (SQL) database world, so people generally familiar with database terminology will know what they mean.

The examples below refer to SQL definitions. DatabaseCleaner however supports other non-SQL types of databases too, but generally the definitions will be the same or similar.

Deletion

This means the database tables are cleaned using the SQL DELETE FROM statement. This is usually slower than truncation, but may have other advantages instead.

Truncation

This means the database tables are cleaned using the TRUNCATE TABLE statement. This will simply empty the table immediately, without deleting the table structure itself or deleting records individually.

Transaction

This means using BEGIN TRANSACTION statements coupled with ROLLBACK to roll back a sequence of previous database operations. Think of it as an "undo button" for databases. I would think this is the most frequently used cleaning method, and probably the fastest since changes need not be directly committed to the DB.

Example discussion: Rspec, Cucumber: best speed database clean strategy

Reason for truncation strategy with Capybara

The best explanation was found in the Capybara docs themselves:

# Transactional fixtures do not work with Selenium tests, because Capybara
# uses a separate server thread, which the transactions would be hidden
# from. We hence use DatabaseCleaner to truncate our test database.

Cleaning requirements

You do not necessarily have to clean your database after each test case. However you need to be aware of side effects this could have. I.e. if you create, modify, or delete some records in one step, will the other steps be affected by this?

Normally RSpec runs with transactional fixtures turned on, so you will never notice this when running RSpec - it will simply keep the database automatically clean for you:

https://www.relishapp.com/rspec/rspec-rails/v/2-10/docs/transactions

like image 111
Casper Avatar answered Nov 07 '22 10:11

Casper