Permalink
Browse files

Merge branch 'master' of git@github.com:rsim/oracle-enhanced

  • Loading branch information...
2 parents efb88d7 + 74389dd commit eaaf94b52447e2315f4b51465b42249bece3b30e @rsim rsim committed Jun 5, 2009
@@ -24,17 +24,19 @@ namespace :db do
end
namespace :test do
- redefine_task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
+ redefine_task :clone_structure => [ :rails_env, "db:structure:dump", "db:test:purge" ] do
+ env = RAILS_ENV ||= 'test'
abcs = ActiveRecord::Base.configurations
- ActiveRecord::Base.establish_connection(:test)
+ ActiveRecord::Base.establish_connection(env.to_sym)
IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl.chop)
end
end
- redefine_task :purge => :environment do
+ redefine_task :purge => [:rails_env, :environment] do
+ env = RAILS_ENV ||= 'test'
abcs = ActiveRecord::Base.configurations
- ActiveRecord::Base.establish_connection(:test)
+ ActiveRecord::Base.establish_connection(env.to_sym)
ActiveRecord::Base.connection.structure_drop.split("\n\n").each do |ddl|
ActiveRecord::Base.connection.execute(ddl.chop)
end
@@ -38,17 +38,30 @@ def include_with_custom_methods
module InstanceMethods
def self.included(base)
base.instance_eval do
- alias_method_chain :create, :custom_method
+ if private_instance_methods.include?('create_without_callbacks') || private_instance_methods.include?(:create_without_callbacks)
+ alias_method :create_without_custom_method, :create_without_callbacks
+ alias_method :create_without_callbacks, :create_with_custom_method
+ else
+ alias_method_chain :create, :custom_method
+ end
# insert after dirty checking in Rails 2.1
# in Ruby 1.9 methods names are returned as symbols
if private_instance_methods.include?('update_without_dirty') || private_instance_methods.include?(:update_without_dirty)
alias_method :update_without_custom_method, :update_without_dirty
alias_method :update_without_dirty, :update_with_custom_method
+ elsif private_instance_methods.include?('update_without_callbacks') || private_instance_methods.include?(:update_without_callbacks)
+ alias_method :update_without_custom_method, :update_without_callbacks
+ alias_method :update_without_callbacks, :update_with_custom_method
else
alias_method_chain :update, :custom_method
end
private :create, :update
- alias_method_chain :destroy, :custom_method
+ if public_instance_methods.include?('destroy_without_callbacks') || public_instance_methods.include?(:destroy_without_callbacks)
+ alias_method :destroy_without_custom_method, :destroy_without_callbacks
+ alias_method :destroy_without_callbacks, :destroy_with_custom_method
+ else
+ alias_method_chain :destroy, :custom_method
+ end
public :destroy
end
end
@@ -159,6 +159,26 @@ class ::TestEmployee < ActiveRecord::Base
@employee.update_time.should_not be_nil
end
+ it "should rollback record when exception is raised in after_create callback" do
+ @employee = TestEmployee.new(
+ :first_name => "First",
+ :last_name => "Last",
+ :hire_date => @today
+ )
+ TestEmployee.class_eval { def after_create() raise "Make the transaction rollback" end }
+ begin
+ employees_count = TestEmployee.count
+ @employee.save
+ fail "Did not raise exception"
+ rescue => e
+ e.message.should == "Make the transaction rollback"
+ @employee.id.should == nil
+ TestEmployee.count.should == employees_count
+ ensure
+ TestEmployee.class_eval { remove_method :after_create }
+ end
+ end
+
it "should update record" do
@employee = TestEmployee.create(
:first_name => "First",
@@ -173,6 +193,29 @@ class ::TestEmployee < ActiveRecord::Base
@employee.description.should == "Second Last"
end
+ it "should rollback record when exception is raised in after_update callback" do
+ TestEmployee.class_eval { def after_update() raise "Make the transaction rollback" end }
+ begin
+ @employee = TestEmployee.create(
+ :first_name => "First",
+ :last_name => "Last",
+ :hire_date => @today,
+ :description => "description"
+ )
+ empl_id = @employee.id
+ @employee.reload
+ @employee.first_name = "Second"
+ @employee.save!
+ fail "Did not raise exception"
+ rescue => e
+ e.message.should == "Make the transaction rollback"
+ @employee.reload
+ @employee.first_name.should == "First"
+ ensure
+ TestEmployee.class_eval { remove_method :after_update }
+ end
+ end
+
it "should not update record if nothing is changed and partial updates are enabled" do
return pending("Not in this ActiveRecord version") unless TestEmployee.respond_to?(:partial_updates=)
TestEmployee.partial_updates = true
@@ -214,6 +257,27 @@ class ::TestEmployee < ActiveRecord::Base
TestEmployee.find_by_employee_id(empl_id).should be_nil
end
+ it "should rollback record when exception is raised in after_desotry callback" do
+ TestEmployee.class_eval { def after_destroy() raise "Make the transaction rollback" end }
+ @employee = TestEmployee.create(
+ :first_name => "First",
+ :last_name => "Last",
+ :hire_date => @today
+ )
+ @employee.reload
+ empl_id = @employee.id
+ begin
+ @employee.destroy
+ fail "Did not raise exception"
+ rescue => e
+ e.message.should == "Make the transaction rollback"
+ @employee.id.should == empl_id
+ TestEmployee.find_by_employee_id(empl_id).should_not be_nil
+ ensure
+ TestEmployee.class_eval { remove_method :after_destroy }
+ end
+ end
+
it "should log create record" do
log_to @buffer
# reestablish plsql.connection as log_to might reset existing connection

0 comments on commit eaaf94b

Please sign in to comment.