ar-octopus 0.0.22 → 0.0.23

Sign up to get free protection for your applications and to get access to all the features.
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.22"
32
+ gem.version = "0.0.23"
33
33
  end
34
34
  Jeweler::GemcutterTasks.new
35
35
  rescue LoadError
@@ -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.22"
8
+ s.version = "0.0.23"
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-09}
12
+ s.date = %q{2010-07-14}
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 = [
@@ -12,8 +12,8 @@ module Octopus
12
12
  def self.config()
13
13
  @config ||= HashWithIndifferentAccess.new(YAML.load_file(Octopus.directory() + "/config/shards.yml"))
14
14
 
15
- if !@config[Octopus.env].nil? && @config[Octopus.env()]['excluded_enviroments']
16
- self.excluded_enviroments = @config[Octopus.env()]['excluded_enviroments']
15
+ if !@config[Octopus.env].nil? && @config[Octopus.env()]['octopus_enviroments']
16
+ self.octopus_enviroments = @config[Octopus.env()]['octopus_enviroments']
17
17
  end
18
18
 
19
19
  @config
@@ -32,12 +32,12 @@ module Octopus
32
32
  yield self
33
33
  end
34
34
 
35
- def self.excluded_enviroments=(excluded_enviroments)
36
- @excluded_enviroments = excluded_enviroments.map { |element| element.to_s }
35
+ def self.octopus_enviroments=(octopus_enviroments)
36
+ @octopus_enviroments = octopus_enviroments.map { |element| element.to_s }
37
37
  end
38
38
 
39
- def self.excluded_enviroments
40
- @excluded_enviroments || ['development',"cucumber", "test"]
39
+ def self.octopus_enviroments
40
+ @octopus_enviroments || ['production']
41
41
  end
42
42
 
43
43
  def self.rails3?
@@ -14,9 +14,7 @@ module Octopus::Migration
14
14
  end
15
15
 
16
16
  def using(*args, &block)
17
- Octopus.config()
18
-
19
- unless defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
17
+ if self.connection().is_a?(Octopus::Proxy)
20
18
  args.each do |shard|
21
19
  self.connection().check_schema_migrations(shard)
22
20
  end
@@ -31,9 +29,8 @@ module Octopus::Migration
31
29
  end
32
30
 
33
31
  def using_group(*args)
34
- Octopus.config()
35
32
 
36
- unless defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
33
+ if self.connection().is_a?(Octopus::Proxy)
37
34
  args.each do |group_shard|
38
35
  shards = self.connection().instance_variable_get(:@groups)[group_shard] || []
39
36
 
@@ -57,7 +54,9 @@ module Octopus::Migration
57
54
  def migrate_with_octopus(direction)
58
55
  conn = ActiveRecord::Base.connection
59
56
  groups = conn.instance_variable_get(:@groups)
60
-
57
+
58
+ return migrate_without_octopus(direction) unless conn.is_a?(Octopus::Proxy)
59
+
61
60
  if conn.current_group.is_a?(Array)
62
61
  conn.current_group.each { |group| conn.send_queries_to_multiple_shards(groups[group]) { migrate_without_octopus(direction) } }
63
62
  elsif conn.current_group.is_a?(Symbol)
@@ -2,7 +2,11 @@ module Octopus::Model
2
2
  def self.extended(base)
3
3
  base.send(:include, InstanceMethods)
4
4
  base.extend(ClassMethods)
5
- base.hijack_connection() unless defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
5
+ base.hijack_connection()
6
+
7
+ class << base
8
+ alias_method_chain :connection, :octopus
9
+ end
6
10
  end
7
11
 
8
12
  module SharedMethods
@@ -11,8 +15,8 @@ module Octopus::Model
11
15
  end
12
16
 
13
17
  def using(shard, &block)
14
- return self if defined?(::Rails) && Octopus.excluded_enviroments.include?(Rails.env.to_s)
15
-
18
+ return self if defined?(::Rails) && !Octopus.octopus_enviroments.include?(Rails.env.to_s)
19
+
16
20
  hijack_connection()
17
21
  clean_table_name()
18
22
 
@@ -38,7 +42,7 @@ module Octopus::Model
38
42
  self.current_shard = self.class.connection_proxy.last_current_shard
39
43
  end
40
44
  end
41
-
45
+
42
46
  if !Octopus.rails3?
43
47
  def after_initialize
44
48
  set_current_shard()
@@ -47,14 +51,22 @@ module Octopus::Model
47
51
  end
48
52
 
49
53
  def hijack_connection()
50
- class << self
51
- def connection_proxy
52
- Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config())
53
- end
54
+ def self.connection_proxy
55
+ Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config())
56
+ end
54
57
 
55
- def connection
58
+ def self.connection_with_octopus()
59
+ if defined?(::Rails)
60
+ Octopus.config()
61
+ if Octopus.octopus_enviroments.include?(Rails.env.to_s)
62
+ self.connection_proxy().current_model = self
63
+ return self.connection_proxy()
64
+ else
65
+ self.connection_without_octopus()
66
+ end
67
+ else
56
68
  self.connection_proxy().current_model = self
57
- self.connection_proxy()
69
+ return self.connection_proxy()
58
70
  end
59
71
  end
60
72
  end
@@ -75,7 +87,7 @@ module Octopus::Model
75
87
  def should_set_current_shard?
76
88
  self.respond_to?(:current_shard) && self.current_shard != nil
77
89
  end
78
-
90
+
79
91
  def reload_connection()
80
92
  set_connection() if should_set_current_shard?
81
93
  end
@@ -128,14 +128,9 @@ class Octopus::Proxy
128
128
 
129
129
  def initialize_adapter(adapter)
130
130
  begin
131
- gem "activerecord-#{adapter}-adapter"
132
131
  require "active_record/connection_adapters/#{adapter}_adapter"
133
132
  rescue LoadError
134
- begin
135
- require "active_record/connection_adapters/#{adapter}_adapter"
136
- rescue LoadError
137
- raise "Please install the #{adapter} adapter: `gem install activerecord-#{adapter}-adapter` (#{$!})"
138
- end
133
+ raise "Please install the #{adapter} adapter: `gem install activerecord-#{adapter}-adapter` (#{$!})"
139
134
  end
140
135
  end
141
136
 
@@ -1,8 +1,4 @@
1
1
  octopus:
2
- excluded_enviroments:
3
- - cucumber
4
- - test
5
- - staging
6
2
  shards:
7
3
  alone_shard:
8
4
  adapter: mysql
@@ -107,11 +103,10 @@ production_entire_replicated:
107
103
  database: octopus_shard5
108
104
 
109
105
  octopus_rails:
110
- excluded_enviroments:
111
- - test
112
- - cucumber
113
-
114
106
  replicated: true
107
+ octopus_enviroments:
108
+ - staging
109
+ - production
115
110
 
116
111
  staging:
117
112
  shards:
@@ -23,6 +23,7 @@ class Item < ActiveRecord::Base
23
23
  end
24
24
 
25
25
  class Keyboard < ActiveRecord::Base
26
+ validates_uniqueness_of :name
26
27
  belongs_to :computer
27
28
  end
28
29
 
@@ -246,6 +246,20 @@ describe Octopus::Model do
246
246
  end
247
247
  end
248
248
  end
249
+
250
+ describe "ActiveRecord::Base Validations" do
251
+ it "should work correctly when using validations" do
252
+ @key = Keyboard.create!(:name => "Key")
253
+ lambda { Keyboard.using(:brazil).create!(:name => "Key") }.should_not raise_error()
254
+ lambda { Keyboard.create!(:name => "Key") }.should raise_error()
255
+ end
256
+
257
+ it "should work correctly when using validations with using syntax" do
258
+ @key = Keyboard.using(:brazil).create!(:name => "Key")
259
+ lambda { Keyboard.create!(:name => "Key") }.should_not raise_error()
260
+ lambda { Keyboard.using(:brazil).create!(:name => "Key") }.should raise_error()
261
+ end
262
+ end
249
263
 
250
264
  describe "#replicated_model method" do
251
265
  it "should be replicated" do
@@ -19,25 +19,20 @@ describe Octopus do
19
19
  end
20
20
  end
21
21
 
22
- describe "#setup method" do
23
- it "should load from YAML" do
24
- Octopus.excluded_enviroments.should == ["cucumber", "test", "staging"]
22
+ describe "#setup method" do
23
+ it "should have the default octopus enviroment as production" do
24
+ Octopus.octopus_enviroments.should == ["production"]
25
25
  end
26
26
 
27
- it "should have the default excluded enviroments" do
28
- Octopus.instance_variable_set(:@excluded_enviroments, nil)
29
- Octopus.excluded_enviroments.should == ["development", "cucumber", "test"]
30
- end
31
-
32
- it "should configure the excluded enviroments" do
27
+ it "should allow the user to configure the octopus enviroments" do
33
28
  Octopus.setup do |config|
34
- config.excluded_enviroments = [:cucumber, :test]
29
+ config.octopus_enviroments = [:production, :staging]
35
30
  end
36
31
 
37
- Octopus.excluded_enviroments.should == ['cucumber', 'test']
32
+ Octopus.octopus_enviroments.should == ['production', 'staging']
38
33
 
39
34
  Octopus.setup do |config|
40
- config.excluded_enviroments = [:cucumber, :test, :staging]
35
+ config.octopus_enviroments = [:production]
41
36
  end
42
37
  end
43
38
  end
@@ -71,7 +71,7 @@ describe Octopus::Proxy do
71
71
  Octopus.config()
72
72
 
73
73
  proxy.instance_variable_get(:@replicated).should be_true
74
- Octopus.excluded_enviroments.should == ["test", "cucumber"]
74
+ Octopus.octopus_enviroments.should == ["staging", "production"]
75
75
  end
76
76
 
77
77
  it "should initialize correctly the shards for the staging enviroment" 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: 51
4
+ hash: 49
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 22
10
- version: 0.0.22
9
+ - 23
10
+ version: 0.0.23
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-09 00:00:00 -03:00
20
+ date: 2010-07-14 00:00:00 -03:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency