knockoff 1.0 → 1.1.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd0b434b18fd1ad5b383fa9bc40fcaf77e04b79e
4
- data.tar.gz: 17cee20d59d0cb6099838c0efaac845545e6af55
3
+ metadata.gz: b9352faddb2bf60d2ddf6f3c91c9e03bce498a41
4
+ data.tar.gz: c4bb57f7a8b240d71ca164d489de9b26535ce53b
5
5
  SHA512:
6
- metadata.gz: 6ccbdee78647fea3b3e64b581e7c4dca073bf5428834067e37b4d15d0fb64237dc53f66361a639ad4f535e9511cd0820caf04415269dc9f05acb6cd8e8cef823
7
- data.tar.gz: 9667d13bf4bcaec1c72898c63a7b9a446eade2f290b18bbef282bc6d757c88dfabb6dde5c4d4ea477ece150eb26f5878883b71f8181ae6862d9c6edeb8376782
6
+ metadata.gz: 13d2338a6425fe9eea71c30b8b838f7df6fe9c69e369193b67bd3be59c739062a8f0515f7cc28c2846e0df743d9745467b5e3b5e3cf2bfdbeb680719c2bc4efd
7
+ data.tar.gz: ffda5a9534ca24f6dfb85d59ea3a225bd5759a50eaa94f71ae869396abf7d7806fa05583108adfbd240abb0c5356b2d9b3bf241af0dc0f59fb3c0edeab906d1a
@@ -2,7 +2,7 @@ language: ruby
2
2
  notifications:
3
3
  email: false
4
4
  rvm:
5
- - 2.2.4
6
5
  - 2.3.1
7
6
  - 2.4.0
7
+ - 2.5.1
8
8
  - ruby-head
@@ -0,0 +1,6 @@
1
+ ## Unreleased
2
+
3
+ - Allow for not checking `inside_transaction?` on primary database (https://github.com/joinhandshake/knockoff/pull/13)
4
+ - Allow setting `Knockoff.default_target` to set the default target other than `:primary` (https://github.com/joinhandshake/knockoff/pull/11)
5
+ - Drop Ruby 2.2 support
6
+ - Add Ruby 2.5 support
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Knockoff
2
2
 
3
- [![Build Status](https://travis-ci.org/sgringwe/knockoff.svg?branch=master)](https://travis-ci.org/sgringwe/knockoff)
3
+ [![Build Status](https://travis-ci.org/joinhandshake/knockoff.svg?branch=master)](https://travis-ci.org/joinhandshake/knockoff)
4
4
  [![Gem Version](https://badge.fury.io/rb/knockoff.svg)](https://badge.fury.io/rb/knockoff)
5
5
 
6
6
  A gem for easily using read replicas.
@@ -188,10 +188,6 @@ There are likely other cases specific to each application where it makes sense t
188
188
 
189
189
  * Do not use prepared statements with this gem
190
190
 
191
- ## Inspirations
192
-
193
- Based off of and inspired by https://github.com/kenn/slavery and https://github.com/kickstarter/replica_pools gem.
194
-
195
191
  ## Development
196
192
 
197
193
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -200,7 +196,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
200
196
 
201
197
  ## Contributing
202
198
 
203
- Bug reports and pull requests are welcome on GitHub at https://github.com/sgringwe/knockoff.
199
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joinhandshake/knockoff.
204
200
 
205
201
  ## License
206
202
 
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_runtime_dependency 'activerecord', '>= 4.0.0'
22
22
  spec.add_runtime_dependency 'activesupport', '>= 4.0.0'
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.14.5"
24
+ spec.add_development_dependency "bundler"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec"
27
27
  spec.add_development_dependency 'sqlite3'
@@ -10,15 +10,20 @@ require 'knockoff/active_record/relation'
10
10
  module Knockoff
11
11
  class << self
12
12
  attr_accessor :enabled
13
+ attr_reader :default_target
13
14
 
14
- def on_replica(&block)
15
- Base.new(:replica).run(&block)
15
+ def on_replica(check_transaction: true, &block)
16
+ Base.new(:replica, check_transaction: check_transaction).run(&block)
16
17
  end
17
18
 
18
19
  def on_primary(&block)
19
20
  Base.new(:primary).run(&block)
20
21
  end
21
22
 
23
+ def default_target=(target)
24
+ @default_target = Base.new(target).target
25
+ end
26
+
22
27
  def replica_pool
23
28
  @replica_pool ||= ReplicaConnectionPool.new(config.replica_database_keys)
24
29
  end
@@ -4,7 +4,8 @@ module ActiveRecord
4
4
  alias_method :original_connection, :connection
5
5
 
6
6
  def connection
7
- case Thread.current[:knockoff]
7
+ target = Thread.current[:knockoff] || Knockoff.default_target
8
+ case target
8
9
  when :replica
9
10
  # Attempts to use a random replica connection, but otherwise falls back to primary
10
11
  Knockoff.replica_pool.random_replica_connection.original_connection
@@ -1,7 +1,9 @@
1
1
  module Knockoff
2
2
  class Base
3
- def initialize(target)
4
- @target = decide_with(target)
3
+ attr_reader :target
4
+
5
+ def initialize(target, check_transaction: true)
6
+ @target = decide_with(target, check_transaction)
5
7
  end
6
8
 
7
9
  def run(&block)
@@ -10,7 +12,7 @@ module Knockoff
10
12
 
11
13
  private
12
14
 
13
- def decide_with(target)
15
+ def decide_with(target, check_transaction)
14
16
  calculated_target =
15
17
  if Knockoff.enabled
16
18
  target
@@ -19,7 +21,9 @@ module Knockoff
19
21
  end
20
22
 
21
23
  # Don't allow setting the target to anything other than primary if we are already in a transaction
22
- raise Knockoff::Error.new('on_replica cannot be used inside transaction block!') if calculated_target != :primary && inside_transaction?
24
+ if calculated_target != :primary && check_transaction && inside_transaction?
25
+ raise Knockoff::Error.new('on_replica cannot be used inside transaction block!')
26
+ end
23
27
  calculated_target
24
28
  end
25
29
 
@@ -1,3 +1,3 @@
1
1
  module Knockoff
2
- VERSION = '1.0'.freeze
2
+ VERSION = '1.1.0.alpha'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knockoff
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.1.0.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Ringwelski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-26 00:00:00.000000000 Z
11
+ date: 2018-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 1.14.5
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 1.14.5
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -104,6 +104,7 @@ files:
104
104
  - ".gitignore"
105
105
  - ".rspec"
106
106
  - ".travis.yml"
107
+ - CHANGELOG.md
107
108
  - Gemfile
108
109
  - LICENSE.txt
109
110
  - README.md
@@ -135,14 +136,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
136
  version: '0'
136
137
  required_rubygems_version: !ruby/object:Gem::Requirement
137
138
  requirements:
138
- - - ">="
139
+ - - ">"
139
140
  - !ruby/object:Gem::Version
140
- version: '0'
141
+ version: 1.3.1
141
142
  requirements: []
142
143
  rubyforge_project:
143
- rubygems_version: 2.4.8
144
+ rubygems_version: 2.6.13
144
145
  signing_key:
145
146
  specification_version: 4
146
147
  summary: A gem for easily using read replicas
147
148
  test_files: []
148
- has_rdoc: