ar-octopus 0.0.21 → 0.0.22
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/ar-octopus.gemspec +2 -2
- data/lib/octopus.rb +9 -1
- data/lib/octopus/migration.rb +32 -15
- data/lib/octopus/proxy.rb +6 -1
- data/spec/config/shards.yml +30 -0
- data/spec/octopus/proxy_spec.rb +38 -7
- metadata +4 -4
data/Rakefile
CHANGED
@@ -29,7 +29,7 @@ begin
|
|
29
29
|
gem.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
|
30
30
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
31
31
|
gem.add_dependency('activerecord', '>= 3.0.0beta')
|
32
|
-
gem.version = "0.0.
|
32
|
+
gem.version = "0.0.22"
|
33
33
|
end
|
34
34
|
Jeweler::GemcutterTasks.new
|
35
35
|
rescue LoadError
|
data/ar-octopus.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ar-octopus}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.22"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Thiago Pradi", "Mike Perham", "Amit Agarwal"]
|
12
|
-
s.date = %q{2010-07-
|
12
|
+
s.date = %q{2010-07-09}
|
13
13
|
s.description = %q{This gem allows you to use sharded databases with ActiveRecord. this also provides a interface for replication and for running migrations with multiples shards.}
|
14
14
|
s.email = %q{tchandy@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/octopus.rb
CHANGED
@@ -2,7 +2,11 @@ require "yaml"
|
|
2
2
|
|
3
3
|
module Octopus
|
4
4
|
def self.env()
|
5
|
-
|
5
|
+
@env ||= 'octopus'
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.rails_env()
|
9
|
+
@rails_env ||= self.rails? ? Rails.env.to_s : 'shards'
|
6
10
|
end
|
7
11
|
|
8
12
|
def self.config()
|
@@ -39,6 +43,10 @@ module Octopus
|
|
39
43
|
def self.rails3?
|
40
44
|
ActiveRecord::VERSION::MAJOR == 3
|
41
45
|
end
|
46
|
+
|
47
|
+
def self.rails?
|
48
|
+
defined?(Rails)
|
49
|
+
end
|
42
50
|
end
|
43
51
|
|
44
52
|
|
data/lib/octopus/migration.rb
CHANGED
@@ -2,19 +2,29 @@ module Octopus::Migration
|
|
2
2
|
def self.extended(base)
|
3
3
|
class << base
|
4
4
|
alias_method_chain :migrate, :octopus
|
5
|
+
|
6
|
+
def announce(message)
|
7
|
+
version = defined?(@version) ? @version : nil
|
8
|
+
|
9
|
+
text = "#{version} #{name}: #{message} - #{get_current_shard}"
|
10
|
+
length = [0, 75 - text.length].max
|
11
|
+
write "== %s %s" % [text, "=" * length]
|
12
|
+
end
|
5
13
|
end
|
6
14
|
end
|
7
15
|
|
8
16
|
def using(*args, &block)
|
9
17
|
Octopus.config()
|
18
|
+
|
19
|
+
unless defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
|
20
|
+
args.each do |shard|
|
21
|
+
self.connection().check_schema_migrations(shard)
|
22
|
+
end
|
10
23
|
|
11
|
-
|
12
|
-
self.connection().
|
24
|
+
self.connection().block = true
|
25
|
+
self.connection().current_shard = args
|
13
26
|
end
|
14
|
-
|
15
|
-
self.connection().block = true
|
16
|
-
self.connection().current_shard = args
|
17
|
-
|
27
|
+
|
18
28
|
yield if block_given?
|
19
29
|
|
20
30
|
return self
|
@@ -23,24 +33,31 @@ module Octopus::Migration
|
|
23
33
|
def using_group(*args)
|
24
34
|
Octopus.config()
|
25
35
|
|
26
|
-
|
27
|
-
|
36
|
+
unless defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
|
37
|
+
args.each do |group_shard|
|
38
|
+
shards = self.connection().instance_variable_get(:@groups)[group_shard] || []
|
28
39
|
|
29
|
-
|
30
|
-
|
40
|
+
shards.each do |shard|
|
41
|
+
self.connection().check_schema_migrations(shard)
|
42
|
+
end
|
31
43
|
end
|
32
|
-
end
|
33
|
-
|
34
|
-
self.connection().block = true
|
35
|
-
self.connection().current_group = args
|
36
44
|
|
45
|
+
self.connection().block = true
|
46
|
+
self.connection().current_group = args
|
47
|
+
end
|
48
|
+
|
37
49
|
return self
|
38
50
|
end
|
51
|
+
|
52
|
+
def get_current_shard
|
53
|
+
"Shard: #{ActiveRecord::Base.connection.current_shard()}" if ActiveRecord::Base.connection.respond_to?(:current_shard)
|
54
|
+
end
|
55
|
+
|
39
56
|
|
40
57
|
def migrate_with_octopus(direction)
|
41
58
|
conn = ActiveRecord::Base.connection
|
42
59
|
groups = conn.instance_variable_get(:@groups)
|
43
|
-
|
60
|
+
|
44
61
|
if conn.current_group.is_a?(Array)
|
45
62
|
conn.current_group.each { |group| conn.send_queries_to_multiple_shards(groups[group]) { migrate_without_octopus(direction) } }
|
46
63
|
elsif conn.current_group.is_a?(Symbol)
|
data/lib/octopus/proxy.rb
CHANGED
@@ -12,7 +12,12 @@ class Octopus::Proxy
|
|
12
12
|
@shards[:master] = ActiveRecord::Base.connection_pool()
|
13
13
|
@current_shard = :master
|
14
14
|
|
15
|
-
|
15
|
+
if have_config_for_enviroment?(config) && Octopus.rails?
|
16
|
+
shards_config = config[Octopus.env()][Rails.env().to_s]["shards"]
|
17
|
+
elsif have_config_for_enviroment?(config)
|
18
|
+
shards_config = config[Octopus.env()]["shards"]
|
19
|
+
end
|
20
|
+
|
16
21
|
shards_config ||= []
|
17
22
|
|
18
23
|
shards_config.each do |key, value|
|
data/spec/config/shards.yml
CHANGED
@@ -106,4 +106,34 @@ production_entire_replicated:
|
|
106
106
|
host: localhost
|
107
107
|
database: octopus_shard5
|
108
108
|
|
109
|
+
octopus_rails:
|
110
|
+
excluded_enviroments:
|
111
|
+
- test
|
112
|
+
- cucumber
|
113
|
+
|
114
|
+
replicated: true
|
115
|
+
|
116
|
+
staging:
|
117
|
+
shards:
|
118
|
+
slave1:
|
119
|
+
adapter: mysql
|
120
|
+
host: localhost
|
121
|
+
database: octopus_shard2
|
122
|
+
slave2:
|
123
|
+
adapter: mysql
|
124
|
+
host: localhost
|
125
|
+
database: octopus_shard3
|
126
|
+
|
127
|
+
production:
|
128
|
+
shards:
|
129
|
+
slave3:
|
130
|
+
adapter: mysql
|
131
|
+
host: localhost
|
132
|
+
database: octopus_shard4
|
133
|
+
slave4:
|
134
|
+
adapter: mysql
|
135
|
+
host: localhost
|
136
|
+
database: octopus_shard5
|
137
|
+
|
138
|
+
|
109
139
|
|
data/spec/octopus/proxy_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
2
2
|
|
3
3
|
describe Octopus::Proxy do
|
4
4
|
let(:proxy) { Octopus::Proxy.new(Octopus.config()) }
|
5
|
-
|
5
|
+
|
6
6
|
describe "creating a new instance" do
|
7
7
|
it "should initialize all shards and groups" do
|
8
8
|
proxy.instance_variable_get(:@shards).keys.to_set.should == [:postgresql_shard, :alone_shard, :aug2011, :canada, :brazil, :aug2009, :russia, :aug2010, :master, :sqlite_shard].to_set
|
@@ -12,7 +12,7 @@ describe Octopus::Proxy do
|
|
12
12
|
it "should initialize the block attribute as false" do
|
13
13
|
proxy.block.should be_false
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "should initialize replicated attribute as false" do
|
17
17
|
proxy.instance_variable_get(:@replicated).should be_false
|
18
18
|
end
|
@@ -31,26 +31,26 @@ describe Octopus::Proxy do
|
|
31
31
|
before(:each) do
|
32
32
|
Octopus.stub!(:env).and_return("crazy_enviroment")
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
it "should initialize just the master shard" do
|
36
36
|
proxy.instance_variable_get(:@shards).keys.should == [:master]
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
it "should not initialize the groups variable" do
|
40
40
|
proxy.instance_variable_get(:@groups).should == {}
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
it "should not initialize replication" do
|
44
44
|
proxy.instance_variable_get(:@replicated).should be_nil
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
describe "when you have a replicated enviroment" do
|
50
50
|
before(:each) do
|
51
51
|
Octopus.stub!(:env).and_return("production_replicated")
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
it "should have the replicated attribute as true" do
|
55
55
|
proxy.instance_variable_get(:@replicated).should be_true
|
56
56
|
end
|
@@ -60,6 +60,37 @@ describe Octopus::Proxy do
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
+
describe "when you have a rails application" do
|
64
|
+
before(:each) do
|
65
|
+
Rails = mock()
|
66
|
+
Octopus.stub!(:env).and_return("octopus_rails")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should initialize correctly octopus common variables for the enviroments" do
|
70
|
+
Rails.stub!(:env).and_return('staging')
|
71
|
+
Octopus.config()
|
72
|
+
|
73
|
+
proxy.instance_variable_get(:@replicated).should be_true
|
74
|
+
Octopus.excluded_enviroments.should == ["test", "cucumber"]
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should initialize correctly the shards for the staging enviroment" do
|
78
|
+
Rails.stub!(:env).and_return('staging')
|
79
|
+
|
80
|
+
proxy.instance_variable_get(:@shards).keys.to_set.should == Set.new([:slave1, :slave2, :master])
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should initialize correctly the shards for the production enviroment" do
|
84
|
+
Rails.stub!(:env).and_return('production')
|
85
|
+
|
86
|
+
proxy.instance_variable_get(:@shards).keys.to_set.should == Set.new([:slave3, :slave4, :master])
|
87
|
+
end
|
88
|
+
|
89
|
+
after(:each) do
|
90
|
+
Object.send(:remove_const, :Rails)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
63
94
|
describe "returning the correct connection" do
|
64
95
|
describe "should return the shard name" do
|
65
96
|
it "when current_shard is empty" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-octopus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 51
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 22
|
10
|
+
version: 0.0.22
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thiago Pradi
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2010-07-
|
20
|
+
date: 2010-07-09 00:00:00 -03:00
|
21
21
|
default_executable:
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|