ar-octopus 0.0.26 → 0.0.27

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. data/Rakefile +9 -2
  2. data/ar-octopus.gemspec +70 -5
  3. data/lib/octopus/has_and_belongs_to_many_association.rb +1 -5
  4. data/lib/octopus/model.rb +6 -16
  5. data/lib/octopus/proxy.rb +1 -6
  6. data/lib/octopus/scope_proxy.rb +7 -10
  7. data/sample_app/.gitignore +4 -0
  8. data/sample_app/.rspec +1 -0
  9. data/sample_app/Gemfile +22 -0
  10. data/sample_app/Gemfile.lock +149 -0
  11. data/sample_app/README +3 -0
  12. data/sample_app/Rakefile +7 -0
  13. data/sample_app/app/controllers/application_controller.rb +4 -0
  14. data/sample_app/app/helpers/application_helper.rb +2 -0
  15. data/sample_app/app/models/item.rb +2 -0
  16. data/sample_app/app/models/user.rb +2 -0
  17. data/sample_app/app/views/layouts/application.html.erb +14 -0
  18. data/sample_app/autotest/discover.rb +2 -0
  19. data/sample_app/config.ru +4 -0
  20. data/sample_app/config/application.rb +46 -0
  21. data/sample_app/config/boot.rb +13 -0
  22. data/sample_app/config/cucumber.yml +8 -0
  23. data/sample_app/config/database.yml +25 -0
  24. data/sample_app/config/environment.rb +5 -0
  25. data/sample_app/config/environments/development.rb +19 -0
  26. data/sample_app/config/environments/production.rb +46 -0
  27. data/sample_app/config/environments/test.rb +32 -0
  28. data/sample_app/config/initializers/backtrace_silencers.rb +7 -0
  29. data/sample_app/config/initializers/inflections.rb +10 -0
  30. data/sample_app/config/initializers/mime_types.rb +5 -0
  31. data/sample_app/config/initializers/secret_token.rb +7 -0
  32. data/sample_app/config/initializers/session_store.rb +8 -0
  33. data/sample_app/config/locales/en.yml +5 -0
  34. data/sample_app/config/routes.rb +58 -0
  35. data/sample_app/config/shards.yml +28 -0
  36. data/sample_app/db/migrate/20100720172715_create_users.rb +15 -0
  37. data/sample_app/db/migrate/20100720172730_create_items.rb +16 -0
  38. data/sample_app/db/migrate/20100720210335_create_sample_users.rb +11 -0
  39. data/sample_app/db/schema.rb +29 -0
  40. data/sample_app/db/seeds.rb +16 -0
  41. data/sample_app/doc/README_FOR_APP +2 -0
  42. data/sample_app/features/migrate.feature +44 -0
  43. data/sample_app/features/seed.feature +15 -0
  44. data/sample_app/features/step_definitions/seeds_steps.rb +15 -0
  45. data/sample_app/features/step_definitions/web_steps.rb +219 -0
  46. data/sample_app/features/support/env.rb +65 -0
  47. data/sample_app/features/support/paths.rb +33 -0
  48. data/sample_app/lib/tasks/.gitkeep +0 -0
  49. data/sample_app/lib/tasks/cucumber.rake +53 -0
  50. data/sample_app/public/404.html +26 -0
  51. data/sample_app/public/422.html +26 -0
  52. data/sample_app/public/500.html +26 -0
  53. data/sample_app/public/favicon.ico +0 -0
  54. data/sample_app/public/images/rails.png +0 -0
  55. data/sample_app/public/index.html +279 -0
  56. data/sample_app/public/javascripts/application.js +2 -0
  57. data/sample_app/public/javascripts/controls.js +965 -0
  58. data/sample_app/public/javascripts/dragdrop.js +974 -0
  59. data/sample_app/public/javascripts/effects.js +1123 -0
  60. data/sample_app/public/javascripts/prototype.js +4874 -0
  61. data/sample_app/public/javascripts/rails.js +118 -0
  62. data/sample_app/public/robots.txt +5 -0
  63. data/sample_app/public/stylesheets/.gitkeep +0 -0
  64. data/sample_app/script/cucumber +10 -0
  65. data/sample_app/script/rails +6 -0
  66. data/sample_app/spec/models/item_spec.rb +5 -0
  67. data/sample_app/spec/models/user_spec.rb +5 -0
  68. data/sample_app/spec/spec_helper.rb +27 -0
  69. data/sample_app/test/performance/browsing_test.rb +9 -0
  70. data/sample_app/test/test_helper.rb +13 -0
  71. data/sample_app/vendor/plugins/.gitkeep +0 -0
  72. data/spec/config/shards.yml +16 -18
  73. data/spec/database_models.rb +5 -0
  74. data/spec/octopus/model_spec.rb +77 -14
  75. data/spec/octopus/proxy_spec.rb +2 -0
  76. data/spec/octopus/replication_specs.rb +5 -0
  77. metadata +72 -8
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+ $LOAD_PATH << (File.dirname(__FILE__) + '/spec')
2
3
  require 'rubygems'
3
4
  require 'rake'
4
5
  require "yaml"
@@ -30,8 +31,8 @@ begin
30
31
  gem.add_development_dependency "mysql", ">= 2.8.1"
31
32
  gem.add_development_dependency "pg", ">= 0.9.0"
32
33
  gem.add_development_dependency "sqlite3-ruby", ">= 1.3.1"
33
- gem.add_dependency('activerecord', '>= 3.0.0beta')
34
- gem.version = "0.0.26"
34
+ gem.add_dependency('activerecord', '>= 2.3')
35
+ gem.version = "0.0.27"
35
36
  end
36
37
  Jeweler::GemcutterTasks.new
37
38
  rescue LoadError
@@ -85,6 +86,7 @@ namespace :db do
85
86
  end
86
87
 
87
88
  %x( dropdb -U #{postgres_user} octopus_shard1 )
89
+ %x(rm /tmp/database.sqlite3)
88
90
  end
89
91
 
90
92
  desc 'Create tables on tests databases'
@@ -155,6 +157,11 @@ namespace :db do
155
157
  u.string :commentable_type
156
158
  u.integer :commentable_id
157
159
  end
160
+
161
+ ActiveRecord::Base.using(shard_symbol).connection.create_table(:parts) do |u|
162
+ u.string :name
163
+ u.integer :item_id
164
+ end
158
165
  end
159
166
  end
160
167
 
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.26"
8
+ s.version = "0.0.27"
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"]
12
- s.date = %q{2010-07-19}
12
+ s.date = %q{2010-07-23}
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 = [
@@ -39,6 +39,71 @@ Gem::Specification.new do |s|
39
39
  "lib/octopus/rails3/persistence.rb",
40
40
  "lib/octopus/scope_proxy.rb",
41
41
  "rails/init.rb",
42
+ "sample_app/.gitignore",
43
+ "sample_app/.rspec",
44
+ "sample_app/Gemfile",
45
+ "sample_app/Gemfile.lock",
46
+ "sample_app/README",
47
+ "sample_app/Rakefile",
48
+ "sample_app/app/controllers/application_controller.rb",
49
+ "sample_app/app/helpers/application_helper.rb",
50
+ "sample_app/app/models/item.rb",
51
+ "sample_app/app/models/user.rb",
52
+ "sample_app/app/views/layouts/application.html.erb",
53
+ "sample_app/autotest/discover.rb",
54
+ "sample_app/config.ru",
55
+ "sample_app/config/application.rb",
56
+ "sample_app/config/boot.rb",
57
+ "sample_app/config/cucumber.yml",
58
+ "sample_app/config/database.yml",
59
+ "sample_app/config/environment.rb",
60
+ "sample_app/config/environments/development.rb",
61
+ "sample_app/config/environments/production.rb",
62
+ "sample_app/config/environments/test.rb",
63
+ "sample_app/config/initializers/backtrace_silencers.rb",
64
+ "sample_app/config/initializers/inflections.rb",
65
+ "sample_app/config/initializers/mime_types.rb",
66
+ "sample_app/config/initializers/secret_token.rb",
67
+ "sample_app/config/initializers/session_store.rb",
68
+ "sample_app/config/locales/en.yml",
69
+ "sample_app/config/routes.rb",
70
+ "sample_app/config/shards.yml",
71
+ "sample_app/db/migrate/20100720172715_create_users.rb",
72
+ "sample_app/db/migrate/20100720172730_create_items.rb",
73
+ "sample_app/db/migrate/20100720210335_create_sample_users.rb",
74
+ "sample_app/db/schema.rb",
75
+ "sample_app/db/seeds.rb",
76
+ "sample_app/doc/README_FOR_APP",
77
+ "sample_app/features/migrate.feature",
78
+ "sample_app/features/seed.feature",
79
+ "sample_app/features/step_definitions/seeds_steps.rb",
80
+ "sample_app/features/step_definitions/web_steps.rb",
81
+ "sample_app/features/support/env.rb",
82
+ "sample_app/features/support/paths.rb",
83
+ "sample_app/lib/tasks/.gitkeep",
84
+ "sample_app/lib/tasks/cucumber.rake",
85
+ "sample_app/public/404.html",
86
+ "sample_app/public/422.html",
87
+ "sample_app/public/500.html",
88
+ "sample_app/public/favicon.ico",
89
+ "sample_app/public/images/rails.png",
90
+ "sample_app/public/index.html",
91
+ "sample_app/public/javascripts/application.js",
92
+ "sample_app/public/javascripts/controls.js",
93
+ "sample_app/public/javascripts/dragdrop.js",
94
+ "sample_app/public/javascripts/effects.js",
95
+ "sample_app/public/javascripts/prototype.js",
96
+ "sample_app/public/javascripts/rails.js",
97
+ "sample_app/public/robots.txt",
98
+ "sample_app/public/stylesheets/.gitkeep",
99
+ "sample_app/script/cucumber",
100
+ "sample_app/script/rails",
101
+ "sample_app/spec/models/item_spec.rb",
102
+ "sample_app/spec/models/user_spec.rb",
103
+ "sample_app/spec/spec_helper.rb",
104
+ "sample_app/test/performance/browsing_test.rb",
105
+ "sample_app/test/test_helper.rb",
106
+ "sample_app/vendor/plugins/.gitkeep",
42
107
  "spec/config/shards.yml",
43
108
  "spec/database_connection.rb",
44
109
  "spec/database_models.rb",
@@ -109,20 +174,20 @@ Gem::Specification.new do |s|
109
174
  s.add_development_dependency(%q<mysql>, [">= 2.8.1"])
110
175
  s.add_development_dependency(%q<pg>, [">= 0.9.0"])
111
176
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
112
- s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0beta"])
177
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.3"])
113
178
  else
114
179
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
115
180
  s.add_dependency(%q<mysql>, [">= 2.8.1"])
116
181
  s.add_dependency(%q<pg>, [">= 0.9.0"])
117
182
  s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
118
- s.add_dependency(%q<activerecord>, [">= 3.0.0beta"])
183
+ s.add_dependency(%q<activerecord>, [">= 2.3"])
119
184
  end
120
185
  else
121
186
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
122
187
  s.add_dependency(%q<mysql>, [">= 2.8.1"])
123
188
  s.add_dependency(%q<pg>, [">= 0.9.0"])
124
189
  s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
125
- s.add_dependency(%q<activerecord>, [">= 3.0.0beta"])
190
+ s.add_dependency(%q<activerecord>, [">= 2.3"])
126
191
  end
127
192
  end
128
193
 
@@ -4,11 +4,7 @@ module Octopus::HasAndBelongsToManyAssociation
4
4
  alias_method_chain :insert_record, :octopus
5
5
  end
6
6
  end
7
-
8
- def should_wrap_the_connection?
9
- @owner.respond_to?(:current_shard) && @owner.current_shard != nil
10
- end
11
-
7
+
12
8
  def insert_record_with_octopus(record, force = true, validate = true)
13
9
  if should_wrap_the_connection?
14
10
  Octopus.using(@owner.current_shard) { insert_record_without_octopus(record, force, validate) }
data/lib/octopus/model.rb CHANGED
@@ -3,10 +3,6 @@ module Octopus::Model
3
3
  base.send(:include, InstanceMethods)
4
4
  base.extend(ClassMethods)
5
5
  base.hijack_connection()
6
-
7
- class << base
8
- alias_method_chain :connection, :octopus
9
- end
10
6
  end
11
7
 
12
8
  module SharedMethods
@@ -50,19 +46,13 @@ module Octopus::Model
50
46
  Thread.current[:connection_proxy] ||= Octopus::Proxy.new(Octopus.config())
51
47
  end
52
48
 
53
- def self.connection_with_octopus()
54
- if defined?(::Rails)
55
- Octopus.config()
56
- if Octopus.enviroments.include?(Rails.env.to_s)
57
- self.connection_proxy().current_model = self
58
- return self.connection_proxy()
59
- else
60
- self.connection_without_octopus()
61
- end
62
- else
63
- self.connection_proxy().current_model = self
64
- return self.connection_proxy()
49
+ def self.connection()
50
+ if defined?(::Rails) && Octopus.config() && !Octopus.enviroments.include?(Rails.env.to_s)
51
+ return super
65
52
  end
53
+
54
+ self.connection_proxy().current_model = self
55
+ self.connection_proxy()
66
56
  end
67
57
  end
68
58
  end
data/lib/octopus/proxy.rb CHANGED
@@ -12,12 +12,7 @@ class Octopus::Proxy
12
12
  @shards[:master] = ActiveRecord::Base.connection_pool()
13
13
  @current_shard = :master
14
14
 
15
- if !config.nil? && Octopus.rails?
16
- shards_config = config[Rails.env().to_s]["shards"]
17
- elsif !config.nil?
18
- shards_config = config["shards"]
19
- end
20
-
15
+ shards_config = config[Octopus.rails_env()] if !config.nil?
21
16
  shards_config ||= []
22
17
 
23
18
  shards_config.each do |key, value|
@@ -13,14 +13,9 @@ class Octopus::ScopeProxy
13
13
 
14
14
  # Transaction Method send all queries to a specified shard.
15
15
  def transaction(options = {}, &block)
16
- @klass.connection().current_shard = @shard
17
- @klass.connection().block = true
18
-
19
- begin
20
- @klass.connection().transaction(options, &block)
21
- ensure
22
- @klass.connection().block = false
23
- end
16
+ @klass.connection.run_queries_on_shard(@shard) do
17
+ @klass = @klass.connection().transaction(options, &block)
18
+ end
24
19
  end
25
20
 
26
21
  def connection
@@ -29,8 +24,10 @@ class Octopus::ScopeProxy
29
24
  end
30
25
 
31
26
  def method_missing(method, *args, &block)
32
- @klass.connection().current_shard = @shard
33
- @klass = @klass.send(method, *args, &block)
27
+ @klass.connection.run_queries_on_shard(@shard) do
28
+ @klass = @klass.send(method, *args, &block)
29
+ end
30
+
34
31
  return @klass if @klass.is_a?(ActiveRecord::Base) or @klass.is_a?(Array) or @klass.is_a?(Fixnum) or @klass.nil?
35
32
  return self
36
33
  end
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ db/*.sqlite3
3
+ log/*.log
4
+ tmp/**/*
data/sample_app/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,22 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails', '3.0.0.beta4'
4
+
5
+ # Bundle edge Rails instead:
6
+ # gem 'rails', :git => 'git://github.com/rails/rails.git'
7
+
8
+ gem 'sqlite3-ruby', :require => 'sqlite3'
9
+
10
+ gem 'ar-octopus', :git => 'git://github.com/tchandy/octopus.git', :require => "octopus"
11
+
12
+ group :test do
13
+ gem 'capybara'
14
+ gem 'database_cleaner'
15
+ gem 'cucumber-rails'
16
+ gem 'cucumber'
17
+ gem 'spork'
18
+ gem 'launchy'
19
+ gem "rspec-rails", ">= 2.0.0.beta.16"
20
+ gem 'ruby-debug'
21
+ gem "aruba"
22
+ end
@@ -0,0 +1,149 @@
1
+ GIT
2
+ remote: git://github.com/tchandy/octopus.git
3
+ revision: 65b5bcf
4
+ specs:
5
+ ar-octopus (0.0.26)
6
+ activerecord (>= 3.0.0beta)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ abstract (1.0.0)
12
+ actionmailer (3.0.0.beta4)
13
+ actionpack (= 3.0.0.beta4)
14
+ mail (~> 2.2.3)
15
+ actionpack (3.0.0.beta4)
16
+ activemodel (= 3.0.0.beta4)
17
+ activesupport (= 3.0.0.beta4)
18
+ builder (~> 2.1.2)
19
+ erubis (~> 2.6.5)
20
+ i18n (~> 0.4.1)
21
+ rack (~> 1.1.0)
22
+ rack-mount (~> 0.6.3)
23
+ rack-test (~> 0.5.4)
24
+ tzinfo (~> 0.3.16)
25
+ activemodel (3.0.0.beta4)
26
+ activesupport (= 3.0.0.beta4)
27
+ builder (~> 2.1.2)
28
+ i18n (~> 0.4.1)
29
+ activerecord (3.0.0.beta4)
30
+ activemodel (= 3.0.0.beta4)
31
+ activesupport (= 3.0.0.beta4)
32
+ arel (~> 0.4.0)
33
+ tzinfo (~> 0.3.16)
34
+ activeresource (3.0.0.beta4)
35
+ activemodel (= 3.0.0.beta4)
36
+ activesupport (= 3.0.0.beta4)
37
+ activesupport (3.0.0.beta4)
38
+ arel (0.4.0)
39
+ activesupport (>= 3.0.0.beta)
40
+ aruba (0.2.1)
41
+ builder (2.1.2)
42
+ capybara (0.3.9)
43
+ culerity (>= 0.2.4)
44
+ mime-types (>= 1.16)
45
+ nokogiri (>= 1.3.3)
46
+ rack (>= 1.0.0)
47
+ rack-test (>= 0.5.4)
48
+ selenium-webdriver (>= 0.0.3)
49
+ columnize (0.3.1)
50
+ configuration (1.1.0)
51
+ cucumber (0.8.5)
52
+ builder (~> 2.1.2)
53
+ diff-lcs (~> 1.1.2)
54
+ gherkin (~> 2.1.4)
55
+ json_pure (~> 1.4.3)
56
+ term-ansicolor (~> 1.0.4)
57
+ cucumber-rails (0.3.2)
58
+ cucumber (>= 0.8.0)
59
+ culerity (0.2.10)
60
+ database_cleaner (0.5.2)
61
+ diff-lcs (1.1.2)
62
+ erubis (2.6.6)
63
+ abstract (>= 1.0.0)
64
+ ffi (0.6.3)
65
+ rake (>= 0.8.7)
66
+ gherkin (2.1.5)
67
+ trollop (~> 1.16.2)
68
+ i18n (0.4.1)
69
+ json_pure (1.4.3)
70
+ launchy (0.3.7)
71
+ configuration (>= 0.0.5)
72
+ rake (>= 0.8.1)
73
+ linecache (0.43)
74
+ mail (2.2.5)
75
+ activesupport (>= 2.3.6)
76
+ mime-types
77
+ treetop (>= 1.4.5)
78
+ mime-types (1.16)
79
+ nokogiri (1.4.2)
80
+ polyglot (0.3.1)
81
+ rack (1.1.0)
82
+ rack-mount (0.6.9)
83
+ rack (>= 1.0.0)
84
+ rack-test (0.5.4)
85
+ rack (>= 1.0)
86
+ rails (3.0.0.beta4)
87
+ actionmailer (= 3.0.0.beta4)
88
+ actionpack (= 3.0.0.beta4)
89
+ activerecord (= 3.0.0.beta4)
90
+ activeresource (= 3.0.0.beta4)
91
+ activesupport (= 3.0.0.beta4)
92
+ bundler (>= 0.9.26)
93
+ railties (= 3.0.0.beta4)
94
+ railties (3.0.0.beta4)
95
+ actionpack (= 3.0.0.beta4)
96
+ activesupport (= 3.0.0.beta4)
97
+ rake (>= 0.8.3)
98
+ thor (~> 0.13.6)
99
+ rake (0.8.7)
100
+ rspec (2.0.0.beta.18)
101
+ rspec-core (= 2.0.0.beta.18)
102
+ rspec-expectations (= 2.0.0.beta.18)
103
+ rspec-mocks (= 2.0.0.beta.18)
104
+ rspec-core (2.0.0.beta.18)
105
+ rspec-expectations (2.0.0.beta.18)
106
+ diff-lcs (>= 1.1.2)
107
+ rspec-mocks (2.0.0.beta.18)
108
+ rspec-rails (2.0.0.beta.18)
109
+ rspec (>= 2.0.0.beta.14)
110
+ webrat (>= 0.7.0)
111
+ ruby-debug (0.10.3)
112
+ columnize (>= 0.1)
113
+ ruby-debug-base (~> 0.10.3.0)
114
+ ruby-debug-base (0.10.3)
115
+ linecache (>= 0.3)
116
+ rubyzip (0.9.4)
117
+ selenium-webdriver (0.0.26)
118
+ ffi (>= 0.6.1)
119
+ json_pure
120
+ rubyzip
121
+ spork (0.8.4)
122
+ sqlite3-ruby (1.3.1)
123
+ term-ansicolor (1.0.5)
124
+ thor (0.13.8)
125
+ treetop (1.4.8)
126
+ polyglot (>= 0.3.1)
127
+ trollop (1.16.2)
128
+ tzinfo (0.3.22)
129
+ webrat (0.7.1)
130
+ nokogiri (>= 1.2.0)
131
+ rack (>= 1.0)
132
+ rack-test (>= 0.5.3)
133
+
134
+ PLATFORMS
135
+ ruby
136
+
137
+ DEPENDENCIES
138
+ ar-octopus!
139
+ aruba
140
+ capybara
141
+ cucumber
142
+ cucumber-rails
143
+ database_cleaner
144
+ launchy
145
+ rails (= 3.0.0.beta4)
146
+ rspec-rails (>= 2.0.0.beta.16)
147
+ ruby-debug
148
+ spork
149
+ sqlite3-ruby
data/sample_app/README ADDED
@@ -0,0 +1,3 @@
1
+ == Welcome to the Octopus test app!
2
+
3
+ This is a sample app, created for testing propouses.
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Rails::Application.load_tasks
@@ -0,0 +1,4 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ layout 'application'
4
+ end