switch_point 0.4.0 → 0.4.1

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: d745412c8403805b7d7221bd0873e96156fbf117
4
- data.tar.gz: a3ebbfe8088491a63e86525f6e10e045c4c7b90e
3
+ metadata.gz: b1f2d543d57d381bd1f30ada661a2bd0af01de56
4
+ data.tar.gz: 18b2322fdead64b792dbc5bde8ea8b4a08c38ffe
5
5
  SHA512:
6
- metadata.gz: 0288d5f1aee3e3a1bbe3e7816f728628f795f0903fa0ce32b1b96bfe54284b4fcd20260f3dbdd2b8fbdb93fee24779b6921f0fce30d13a5b5232b7e92402fb12
7
- data.tar.gz: d58730ca9ec1cae341942d644600d87b523cadf64b3ce3d78ee65791ebb3c4c9f487843b524f9ec194b18a857ca7220a53dd05a72a18b6d193268eec36052dff
6
+ metadata.gz: 276be82faa7bdb01ab7c15959c6ef226433c5296ebfc4cf3e3b081ef3e162bf3b342c1ced86ed72985589a5d447a049bf04f4a44595ab1cd63e6be605d129a1a
7
+ data.tar.gz: 5affb3b76703ee30e84714e388a9c742c5444c66e5ffd71be91e48b2dcb206831172c520bea7ffae9b833c62725f1091877b4dd0a036fc5df254549aefd15b0f
@@ -1,3 +1,6 @@
1
+ ## 0.4.1 (2014-06-19)
2
+ - Support :writable only configuration
3
+
1
4
  ## 0.4.0 (2014-06-17)
2
5
  - auto_writable is disabled by default
3
6
  - To restore the previous behavior, set `config.auto_writable = true`.
data/README.md CHANGED
@@ -34,7 +34,7 @@ Suppose `after_save` callback is set to User model. When `User.create` is called
34
34
 
35
35
  1. BEGIN TRANSACTION is sent to READONLY connection.
36
36
  2. switch_point switches the connection to WRITABLE.
37
- 3. CREATE statement is sent to WRITABLE connection.
37
+ 3. INSERT statement is sent to WRITABLE connection.
38
38
  4. switch_point reset the connection to READONLY.
39
39
  5. after_save callback is called.
40
40
  - At this point, the connection is READONLY and in a transaction.
@@ -44,11 +44,13 @@ module SwitchPoint
44
44
  private
45
45
 
46
46
  def assert_valid_config!(config)
47
- unless config.has_key?(:readonly)
48
- raise ArgumentError.new(":readonly key is required")
47
+ unless config.has_key?(:readonly) || config.has_key?(:writable)
48
+ raise ArgumentError.new(':readonly or :writable must be specified')
49
49
  end
50
- unless config[:readonly].is_a?(Symbol)
51
- raise TypeError.new(":readonly's value must be Symbol")
50
+ if config.has_key?(:readonly)
51
+ unless config[:readonly].is_a?(Symbol)
52
+ raise TypeError.new(":readonly's value must be Symbol")
53
+ end
52
54
  end
53
55
  if config.has_key?(:writable)
54
56
  unless config[:writable].is_a?(Symbol)
@@ -2,7 +2,7 @@ module SwitchPoint
2
2
  class Proxy
3
3
  attr_reader :initial_name
4
4
 
5
- AVAILABLE_MODES = [:readonly, :writable]
5
+ AVAILABLE_MODES = [:writable, :readonly]
6
6
  DEFAULT_MODE = :readonly
7
7
 
8
8
  def initialize(name)
@@ -16,13 +16,18 @@ module SwitchPoint
16
16
  end
17
17
 
18
18
  def define_model(name, mode)
19
- model = Class.new(ActiveRecord::Base)
20
19
  model_name = SwitchPoint.config.model_name(name, mode)
21
20
  if model_name
21
+ model = Class.new(ActiveRecord::Base)
22
22
  Proxy.const_set(model_name, model)
23
23
  model.establish_connection(SwitchPoint.config.database_name(name, mode))
24
+ model
25
+ elsif mode == :readonly
26
+ # Re-use writable connection
27
+ Proxy.const_get(SwitchPoint.config.model_name(name, :writable))
28
+ else
29
+ Class.new(ActiveRecord::Base)
24
30
  end
25
- model
26
31
  end
27
32
 
28
33
  def memorize_switch_point(name, mode, connection)
@@ -35,6 +40,8 @@ module SwitchPoint
35
40
  switch_points = pool.instance_variable_get(:@switch_points) || []
36
41
  switch_points << switch_point
37
42
  pool.instance_variable_set(:@switch_points, switch_points)
43
+ elsif pool.instance_variable_defined?(:@switch_point)
44
+ # Only :writable is specified
38
45
  else
39
46
  pool.instance_variable_set(:@switch_point, switch_point)
40
47
  end
@@ -119,6 +126,11 @@ module SwitchPoint
119
126
  model_name = SwitchPoint.config.model_name(@current_name, mode)
120
127
  if model_name
121
128
  Proxy.const_get(model_name).connection
129
+ elsif mode == :readonly
130
+ # When only writable is specified, re-use writable connection.
131
+ with_writable do
132
+ connection
133
+ end
122
134
  else
123
135
  ActiveRecord::Base.connection
124
136
  end
@@ -1,3 +1,3 @@
1
1
  module SwitchPoint
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -15,6 +15,8 @@ SwitchPoint.configure do |config|
15
15
  readonly: :main_readonly
16
16
  config.define_switch_point :nanika2,
17
17
  readonly: :main_readonly
18
+ config.define_switch_point :nanika3,
19
+ writable: :comment_writable
18
20
  end
19
21
 
20
22
  require 'active_record'
@@ -56,6 +58,10 @@ class Nanika2 < ActiveRecord::Base
56
58
  use_switch_point :nanika2
57
59
  end
58
60
 
61
+ class Nanika3 < ActiveRecord::Base
62
+ use_switch_point :nanika3
63
+ end
64
+
59
65
  base = { adapter: 'sqlite3' }
60
66
  ActiveRecord::Base.configurations = {
61
67
  'main_readonly' => base.merge(database: 'main_readonly.sqlite3'),
@@ -41,6 +41,8 @@ RSpec.configure do |config|
41
41
  FileUtils.cp('main_writable.sqlite3', 'main_readonly.sqlite3')
42
42
 
43
43
  Note.connection.execute('CREATE TABLE notes (id integer primary key autoincrement)')
44
+
45
+ Nanika3.connection.execute('CREATE TABLE nanika3s (id integer primary key)')
44
46
  end
45
47
 
46
48
  config.after(:suite) do
@@ -54,6 +56,8 @@ RSpec.configure do |config|
54
56
  Book.delete_all
55
57
  end
56
58
  FileUtils.cp('main_writable.sqlite3', 'main_readonly.sqlite3')
59
+
60
+ Nanika3.delete_all
57
61
  end
58
62
  end
59
63
 
@@ -117,6 +117,18 @@ RSpec.describe SwitchPoint::Model do
117
117
  Note.create
118
118
  end
119
119
  end
120
+
121
+ context 'without :readonly' do
122
+ it 'sends all queries to :writable' do
123
+ expect(Nanika3).to connect_to('comment_writable.sqlite3')
124
+ Nanika3.with_writable do
125
+ expect(Nanika3).to connect_to('comment_writable.sqlite3')
126
+ Nanika3.create
127
+ end
128
+ expect(Nanika3.count).to eq(1)
129
+ expect(Nanika3.with_readonly { Nanika3.connection }).to equal(Nanika3.with_writable { Nanika3.connection })
130
+ end
131
+ end
120
132
  end
121
133
 
122
134
  describe '.with_writable' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switch_point
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Suzuki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-17 00:00:00.000000000 Z
11
+ date: 2014-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal