From 77eb5299bddb7a41b731adffe9c4f9a893784994 Mon Sep 17 00:00:00 2001 From: Alexander Starovojtov <37301326+AS-AlStar@users.noreply.github.com> Date: Thu, 2 Dec 2021 13:54:40 +0300 Subject: [PATCH] Skip publish on destroy for non persisted object in AR (#62) * Skip publish when object is new and event destroy * Replace return to next * Fix rubocop * Add spec * Fix docs * Fix version * Fix docs * bundle * Fix docs and specs * fix spec --- CHANGELOG.md | 4 ++++ Gemfile.lock | 16 +++++++++------- docs/publishing.md | 19 ++++++++++++++++++- lib/table_sync/setup/active_record.rb | 2 ++ lib/table_sync/version.rb | 2 +- spec/setup/active_record_spec.rb | 11 +++++++++++ spec/setup/sequel_spec.rb | 11 +++++++++++ spec/support/shared/setup.rb | 11 +++++++++++ 8 files changed, 67 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2143cb0..40a967f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. +## [6.0.2] - 2021-12-01 +### Fixed +- Fixed bug: skip publish when object is new and event is destroy for ActiveRecord + ## [6.0.1] - 2021-11-30 ### Fixed - fixed docs diff --git a/Gemfile.lock b/Gemfile.lock index 4051ab7..cceab65 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - table_sync (6.0.1) + table_sync (6.0.2) memery rabbit_messaging rails @@ -91,7 +91,7 @@ GEM activesupport (>= 5.0) i18n (1.8.10) concurrent-ruby (~> 1.0) - lamian (1.2.0) + lamian (1.4.0) rails (>= 4.2) loofah (2.12.0) crass (~> 1.0.2) @@ -103,9 +103,11 @@ GEM ruby2_keywords (~> 0.0.2) method_source (1.0.0) mini_mime (1.1.2) + mini_portile2 (2.6.1) minitest (5.14.4) nio4r (2.5.8) - nokogiri (1.12.5-x86_64-linux) + nokogiri (1.12.5) + mini_portile2 (~> 2.6.1) racc (~> 1.4) parallel (1.20.1) parser (3.0.0.0) @@ -207,7 +209,7 @@ GEM sequel (5.43.0) serverengine (2.0.7) sigdump (~> 0.2.2) - set (1.0.1) + set (1.0.2) sigdump (0.2.4) simplecov (0.21.2) docile (~> 1.1) @@ -228,9 +230,9 @@ GEM sprockets (4.0.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.2) - actionpack (>= 4.0) - activesupport (>= 4.0) + sprockets-rails (3.4.1) + actionpack (>= 5.2) + activesupport (>= 5.2) sprockets (>= 3.0.0) tainbox (2.1.2) activesupport diff --git a/docs/publishing.md b/docs/publishing.md index 5a03f15..b61941a 100644 --- a/docs/publishing.md +++ b/docs/publishing.md @@ -41,6 +41,23 @@ class SomeOtherModel < Sequel::Model end ``` +### Non persisted record destruction + +Sometimes destroy event can happen for non persisted record. In this case we can expect the following: + +For Sequel: 'Sequel::NoExistingObject' is raised. (This is default Sequel behaviour) +For Active Record: Publishing is skipped. + +Example: + +```ruby + # ActiveRecord + user = User.new.destroy! # Publishing is skipped. + + # Sequel + user = User.new.destroy! # raise Sequel::NoExistingObject +``` + ## Manual Directly call one of the publishers. It's the best if you need to sync a lot of data. @@ -60,4 +77,4 @@ Example: - [Publishers](publishing/publishers.md) - [Configuration](publishing/configuration.md) -- [Manual Sync (examples)](publishing/manual.md) \ No newline at end of file +- [Manual Sync (examples)](publishing/manual.md) diff --git a/lib/table_sync/setup/active_record.rb b/lib/table_sync/setup/active_record.rb index 4101357..55f49cc 100644 --- a/lib/table_sync/setup/active_record.rb +++ b/lib/table_sync/setup/active_record.rb @@ -8,6 +8,8 @@ def define_after_commit(event) options = options_exposed_for_block object_class.after_commit(on: event) do + next if new_record? && destroyed? + if instance_eval(&options[:if]) && !instance_eval(&options[:unless]) TableSync::Publishing::Single.new( object_class: self.class.name, diff --git a/lib/table_sync/version.rb b/lib/table_sync/version.rb index 24de597..7053f00 100644 --- a/lib/table_sync/version.rb +++ b/lib/table_sync/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module TableSync - VERSION = "6.0.1" + VERSION = "6.0.2" end diff --git a/spec/setup/active_record_spec.rb b/spec/setup/active_record_spec.rb index 7c974f8..7b64673 100644 --- a/spec/setup/active_record_spec.rb +++ b/spec/setup/active_record_spec.rb @@ -26,4 +26,15 @@ def setup_sync(options = {}) setup_sync end end + + context "when event destroy" do + before { setup_sync } + + context "when user is new record" do + specify do + expect(job).not_to receive(:perform_at) + test_class.new.destroy + end + end + end end diff --git a/spec/setup/sequel_spec.rb b/spec/setup/sequel_spec.rb index 4f917ce..68a3498 100644 --- a/spec/setup/sequel_spec.rb +++ b/spec/setup/sequel_spec.rb @@ -27,4 +27,15 @@ def setup_sync(options = {}) setup_sync end end + + context "when event destroy" do + before { setup_sync } + + context "when user is new record" do + specify do + expect(job).not_to receive(:perform_at) + expect { test_class.new.destroy }.to raise_error(Sequel::NoExistingObject) + end + end + end end diff --git a/spec/support/shared/setup.rb b/spec/support/shared/setup.rb index 992f8cb..a0fa4dc 100644 --- a/spec/support/shared/setup.rb +++ b/spec/support/shared/setup.rb @@ -64,4 +64,15 @@ include_examples "doesn't enqueue job" end end + + context "when event destroy" do + before { setup_sync } + + context "when user is persisted" do + specify do + expect(job).to receive(:perform_at) + test_class.first.destroy + end + end + end end