diff options
author | Timur Alperovich <timur@maginatics.com> | 2014-01-08 15:08:25 -0800 |
---|---|---|
committer | Timur Alperovich <timur@maginatics.com> | 2014-01-15 10:35:21 -0800 |
commit | 6f16513b587a0805a23eab99d5f283476b1d6687 (patch) | |
tree | 5140b1d17ebd2764af7e7bd9a2434cf70360d8db /activerecord | |
parent | 8d146c8bec3a33052116ca5730151dc44d766222 (diff) | |
download | rails-6f16513b587a0805a23eab99d5f283476b1d6687.tar.gz rails-6f16513b587a0805a23eab99d5f283476b1d6687.tar.bz2 rails-6f16513b587a0805a23eab99d5f283476b1d6687.zip |
SQLite3: Always close statements.
SQLite3 adapter must make sure to close statements after queries.
Fixes: #13631
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb | 15 |
3 files changed, 27 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 5c5dd99ab7..27ad5a2f84 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Ensure SQLite3 statements are closed on errors. + + Fixes: #13631 + + *Timur Alperovich* + * Enable partial indexes for sqlite >= 3.8.0 See http://www.sqlite.org/partialindex.html diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 170dddb08e..b40d7b3a58 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -299,9 +299,12 @@ module ActiveRecord # Don't cache statements if they are not prepared if without_prepared_statement?(binds) stmt = @connection.prepare(sql) - cols = stmt.columns - records = stmt.to_a - stmt.close + begin + cols = stmt.columns + records = stmt.to_a + ensure + stmt.close + end stmt = records else cache = @statements[sql] ||= { diff --git a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb index 02834edf7b..1a5c3e7aba 100644 --- a/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +++ b/activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb @@ -401,6 +401,21 @@ module ActiveRecord assert @conn.respond_to?(:disable_extension) end + def test_statement_closed + db = SQLite3::Database.new(ActiveRecord::Base. + configurations['arunit']['database']) + statement = SQLite3::Statement.new(db, + 'CREATE TABLE statement_test (number integer not null)') + statement.stubs(:step).raises(SQLite3::BusyException, 'busy') + statement.stubs(:columns).once.returns([]) + statement.expects(:close).once + SQLite3::Statement.stubs(:new).returns(statement) + + assert_raise ActiveRecord::StatementInvalid do + @conn.exec_query 'select * from statement_test' + end + end + private def assert_logged logs |