activerecord-multi-tenant 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d6ecfbf3c596f4c97db44220ae47b9b17370060
4
- data.tar.gz: a13da12778bcc68651a52fd03f25b5e99f32fee4
3
+ metadata.gz: 0692994e7b629c802155cdf7314b1b9178ec8897
4
+ data.tar.gz: 07e223afb891884df3e0f5cdf5b4d17149eb76fa
5
5
  SHA512:
6
- metadata.gz: 40aab782eaa7eaf613f576febca765da3942d7678fe3ee3bad99fb7b0aa495dcb3bbac651d33eea5d5807461b42c934329a489a74d9c77691a310965ac1e7429
7
- data.tar.gz: 0ff0d1fbf17c30c5730ec24e8f8274a0367af4382f790d67f97908c8ea4623676c312f6f3b14788237ab8698a41128ec84c5fcc4310902a51316d5ccac936d76
6
+ metadata.gz: da7f603099dab7a4c87492e26d4e602c7f11eb5cdd93bed4d9e8573b437cffd178e9380759d1e8934e28e87369fa0d7baca723dafd73202e2cf712f7128279c4
7
+ data.tar.gz: d51a45f8c081e5d9385c7a5e248efe398337028f497db8c8220cd56b096da9369e23e134a116d3f3fb0cada2e412dc52da24b0cc3688a3da98c6b738be4c9cb5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.2 2017-02-16
4
+
5
+ * Support blocks passed into the unscoped method (this fixes reload, amongst other issues)
6
+ * Make with_lock work by adding workaround for Citus #1236 (SELECT ... FOR UPDATE is not router-plannable)
7
+
8
+
3
9
  ## 0.3.1 2017-02-13
4
10
 
5
11
  * Rails 5 API controller support [@mstahl](https://github.com/mstahl) [#4](https://github.com/citusdata/activerecord-multi-tenant/pull/4)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- activerecord-multi-tenant (0.3.1)
4
+ activerecord-multi-tenant (0.3.2)
5
5
  rails (>= 3.1)
6
6
  request_store (>= 1.0.5)
7
7
 
@@ -6,3 +6,4 @@ require 'activerecord-multi-tenant/model_extensions'
6
6
  require 'activerecord-multi-tenant/multi_tenant'
7
7
  require 'activerecord-multi-tenant/referential_integrity'
8
8
  require 'activerecord-multi-tenant/version'
9
+ require 'activerecord-multi-tenant/with_lock'
@@ -2,11 +2,13 @@ class ActiveRecord::Base
2
2
  class << self
3
3
  alias :unscoped_orig :unscoped
4
4
  def unscoped
5
- if respond_to?(:scoped_by_tenant?) && MultiTenant.current_tenant_id
5
+ scope = if respond_to?(:scoped_by_tenant?) && MultiTenant.current_tenant_id
6
6
  unscoped_orig.where(arel_table[self.partition_key].eq(MultiTenant.current_tenant_id))
7
7
  else
8
8
  unscoped_orig
9
9
  end
10
+
11
+ block_given? ? scope.scoping { yield } : scope
10
12
  end
11
13
  end
12
14
  end
@@ -15,6 +15,11 @@ module MultiTenant
15
15
  "#{@@tenant_klass.to_s}_id"
16
16
  end
17
17
 
18
+ # Workaroud to make "with_lock" work until https://github.com/citusdata/citus/issues/1236 is fixed
19
+ @@enable_with_lock_workaround = false
20
+ def self.enable_with_lock_workaround; @@enable_with_lock_workaround = true; end
21
+ def self.with_lock_workaround_enabled?; @@enable_with_lock_workaround; end
22
+
18
23
  def self.current_tenant=(tenant)
19
24
  RequestStore.store[:current_tenant] = tenant
20
25
  end
@@ -1,5 +1,5 @@
1
- # Disable Rails trigger enable/disable mechanism used for test cases, since
2
- # DISABLE TRIGGER is not supported on distributed tables.
1
+ # Workaround for https://github.com/citusdata/citus/issues/1080
2
+ # "Support DISABLE/ENABLE TRIGGER ALL on distributed tables"
3
3
 
4
4
  module ActiveRecord
5
5
  module ConnectionAdapters
@@ -1,3 +1,3 @@
1
1
  module MultiTenant
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
@@ -0,0 +1,17 @@
1
+ # Workaround for https://github.com/citusdata/citus/issues/1236
2
+ # "SELECT ... FOR UPDATE is not supported for router-plannable queries"
3
+
4
+ class ActiveRecord::Base
5
+ alias :with_lock_orig :with_lock
6
+ def with_lock(&block)
7
+ if self.class.respond_to?(:scoped_by_tenant?) && MultiTenant.current_tenant_id && MultiTenant.with_lock_workaround_enabled?
8
+ transaction do
9
+ self.class.unscoped.where(id: id).update_all(id: id) # No-op UPDATE that locks the row
10
+ reload # This is just to act similar to the default Rails approach, in case someone relies on the reload
11
+ yield
12
+ end
13
+ else
14
+ with_lock_orig(&block)
15
+ end
16
+ end
17
+ end
@@ -149,5 +149,28 @@ describe MultiTenant do
149
149
 
150
150
  expect(value).to eq "something"
151
151
  end
152
+
153
+ it 'supports reload inside the block' do
154
+ @account = Account.create!(name: 'foo')
155
+
156
+ MultiTenant.with @account do
157
+ project = @account.projects.create!(name: 'project')
158
+ project.reload
159
+ expect(project.name).to eq 'project'
160
+ end
161
+ end
162
+ end
163
+
164
+ describe '.with_lock' do
165
+ it 'supports with_lock blocks inside the block' do
166
+ @account = Account.create!(name: 'foo')
167
+
168
+ MultiTenant.with @account do
169
+ project = @account.projects.create!(name: 'project')
170
+ project.with_lock do
171
+ expect(project.name).to eq 'project'
172
+ end
173
+ end
174
+ end
152
175
  end
153
176
  end
data/spec/spec_helper.rb CHANGED
@@ -23,6 +23,9 @@ RSpec.configure do |config|
23
23
  config.before(:suite) do
24
24
  DatabaseCleaner[:active_record].strategy = :transaction
25
25
  DatabaseCleaner[:active_record].clean_with(:truncation)
26
+
27
+ # Keep this here until https://github.com/citusdata/citus/issues/1236 is fixed in a patch release we can run tests with
28
+ MultiTenant.enable_with_lock_workaround
26
29
  end
27
30
 
28
31
  config.before(:each) do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-multi-tenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Citus Data
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-14 00:00:00.000000000 Z
11
+ date: 2017-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: request_store
@@ -152,6 +152,7 @@ files:
152
152
  - lib/activerecord-multi-tenant/multi_tenant.rb
153
153
  - lib/activerecord-multi-tenant/referential_integrity.rb
154
154
  - lib/activerecord-multi-tenant/version.rb
155
+ - lib/activerecord-multi-tenant/with_lock.rb
155
156
  - spec/activerecord-multi-tenant/controller_extensions_spec.rb
156
157
  - spec/activerecord-multi-tenant/model_extensions_spec.rb
157
158
  - spec/activerecord-multi-tenant/record_callback_spec.rb