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 i开发者_如何学Gon 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
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.
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
精彩评论