截断,事务和删除数据库策略之间的差异 [英] Difference between truncation, transaction and deletion database strategies

查看:262
本文介绍了截断,事务和删除数据库策略之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Rspec时,截断,事务和删除数据库策略有什么区别?我找不到任何资源解释这个。我读了数据库清理自述文件,但它不解释这些都做什么。

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.

为什么我们必须对Capybara使用截断策略?测试时,我必须清理我的数据库,或者我可以禁用它。我不明白为什么我应该在每个测试案例后清理我的数据库,不会只是减慢测试?

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?

推荐答案

数据库清理策略是指数据库术语。也就是说这些术语来自(SQL)数据库世界,因此通常熟悉数据库术语的人将知道它们的意思。

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.

下面的示例引用了SQL定义。 DatabaseCleaner 但是也支持其他非SQL类型的数据库,但一般定义将是相同或相似的。

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.

删除

这意味着使用SQL DELETE FROM 语句。这通常比慢于截断,但可能有其他优势

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.

截断

这意味着使用 TRUNCATE TABLE 语句清理数据库表。

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 individual records.

交易

这意味着使用 BEGIN TRANSACTION 语句以及 ROLLBACK 来回滚以前的数据库序列操作。将其视为数据库的撤消按钮。我认为这是最常用的清理方法,可能是最快的,因为更改不需要直接提交给数据库。

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.

示例讨论: Rspec,Cucumber:最快速度的数据库清理策略

Capybara截断原因的原因

Reason for truncation strategy with Capybara

最好的解释是在 Capybara文档自己

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

清洁要求

在每个测试用例之后,您不必清理数据库。然而,你需要知道这可能有副作用。也就是说如果您在一个步骤中创建,修改或删除一些记录,其他步骤是否会受此影响?

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?

通常,RSpec会在事务夹具打开的情况下运行,因此在运行RSpec时不会注意到这一点 - 它将简单地保持数据库自动为您清理:

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

这篇关于截断,事务和删除数据库策略之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆