sqlite 事务未显示在 test.log 中
我开始使用 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
也没有任何交易的迹象 code> 在我的 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
I started developing my rails application using sqlite. I have reached a point where I need to make sure that some code runs under a database transaction which is something new for me. To begin the journey, I wan't to see the transactions being used in every test case. After all, I'll need to disable them for testing this feature.
When I see the test.log for any one of my specs, I get something like this in 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')
No sign of any transaction even though I have config.use_transactional_fixtures = true
in my spec_helper.rb
If change my database to MySQL, the same test produces this output in 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
So? What is going on here? Why are the transactions not shown for sqlite? My understanding is that sqlite transactions are supported in rails.
My versions: 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ActiveRecord 3.0.9 不支持支持sqlite保存点:
最新3.1 候选版本 (3.1.0.截至撰写本文时,rc5)确实支持保存点,因为 2011 年 1 月添加了支持。
感谢 Rails 3. 嵌套事务。子块中的异常有助于回答这个问题。
ActiveRecord 3.0.9 does not support savepoints for sqlite:
The latest 3.1 release candidate (3.1.0.rc5 as of this writing) does support savepoints because support was added in January 2011.
Thanks to Rails 3. Nested transactions. Exception in a child block for helping answer this question.
事务未显示,因为
sqlite_adapter.rb
中缺少执行此操作的代码。现在已修复在 Rails 主干中:https://github.com/rails/rails/commit/cdb49fc2f3f66bbae81be837424dcb45602ea5e2
The transactions were not showing up because the code for doing so was missing in the
sqlite_adapter.rb
. This is now fixed in the rails trunk:https://github.com/rails/rails/commit/cdb49fc2f3f66bbae81be837424dcb45602ea5e2