sqlite 事务未显示在 test.log 中

发布于 2024-11-27 03:23:32 字数 1484 浏览 1 评论 0原文

我开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠北森屋 2024-12-04 03:23:32

ActiveRecord 3.0.9 不支持支持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 候选版本 (3.1.0.截至撰写本文时,rc5)确实支持保存点,因为 2011 年 1 月添加了支持

感谢 Rails 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

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.

-小熊_ 2024-12-04 03:23:32

事务未显示,因为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文