diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-05-20 11:10:38 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-05-20 11:10:38 +0100 |
commit | 089251581137b041828a7e6dcbf75ecbef55b4a3 (patch) | |
tree | 6f39bcaf9c8c4ca8df27660277ed271eee2a64a0 | |
parent | 1b0128c5413ace12dee45e9db328cff866a5bb06 (diff) | |
download | rails-089251581137b041828a7e6dcbf75ecbef55b4a3.tar.gz rails-089251581137b041828a7e6dcbf75ecbef55b4a3.tar.bz2 rails-089251581137b041828a7e6dcbf75ecbef55b4a3.zip |
Ensure add_column gives valid error for sqlite. [Gunnar Wolf, Pratik] [#197 state:resolved]
SQLite#add_column executes "VACUUM", which fails if inside a live transaction. This patch
ensures a valid exception is raised if add_column is executed within a live transaction for
sqlite adapter.
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/transactions_test.rb | 26 |
2 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 8abbc6d0a4..51cfd10e5c 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -214,6 +214,10 @@ module ActiveRecord end def add_column(table_name, column_name, type, options = {}) #:nodoc: + if @connection.respond_to?(:transaction_active?) && @connection.transaction_active? + raise StatementInvalid, 'Cannot add columns to a SQLite database while inside a transaction' + end + super(table_name, column_name, type, options) # See last paragraph on http://www.sqlite.org/lang_altertable.html execute "VACUUM" diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb index 63f04e3014..06a76eacc3 100644 --- a/activerecord/test/cases/transactions_test.rb +++ b/activerecord/test/cases/transactions_test.rb @@ -179,6 +179,32 @@ class TransactionTest < ActiveRecord::TestCase end end + def test_sqlite_add_column_in_transaction_raises_statement_invalid + return true unless current_adapter?(:SQLite3Adapter, :SQLiteAdapter) + + # Test first if column creation/deletion works correctly when no + # transaction is in place. + # + # We go back to the connection for the column queries because + # Topic.columns is cached and won't report changes to the DB + + assert_nothing_raised do + Topic.reset_column_information + Topic.connection.add_column('topics', 'stuff', :string) + assert Topic.column_names.include?('stuff') + + Topic.reset_column_information + Topic.connection.remove_column('topics', 'stuff') + assert !Topic.column_names.include?('stuff') + end + + # Test now inside a transaction: add_column should raise a StatementInvalid + Topic.transaction do + assert_raises(ActiveRecord::StatementInvalid) { Topic.connection.add_column('topics', 'stuff', :string) } + raise ActiveRecord::Rollback + end + end + private def add_exception_raising_after_save_callback_to_topic Topic.class_eval { def after_save() raise "Make the transaction rollback" end } |