db-charmer 1.6.16 → 1.6.17

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,10 @@
1
+ 1.6.15 (2011-04-25):
2
+
3
+ Bugfixes: Do not touch database for sharded models until we really need to. Before 1.6.15
4
+ if a database server was dead and there were any other connection issues, class loading
5
+ in Ruby would be broken and sharded model class would not be initialized.
6
+
7
+ ----------------------------------------------------------------------------------------
1
8
  1.6.14 (2011-01-09):
2
9
 
3
10
  Bugfixes: We do not support Rails 3, and now we prohibit any versions but 2.2 and 2.3 from
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.16
1
+ 1.6.17
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{db-charmer}
8
- s.version = "1.6.16"
8
+ s.version = "1.6.17"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexey Kovyrin"]
12
- s.date = %q{2011-02-28}
12
+ s.date = %q{2011-04-25}
13
13
  s.description = %q{ActiveRecord Connections Magic (slaves, multiple connections, etc)}
14
14
  s.email = %q{alexey@kovyrin.net}
15
15
  s.extra_rdoc_files = [
@@ -45,7 +45,7 @@ Gem::Specification.new do |s|
45
45
  "lib/db_charmer/sharding/method/db_block_map.rb",
46
46
  "lib/db_charmer/sharding/method/hash_map.rb",
47
47
  "lib/db_charmer/sharding/method/range.rb",
48
- "lib/db_charmer/stub_connection.rb",
48
+ "lib/db_charmer/sharding/stub_connection.rb",
49
49
  "lib/tasks/databases.rake"
50
50
  ]
51
51
  s.homepage = %q{http://github.com/kovyrin/db-charmer}
@@ -71,7 +71,6 @@ require 'db_charmer/core_extensions'
71
71
  require 'db_charmer/connection_factory'
72
72
  require 'db_charmer/connection_proxy'
73
73
  require 'db_charmer/connection_switch'
74
- require 'db_charmer/stub_connection'
75
74
  require 'db_charmer/scope_proxy'
76
75
  require 'db_charmer/multi_db_proxy'
77
76
 
@@ -121,6 +120,10 @@ require 'db_charmer/association_preload'
121
120
  require 'db_charmer/multi_db_migrations'
122
121
  require 'db_charmer/multi_db_proxy'
123
122
 
123
+ require 'db_charmer/sharding'
124
+ require 'db_charmer/sharding/connection'
125
+ require 'db_charmer/sharding/stub_connection'
126
+
124
127
  # Enable multi-db migrations
125
128
  ActiveRecord::Migration.extend(DbCharmer::MultiDbMigrations)
126
129
 
@@ -18,7 +18,7 @@ module DbCharmer
18
18
  return conn.db_charmer_connection_proxy
19
19
  end
20
20
 
21
- if conn.kind_of?(ActiveRecord::ConnectionAdapters::AbstractAdapter) || conn.kind_of?(DbCharmer::StubConnection)
21
+ if conn.kind_of?(ActiveRecord::ConnectionAdapters::AbstractAdapter) || conn.kind_of?(DbCharmer::Sharding::StubConnection)
22
22
  return conn
23
23
  end
24
24
 
@@ -28,7 +28,7 @@ module DbCharmer
28
28
  def switch_connection_to(conn, require_config_to_exist = true)
29
29
  new_conn = coerce_to_connection_proxy(conn, require_config_to_exist)
30
30
 
31
- if db_charmer_connection_proxy.is_a?(DbCharmer::StubConnection)
31
+ if db_charmer_connection_proxy.is_a?(DbCharmer::Sharding::StubConnection)
32
32
  db_charmer_connection_proxy.set_real_connection(new_conn)
33
33
  end
34
34
 
@@ -23,20 +23,6 @@ module DbCharmer
23
23
  if opt[:sharded]
24
24
  raise ArgumentError, "Can't use sharding on a model with slaves!" if opt[:slaves].any?
25
25
  setup_sharding_magic(opt[:sharded])
26
-
27
- # If method supports shards enumeration, get the first shard
28
- conns = sharded_connection.shard_connections || []
29
- real_conn = conns.first
30
-
31
- # If connection we do not have real connection yet, try to use the default one
32
- real_conn ||= sharded_connection.sharder.shard_for_key(:default) if sharded_connection.support_default_shard?
33
-
34
- # Create stub connection
35
- real_conn = coerce_to_connection_proxy(real_conn, DbCharmer.connections_should_exist?) if real_conn
36
- stub_conn = DbCharmer::StubConnection.new(real_conn)
37
-
38
- # ... and set it as the default one for this model
39
- setup_connection_magic(stub_conn)
40
26
  end
41
27
  end
42
28
 
@@ -52,9 +38,16 @@ module DbCharmer
52
38
  end
53
39
 
54
40
  def setup_sharding_magic(config)
41
+ # Add sharding-specific methods
55
42
  self.extend(DbCharmer::Sharding::ClassMethods)
43
+
44
+ # Get configuration
56
45
  name = config[:sharded_connection] or raise ArgumentError, "No :sharded_connection!"
46
+ # Assign sharded connection
57
47
  self.sharded_connection = DbCharmer::Sharding.sharded_connection(name)
48
+
49
+ # Setup model default connection
50
+ setup_connection_magic(sharded_connection.default_connection)
58
51
  end
59
52
 
60
53
  def setup_connection_magic(conn, should_exist = true)
@@ -22,6 +22,10 @@ module DbCharmer
22
22
  def support_default_shard?
23
23
  sharder.respond_to?(:support_default_shard?) && sharder.support_default_shard?
24
24
  end
25
+
26
+ def default_connection
27
+ @default_connection ||= DbCharmer::Sharding::StubConnection.new(self)
28
+ end
25
29
  end
26
30
  end
27
31
  end
@@ -0,0 +1,51 @@
1
+ # This is a simple proxy class used as a default connection on sharded models
2
+ #
3
+ # The idea is to proxy all utility method calls to a real connection (set by
4
+ # the +set_real_connection+ method when we switch shards) and fail on real
5
+ # database querying calls forcing users to switch shard connections.
6
+ #
7
+ module DbCharmer
8
+ module Sharding
9
+ class StubConnection
10
+ attr_accessor :sharded_connection
11
+
12
+ def initialize(sharded_connection)
13
+ @sharded_connection = sharded_connection
14
+ @real_conn = nil
15
+ end
16
+
17
+ def set_real_connection(real_conn)
18
+ @real_conn = real_conn
19
+ end
20
+
21
+ def real_connection
22
+ # Return memoized real connection
23
+ return @real_conn if @real_conn
24
+
25
+ # If sharded connection supports shards enumeration, get the first shard
26
+ @real_conn = sharded_connection.shard_connections.try(:first)
27
+
28
+ # If we do not have real connection yet, try to use the default one (if it is supported by the sharder)
29
+ @real_conn ||= sharded_connection.sharder.shard_for_key(:default) if sharded_connection.support_default_shard?
30
+
31
+ # Get connection proxy for our real connection
32
+ @real_conn && coerce_to_connection_proxy(@real_conn, DbCharmer.connections_should_exist?)
33
+ end
34
+
35
+ def method_missing(meth, *args, &block)
36
+ # Fail on database statements
37
+ if ActiveRecord::ConnectionAdapters::DatabaseStatements.instance_methods.member?(meth.to_s)
38
+ raise ActiveRecord::ConnectionNotEstablished, "You have to switch connection on your model before using it!"
39
+ end
40
+
41
+ # Fail if no connection has been established yet
42
+ unless @real_conn
43
+ raise ActiveRecord::ConnectionNotEstablished, "No real connection to proxy this method to!"
44
+ end
45
+
46
+ # Proxy the call to our real connection target
47
+ @real_conn.__send__(meth, *args, &block)
48
+ end
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-charmer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 47
4
+ hash: 45
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 6
9
- - 16
10
- version: 1.6.16
9
+ - 17
10
+ version: 1.6.17
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alexey Kovyrin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-28 00:00:00 -05:00
18
+ date: 2011-04-25 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -85,7 +85,7 @@ files:
85
85
  - lib/db_charmer/sharding/method/db_block_map.rb
86
86
  - lib/db_charmer/sharding/method/hash_map.rb
87
87
  - lib/db_charmer/sharding/method/range.rb
88
- - lib/db_charmer/stub_connection.rb
88
+ - lib/db_charmer/sharding/stub_connection.rb
89
89
  - lib/tasks/databases.rake
90
90
  has_rdoc: true
91
91
  homepage: http://github.com/kovyrin/db-charmer
@@ -1,32 +0,0 @@
1
- # This is a simple proxy class used as a default connection on sharded models
2
- #
3
- # The idea is to proxy all utility method calls to a real connection (set by
4
- # the +set_real_connection+ method when we switch shards) and fail on real
5
- # database querying calls forcing users to switch shard connections.
6
- #
7
- module DbCharmer
8
- class StubConnection
9
- def initialize(real_conn = nil)
10
- @real_conn = real_conn
11
- end
12
-
13
- def set_real_connection(real_conn)
14
- @real_conn = real_conn
15
- end
16
-
17
- def method_missing(meth, *args, &block)
18
- # Fail on database statements
19
- if ActiveRecord::ConnectionAdapters::DatabaseStatements.instance_methods.member?(meth.to_s)
20
- raise ActiveRecord::ConnectionNotEstablished, "You have to switch connection on your model before using it!"
21
- end
22
-
23
- # Fail if no connection has been established yet
24
- unless @real_conn
25
- raise ActiveRecord::ConnectionNotEstablished, "No real connection to proxy this method to!"
26
- end
27
-
28
- # Proxy the call to our real connection target
29
- @real_conn.__send__(meth, *args, &block)
30
- end
31
- end
32
- end