截断、事务和删除数据库策略的区别 [英] Difference between truncation, transaction and deletion database strategies

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

问题描述

使用Rspec时截断、事务和删除数据库策略有什么区别?我找不到任何资源来解释这一点.我阅读了 Database Cleaner 自述文件,但它没有解释每一个的作用.

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?

推荐答案

数据库清理策略是指数据库术语.IE.这些术语来自 (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 records individually.

交易

这意味着使用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 截断策略的原因

最好的解释是在 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.

清洁要求

您不必在每个测试用例之后清理数据库.但是,您需要注意这可能产生的副作用.IE.如果您在一个步骤中创建、修改或删除某些记录,其他步骤是否会受此影响?

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天全站免登陆