sqlite事务没有显示在test.log中

时间:2011-07-31 22:04:24

标签: mysql ruby-on-rails sqlite transactions rspec

我开始使用sqlite开发我的rails应用程序。我已经达到了一个点,我需要确保一些代码在数据库事务下运行,这对我来说是新的。为了开始这个旅程,我不想看到每个测试用例中使用的事务。毕竟,我需要禁用它们来测试此功能。

当我看到test.log中的任何一个规格时,我在test.log中得到了类似的内容:

SQL (0.2ms)  SELECT 1 FROM "users" WHERE ("users"."login" = 'jaimito1') LIMIT 1
SQL (0.2ms)   SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
AREL (0.5ms)  INSERT INTO "users" ("login", "password_digest", "created_at", "updated_at") VALUES ('jaimito1', '$2a$10$lJ/qIMfKymDmKuY6fYQF.u3bg8/ZdIzGU04MsjC6vJk8qygMEiemC', '2011-07-31 21:32:05.886797', '2011-07-31 21:32:05.886797')

即使我的config.use_transactional_fixtures = true

中有spec_helper.rb,也没有任何交易迹象

如果将我的数据库更改为MySQL,则相同的测试会在test.log中生成此输出:

SQL (0.2ms)  BEGIN
SQL (0.2ms)  SAVEPOINT active_record_1
SQL (0.3ms)  SELECT 1 FROM `users` WHERE (`users`.`login` = BINARY 'jaimito1') LIMIT 1
SQL (5.9ms)  SHOW TABLES
SQL (1.2ms)  describe `users`
AREL (0.3ms)  INSERT INTO `users` (`login`, `password_digest`, `created_at`, `updated_at`) VALUES ('jaimito1', '$2a$10$Ujsv0XGRLyHBZsI7sY4Vf.jv3bm1fyfeueV79o91gDY9xbdc7KUGC', '2011-07-31 19:50:28', '2011-07-31 19:50:28')
SQL (0.1ms)  RELEASE SAVEPOINT active_record_1
SQL (0.7ms)  ROLLBACK

所以?这里发生了什么?为什么没有为sqlite显示事务?我的理解是rails中支持sqlite事务。

我的版本:Mac OS X 10.6.8,Rails 3.0.9,sqlite 3.7.6,sqlite3 gem 1.3.3,ruby 1.9.2p290,mysql 5.0.91

2 个答案:

答案 0 :(得分:3)

ActiveRecord 3.0.9 does not support savepoints for sqlite

# activerecord-3.0.9/lib/active_record/connection_adapters/abstract_adapter.rb
# Does this adapter support savepoints? PostgreSQL and MySQL do, SQLite does not.
def supports_savepoints?
  false
end

最新3.1 release candidate (3.1.0.rc5 as of this writing) does support savepoints因为support was added in January 2011

感谢Rails 3. Nested transactions. Exception in a child block帮助回答了这个问题。

答案 1 :(得分:0)

交易没有显示,因为sqlite_adapter.rb中缺少这样做的代码。现在这已在rails trunk中修复:

https://github.com/rails/rails/commit/cdb49fc2f3f66bbae81be837424dcb45602ea5e2