ar-octopus-ruby-3 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +46 -0
  5. data/.rubocop_todo.yml +56 -0
  6. data/.travis.yml +18 -0
  7. data/Appraisals +16 -0
  8. data/Gemfile +4 -0
  9. data/README.mkdn +257 -0
  10. data/Rakefile +175 -0
  11. data/TODO.txt +7 -0
  12. data/ar-octopus.gemspec +44 -0
  13. data/gemfiles/rails42.gemfile +7 -0
  14. data/gemfiles/rails5.gemfile +7 -0
  15. data/gemfiles/rails51.gemfile +7 -0
  16. data/gemfiles/rails52.gemfile +7 -0
  17. data/lib/ar-octopus.rb +1 -0
  18. data/lib/octopus/abstract_adapter.rb +33 -0
  19. data/lib/octopus/association.rb +14 -0
  20. data/lib/octopus/association_shard_tracking.rb +74 -0
  21. data/lib/octopus/collection_association.rb +17 -0
  22. data/lib/octopus/collection_proxy.rb +16 -0
  23. data/lib/octopus/exception.rb +4 -0
  24. data/lib/octopus/finder_methods.rb +8 -0
  25. data/lib/octopus/load_balancing/round_robin.rb +20 -0
  26. data/lib/octopus/load_balancing.rb +4 -0
  27. data/lib/octopus/log_subscriber.rb +26 -0
  28. data/lib/octopus/migration.rb +236 -0
  29. data/lib/octopus/model.rb +216 -0
  30. data/lib/octopus/persistence.rb +45 -0
  31. data/lib/octopus/proxy.rb +399 -0
  32. data/lib/octopus/proxy_config.rb +251 -0
  33. data/lib/octopus/query_cache_for_shards.rb +24 -0
  34. data/lib/octopus/railtie.rb +11 -0
  35. data/lib/octopus/relation_proxy.rb +74 -0
  36. data/lib/octopus/result_patch.rb +19 -0
  37. data/lib/octopus/scope_proxy.rb +68 -0
  38. data/lib/octopus/shard_tracking/attribute.rb +22 -0
  39. data/lib/octopus/shard_tracking/dynamic.rb +11 -0
  40. data/lib/octopus/shard_tracking.rb +46 -0
  41. data/lib/octopus/singular_association.rb +9 -0
  42. data/lib/octopus/slave_group.rb +13 -0
  43. data/lib/octopus/version.rb +3 -0
  44. data/lib/octopus.rb +209 -0
  45. data/lib/tasks/octopus.rake +16 -0
  46. data/sample_app/.gitignore +4 -0
  47. data/sample_app/.rspec +1 -0
  48. data/sample_app/Gemfile +20 -0
  49. data/sample_app/Gemfile.lock +155 -0
  50. data/sample_app/README +3 -0
  51. data/sample_app/README.rdoc +261 -0
  52. data/sample_app/Rakefile +7 -0
  53. data/sample_app/app/assets/images/rails.png +0 -0
  54. data/sample_app/app/assets/javascripts/application.js +15 -0
  55. data/sample_app/app/assets/stylesheets/application.css +13 -0
  56. data/sample_app/app/controllers/application_controller.rb +4 -0
  57. data/sample_app/app/helpers/application_helper.rb +2 -0
  58. data/sample_app/app/mailers/.gitkeep +0 -0
  59. data/sample_app/app/models/.gitkeep +0 -0
  60. data/sample_app/app/models/item.rb +3 -0
  61. data/sample_app/app/models/user.rb +3 -0
  62. data/sample_app/app/views/layouts/application.html.erb +14 -0
  63. data/sample_app/autotest/discover.rb +2 -0
  64. data/sample_app/config/application.rb +62 -0
  65. data/sample_app/config/boot.rb +6 -0
  66. data/sample_app/config/cucumber.yml +8 -0
  67. data/sample_app/config/database.yml +28 -0
  68. data/sample_app/config/environment.rb +5 -0
  69. data/sample_app/config/environments/development.rb +37 -0
  70. data/sample_app/config/environments/production.rb +67 -0
  71. data/sample_app/config/environments/test.rb +37 -0
  72. data/sample_app/config/initializers/backtrace_silencers.rb +7 -0
  73. data/sample_app/config/initializers/inflections.rb +15 -0
  74. data/sample_app/config/initializers/mime_types.rb +5 -0
  75. data/sample_app/config/initializers/secret_token.rb +7 -0
  76. data/sample_app/config/initializers/session_store.rb +8 -0
  77. data/sample_app/config/initializers/wrap_parameters.rb +14 -0
  78. data/sample_app/config/locales/en.yml +5 -0
  79. data/sample_app/config/routes.rb +58 -0
  80. data/sample_app/config/shards.yml +28 -0
  81. data/sample_app/config.ru +4 -0
  82. data/sample_app/db/migrate/20100720172715_create_users.rb +15 -0
  83. data/sample_app/db/migrate/20100720172730_create_items.rb +16 -0
  84. data/sample_app/db/migrate/20100720210335_create_sample_users.rb +11 -0
  85. data/sample_app/db/schema.rb +29 -0
  86. data/sample_app/db/seeds.rb +16 -0
  87. data/sample_app/doc/README_FOR_APP +2 -0
  88. data/sample_app/features/migrate.feature +45 -0
  89. data/sample_app/features/seed.feature +15 -0
  90. data/sample_app/features/step_definitions/seeds_steps.rb +13 -0
  91. data/sample_app/features/step_definitions/web_steps.rb +218 -0
  92. data/sample_app/features/support/database.rb +13 -0
  93. data/sample_app/features/support/env.rb +57 -0
  94. data/sample_app/features/support/paths.rb +33 -0
  95. data/sample_app/lib/assets/.gitkeep +0 -0
  96. data/sample_app/lib/tasks/.gitkeep +0 -0
  97. data/sample_app/lib/tasks/cucumber.rake +64 -0
  98. data/sample_app/log/.gitkeep +0 -0
  99. data/sample_app/public/404.html +26 -0
  100. data/sample_app/public/422.html +26 -0
  101. data/sample_app/public/500.html +26 -0
  102. data/sample_app/public/favicon.ico +0 -0
  103. data/sample_app/public/images/rails.png +0 -0
  104. data/sample_app/public/index.html +279 -0
  105. data/sample_app/public/javascripts/application.js +2 -0
  106. data/sample_app/public/javascripts/controls.js +965 -0
  107. data/sample_app/public/javascripts/dragdrop.js +974 -0
  108. data/sample_app/public/javascripts/effects.js +1123 -0
  109. data/sample_app/public/javascripts/prototype.js +4874 -0
  110. data/sample_app/public/javascripts/rails.js +118 -0
  111. data/sample_app/public/robots.txt +5 -0
  112. data/sample_app/public/stylesheets/.gitkeep +0 -0
  113. data/sample_app/script/cucumber +10 -0
  114. data/sample_app/script/rails +6 -0
  115. data/sample_app/spec/models/item_spec.rb +5 -0
  116. data/sample_app/spec/models/user_spec.rb +5 -0
  117. data/sample_app/spec/spec_helper.rb +27 -0
  118. data/sample_app/vendor/assets/javascripts/.gitkeep +0 -0
  119. data/sample_app/vendor/assets/stylesheets/.gitkeep +0 -0
  120. data/sample_app/vendor/plugins/.gitkeep +0 -0
  121. data/spec/config/shards.yml +231 -0
  122. data/spec/migrations/10_create_users_using_replication.rb +9 -0
  123. data/spec/migrations/11_add_field_in_all_slaves.rb +11 -0
  124. data/spec/migrations/12_create_users_using_block.rb +23 -0
  125. data/spec/migrations/13_create_users_using_block_and_using.rb +15 -0
  126. data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +11 -0
  127. data/spec/migrations/15_create_user_on_shards_of_default_group_with_versions.rb +9 -0
  128. data/spec/migrations/1_create_users_on_master.rb +9 -0
  129. data/spec/migrations/2_create_users_on_canada.rb +11 -0
  130. data/spec/migrations/3_create_users_on_both_shards.rb +11 -0
  131. data/spec/migrations/4_create_users_on_shards_of_a_group.rb +11 -0
  132. data/spec/migrations/5_create_users_on_multiples_groups.rb +11 -0
  133. data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +11 -0
  134. data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +11 -0
  135. data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +11 -0
  136. data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +11 -0
  137. data/spec/octopus/association_shard_tracking_spec.rb +1036 -0
  138. data/spec/octopus/collection_proxy_spec.rb +16 -0
  139. data/spec/octopus/load_balancing/round_robin_spec.rb +15 -0
  140. data/spec/octopus/log_subscriber_spec.rb +19 -0
  141. data/spec/octopus/migration_spec.rb +151 -0
  142. data/spec/octopus/model_spec.rb +837 -0
  143. data/spec/octopus/octopus_spec.rb +123 -0
  144. data/spec/octopus/proxy_spec.rb +303 -0
  145. data/spec/octopus/query_cache_for_shards_spec.rb +40 -0
  146. data/spec/octopus/relation_proxy_spec.rb +132 -0
  147. data/spec/octopus/replicated_slave_grouped_spec.rb +91 -0
  148. data/spec/octopus/replication_spec.rb +196 -0
  149. data/spec/octopus/scope_proxy_spec.rb +97 -0
  150. data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +55 -0
  151. data/spec/octopus/sharded_spec.rb +33 -0
  152. data/spec/spec_helper.rb +18 -0
  153. data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +15 -0
  154. data/spec/support/database_connection.rb +4 -0
  155. data/spec/support/database_models.rb +118 -0
  156. data/spec/support/octopus_helper.rb +66 -0
  157. data/spec/support/query_count.rb +17 -0
  158. data/spec/support/shared_contexts.rb +18 -0
  159. data/spec/tasks/octopus.rake_spec.rb +32 -0
  160. metadata +351 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 45148ba25887b36967a31b5b152fca794127f8aed6c9cbe6e510afb9e46a394b
4
+ data.tar.gz: 90e3f6c6a6d5df3c596c6e6c7187ac8cd12d0564cdbbc52b257034f0080a1328
5
+ SHA512:
6
+ metadata.gz: 492e656e572b5ce6d402e48bd7210ff305c00c8d61bc1d0fe71a0f9d8e30a590e2aba80bfc3b9a74011bf7ac8b48b83cd197867a1d29d41d4d044179a9196166
7
+ data.tar.gz: 27e3f7c9fbdacf8c1e05e7bc6f78f444b53f850b4d18898bf2d29d05cdf70aca2077da772c67bf040357c3f6556fb82399ddb2d7f7dfba5bc848bdf74f17a309
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ *.gem
2
+ *.sqlite3
3
+ .bundle
4
+ .rvmrc
5
+ .ruby-version
6
+ gemfiles/*.lock
7
+ pkg/*
8
+ *.rbc
9
+ tmp/*
10
+ .*.sw[a-z]
11
+ database.log
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ ActionFilter:
4
+ EnforcedStyle: filter
5
+
6
+ CaseIndentation:
7
+ IndentWhenRelativeTo: end
8
+
9
+ CollectionMethods:
10
+ PreferredMethods:
11
+ find: detect
12
+
13
+ EmptyLineBetweenDefs:
14
+ AllowAdjacentOneLineDefs: true
15
+
16
+ Encoding:
17
+ Enabled: false
18
+
19
+ EndAlignment:
20
+ AlignWith: variable
21
+
22
+ HashSyntax:
23
+ EnforcedStyle: hash_rockets
24
+
25
+ Style/IndentHash:
26
+ EnforcedStyle: consistent
27
+
28
+ Loop:
29
+ Enabled: false
30
+
31
+ PredicateName:
32
+ Enabled: false
33
+
34
+ RegexpLiteral:
35
+ Enabled: false
36
+
37
+ Semicolon:
38
+ AllowAsExpressionSeparator: true
39
+
40
+ Style/TrailingComma:
41
+ EnforcedStyleForMultiline: comma
42
+
43
+ Style/TrivialAccessors:
44
+ AllowDSLWriters: true
45
+ AllowPredicates: true
46
+ ExactNameMatch: true
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,56 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2014-09-25 20:13:18 -0700 using RuboCop version 0.26.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 27
9
+ Lint/AmbiguousRegexpLiteral:
10
+ Enabled: false
11
+
12
+ # Offense count: 1
13
+ Lint/HandleExceptions:
14
+ Enabled: false
15
+
16
+ # Offense count: 1
17
+ # Configuration parameters: CountComments.
18
+ Metrics/ClassLength:
19
+ Max: 324
20
+
21
+ # Offense count: 3
22
+ Metrics/CyclomaticComplexity:
23
+ Max: 14
24
+
25
+ # Offense count: 300
26
+ # Configuration parameters: AllowURI, URISchemes.
27
+ Metrics/LineLength:
28
+ Max: 180
29
+
30
+ # Offense count: 10
31
+ # Configuration parameters: CountComments.
32
+ Metrics/MethodLength:
33
+ Max: 52
34
+
35
+ # Offense count: 3
36
+ Metrics/PerceivedComplexity:
37
+ Max: 15
38
+
39
+ # Offense count: 80
40
+ Style/Documentation:
41
+ Enabled: false
42
+
43
+ # Offense count: 1
44
+ # Configuration parameters: Exclude.
45
+ Style/FileName:
46
+ Enabled: false
47
+
48
+ # Offense count: 1
49
+ # Configuration parameters: MinBodyLength.
50
+ Style/GuardClause:
51
+ Enabled: false
52
+
53
+ # Offense count: 2
54
+ # Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles.
55
+ Style/Next:
56
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+ env:
3
+ - CI=true
4
+ before_script:
5
+ - "bundle exec rake db:prepare"
6
+ rvm:
7
+ - 2.2.7
8
+ - 2.3.4
9
+ - 2.4.1
10
+ gemfile:
11
+ - gemfiles/rails42.gemfile
12
+ - gemfiles/rails5.gemfile
13
+ - gemfiles/rails51.gemfile
14
+ - gemfiles/rails52.gemfile
15
+ notifications:
16
+ recipients:
17
+ - gabriel.sobrinho@gmail.com
18
+ - thiago.pradi@gmail.com
data/Appraisals ADDED
@@ -0,0 +1,16 @@
1
+ appraise "rails42" do
2
+ gem "activerecord", "~> 4.2.0"
3
+ end
4
+
5
+ appraise "rails5" do
6
+ gem "activerecord", "~> 5.0.0"
7
+ end
8
+
9
+ appraise "rails51" do
10
+ gem "activerecord", "~> 5.1.0"
11
+ end
12
+
13
+ appraise "rails52" do
14
+ gem "activerecord", "~> 5.2.0"
15
+ end
16
+ # vim: ft=ruby
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ar-octopus.gemspec
4
+ gemspec
data/README.mkdn ADDED
@@ -0,0 +1,257 @@
1
+ # Important Notice:
2
+
3
+ Octopus will enter into maintainance mode once Rails 6 is released - since Rails 6 will include all the features for Database Sharding / Replication into Rails Core (PR: https://github.com/rails/rails/pull/34052).
4
+ Once the first version of Rails 6 beta is released, there will be a migration guide to help users migrate from Octopus to Rails 6.
5
+
6
+ # Octopus - Easy Database Sharding for ActiveRecord
7
+
8
+ [![Build Status](https://travis-ci.org/thiagopradi/octopus.svg)](https://travis-ci.org/thiagopradi/octopus) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/thiagopradi/octopus)
9
+
10
+ Octopus is a better way to do Database Sharding in ActiveRecord. Sharding allows multiple databases in the same rails application. While there are several projects that implement Sharding (e.g. DbCharmer, DataFabric, MultiDb), each project has its own limitations. The main goal of octopus project is to provide a better way of doing Database Sharding.
11
+
12
+ ## Feature list:
13
+
14
+ The api is designed to be simple as possible. Octopus focuses on the end user, giving the power of multiple databases but with reliable code and flexibility. Octopus is compatible with Rails 4 and Rails 5.
15
+
16
+ Octopus supports:
17
+
18
+ - Sharding (with multiple shards, and grouped shards).
19
+ - Replication (Master/slave support, with multiple slaves).
20
+ - Moving data between shards with migrations.
21
+ - Tools to manage database configurations. (soon)
22
+
23
+ ### Replication
24
+
25
+ When using replication, all writes queries will be sent to master, and read queries to slaves. More info could be found at: <a href="https://github.com/thiagopradi/octopus/wiki/replication"> Wiki</a>
26
+
27
+ ### Sharding
28
+
29
+ When using sharding, you need to specify which shard to send the query. Octopus supports selecting the shard inside a controller, or manually in each object. More could be found at <a href="https://github.com/thiagopradi/octopus/wiki/sharding"> Wiki</a>
30
+
31
+ ### Replication + Sharding
32
+
33
+ When using replication and sharding concurrently, you must specify a shard, and can optionally specify a <a href="https://github.com/thiagopradi/octopus/wiki/Slave-Groups">slave group</a>.
34
+ All write queries will be sent to each shard's master. If the slave group is specified read queries will be sent to slaves in it, or else to shard's master.
35
+ More info could be found at <a href="https://github.com/thiagopradi/octopus/wiki/Slave-Groups"> Wiki</a>
36
+
37
+ ## Install
38
+
39
+ Add this line to Gemfile:
40
+
41
+ ```
42
+ gem 'ar-octopus'
43
+ ```
44
+
45
+ Currently, Octopus doesn't support Rails 2. If you need support for rails 2, please use the version 0.5.0.
46
+
47
+ ## Upgrading
48
+
49
+ ### From < 0.5.0
50
+
51
+ Octopus < 0.5.0 stored schema version information in the master database defined in the database.yml file, and assumed
52
+ that each shard's schema matched the others and the master database. Beginning with Octopus 0.5.0, the schema version
53
+ information for each shard is stored within that shard's database.
54
+
55
+ If you are upgrading from < 0.5.0 run the `copy_schema_versions` rake task to copy the schema version information in the
56
+ master database to each of the shards:
57
+
58
+ ```bash
59
+ rake octopus:copy_schema_versions
60
+ ```
61
+
62
+ Once the task completes migrations will operate normally and schema information will be stored in each shard database
63
+ going forward.
64
+
65
+ ## How to use Octopus?
66
+
67
+ First, you need to create a config file, shards.yml, inside your config/ directory. to see the syntax and how this file should look, please checkout <a href="https://github.com/thiagopradi/octopus/wiki/config-file">this page on wiki</a>.
68
+
69
+ ### Syntax
70
+
71
+ Octopus adds a method to each AR Class and object: the using method is used to select the shard like this:
72
+
73
+ ```ruby
74
+ User.where(:name => "Boba").limit(3).using(:read_replica_one)
75
+ ```
76
+
77
+ Octopus also supports queries within a block. When you pass a block to the using method, all queries inside the block will be sent to the specified shard.
78
+
79
+ ```ruby
80
+ Octopus.using(:read_replica_two) do
81
+ User.create(:name => "Thiago")
82
+ end
83
+ ```
84
+
85
+ If you want to use the same code for all shards or all shards in a specific group (for example in `db/seeds.rb`), you can use this syntax.
86
+
87
+ ```ruby
88
+ # This will return a list of the given block's results, per shard.
89
+ Octopus.using_all do
90
+ User.create_from_csv!
91
+ end
92
+
93
+ # This will return a list of the given block's results, per shard in history_shards group.
94
+ Octopus.using_group(:history_shards) do
95
+ HistoryCategory.create_from_csv!
96
+ end
97
+ ```
98
+
99
+ Each model instance knows which shard it came from so this will work automatically:
100
+
101
+ ```ruby
102
+ # This will find the user in the shard1
103
+ @user = User.using(:shard1).find_by_name("Joao")
104
+
105
+ # This will find the user in the master database
106
+ @user2 = User.find_by_name("Jose")
107
+
108
+ #Sets the name
109
+ @user.name = "Mike"
110
+
111
+ # Save the user in the correct shard, shard1.
112
+ @user.save
113
+ ```
114
+
115
+ ### Migrations
116
+
117
+ In migrations, you also have access to the using method. The syntax is basically the same. This migration will run in the brazil and canada shards.
118
+
119
+ ```ruby
120
+ class CreateUsersOnBothShards < ActiveRecord::Migration
121
+ using(:brazil, :canada)
122
+
123
+ def self.up
124
+ User.create!(:name => "Both")
125
+ end
126
+
127
+ def self.down
128
+ User.delete_all
129
+ end
130
+ end
131
+ ```
132
+
133
+ You also could send a migration to a group of shards. This migration will be sent to all shards that belongs to history_shards group, specified in shards.yml:
134
+
135
+ ```ruby
136
+ class CreateUsersOnMultiplesGroups < ActiveRecord::Migration
137
+ using_group(:history_shards)
138
+
139
+ def self.up
140
+ User.create!(:name => "MultipleGroup")
141
+ end
142
+
143
+ def self.down
144
+ User.delete_all
145
+ end
146
+ end
147
+ ```
148
+
149
+ You can specify a `default_migration_group` for migrations, so that modifications to each individual migration file are not needed:
150
+
151
+ ```yaml
152
+ octopus:
153
+ default_migration_group: europe_databases
154
+ ```
155
+
156
+ There is no need for a corresponding `default_migration_shard` - simply define that database to be your master. You might want this setting if all of your databases have identical schemas, but are not replicated.
157
+
158
+ You can configure a master shard for the rails application, to connect to when rails is going up. The safest would be to configure this to the shard specified in `database.yml` (some things still use it).
159
+
160
+ ```yaml
161
+ octopus:
162
+ master_shard: <%= ENV['SHARD'] || 'shard1' %>
163
+ ```
164
+
165
+ Then you can use the `SHARD` environment variable to override the `master_shard` specified in `config/shards.yml`, useful for running rake tasks.
166
+
167
+ ```bash
168
+ SHARD=shard1 rake db:setup && SHARD=shard2 rake db:setup
169
+ ```
170
+
171
+ ### Rails Controllers
172
+
173
+ If you want to send a specified action, or all actions from a controller, to a specific shard, use this syntax:
174
+
175
+ ```ruby
176
+ class ApplicationController < ActionController::Base
177
+ around_filter :select_shard
178
+
179
+ def select_shard(&block)
180
+ Octopus.using(:brazil, &block)
181
+ end
182
+ end
183
+ ```
184
+
185
+ To see the complete list of features and syntax, please check out our <a href="https://github.com/thiagopradi/octopus/wiki/"> Wiki</a>
186
+ Want to see sample rails applications using octopus features? please check it out: <a href="http://github.com/thiagopradi/octopus_sharding_example">Sharding Example</a> and <a href="http://github.com/thiagopradi/octopus_replication_example">Replication Example</a>. Also, we have an example that shows how to use Octopus without Rails: <a href="http://github.com/thiagopradi/octopus_sinatra"> Octopus + Sinatra Example</a>.
187
+
188
+ ## Mixing Octopus with the Rails multiple database model
189
+
190
+ If you want to set a custom connection to a specific model, use the syntax `octopus_establish_connection` syntax:
191
+
192
+ ```ruby
193
+ #This class sets its own connection
194
+ class CustomConnection < ActiveRecord::Base
195
+ octopus_establish_connection(:adapter => "mysql", :database => "octopus_shard2")
196
+ end
197
+ ```
198
+
199
+ ### allow_shard
200
+
201
+ If you'd like to use specific shards with a model that has a Rails-managed connection, you can use `allow_shard`:
202
+
203
+ ```ruby
204
+ class CustomConnectedModel
205
+ octopus_establish_connection(...)
206
+ allow_shard :my_shard
207
+ end
208
+
209
+ #This uses :my_shard
210
+ CustomConnectedModel.using(:my_shard).first
211
+
212
+ #This uses the Rails-managed connection pool (the call to 'using' is ignored)
213
+ CustomConnectedModel.using(:some_other_shard).first
214
+ ```
215
+
216
+ This can be useful if you have a model that lives in a separate database and would like to add sharding or replication to it. For other use cases, you may be better off with <a href="https://github.com/thiagopradi/octopus/wiki/Slave-Groups">slave groups</a>.
217
+
218
+ ## Contributing with Octopus
219
+
220
+ Contributors are welcome! To run the test suite, you need mysql, postgresql and sqlite3 installed. This is what you need to setup your Octopus development environment:
221
+
222
+ ```bash
223
+ git clone http://github.com/thiagopradi/octopus.git
224
+ cd octopus
225
+ bundle install
226
+ bundle exec rake db:prepare
227
+ bundle exec rake appraisal:install
228
+ bundle exec rake spec
229
+ ```
230
+
231
+ This command will run the spec suite for all rails versions supported.
232
+ To run our integrations tests inside sample_app, you need to following commands:
233
+
234
+ ```bash
235
+ cd sample_app
236
+ bundle install
237
+ cucumber
238
+ ```
239
+
240
+ If you are having issues running the octopus spec suite, verify your database users and passwords match those inside the config files and your permissions are correct.
241
+
242
+ ## Contributors:
243
+
244
+ - <a href="https://github.com/thiagopradi/octopus/contributors">All Contributors</a>
245
+
246
+ ## Mailing List:
247
+
248
+ - <a href="http://groups.google.com/group/octopus-activerecord/">Octopus Mailing List</a>
249
+
250
+ ## Thanks
251
+
252
+ This project is sponsored by the <a href="http://www.rubysoc.org">Ruby Summer of Code</a>, Rapid River Software,
253
+ and my mentors <a href="http://github.com/mperham">Mike Perham</a> and <a href="http://github.com/amitagarwal">Amit Agarwal</a>.
254
+
255
+ ## Copyright
256
+
257
+ Copyright (c) Thiago Pradi, released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,175 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+ require 'appraisal'
5
+
6
+ RSpec::Core::RakeTask.new
7
+ RuboCop::RakeTask.new
8
+
9
+ task :default => [:spec]
10
+
11
+ namespace :db do
12
+ desc 'Build the databases for tests'
13
+ task :build_databases do
14
+ pg_spec = {
15
+ :adapter => 'postgresql',
16
+ :host => 'localhost',
17
+ :username => (ENV['POSTGRES_USER'] || 'postgres'),
18
+ :encoding => 'utf8',
19
+ :password => ''
20
+ }
21
+
22
+ mysql_spec = {
23
+ :adapter => 'mysql2',
24
+ :host => 'localhost',
25
+ :username => (ENV['MYSQL_USER'] || 'root'),
26
+ :encoding => 'utf8',
27
+ :password => ''
28
+ }
29
+
30
+ ` rm -f /tmp/database.sqlite3 `
31
+
32
+ require 'active_record'
33
+
34
+ # Connects to PostgreSQL
35
+ ActiveRecord::Base.establish_connection(pg_spec.merge('database' => 'postgres', 'schema_search_path' => 'public'))
36
+ (1..2).map do |i|
37
+ # drop the old database (if it exists)
38
+ ActiveRecord::Base.connection.drop_database("octopus_shard_#{i}")
39
+ # create new database
40
+ ActiveRecord::Base.connection.create_database("octopus_shard_#{i}")
41
+ end
42
+
43
+ # Connect to MYSQL
44
+ ActiveRecord::Base.establish_connection(mysql_spec)
45
+ (1..5).map do |i|
46
+ # drop the old database (if it exists)
47
+ ActiveRecord::Base.connection.drop_database("octopus_shard_#{i}")
48
+ # create new database
49
+ ActiveRecord::Base.connection.create_database("octopus_shard_#{i}")
50
+ end
51
+ end
52
+
53
+ desc 'Create tables on tests databases'
54
+ task :create_tables do
55
+ require 'octopus'
56
+ # Set the octopus variable directory to spec dir, in order to load the config/shards.yml file.
57
+ Octopus.instance_variable_set(:@directory, "#{File.dirname(__FILE__)}/spec/")
58
+
59
+ # Require the database connection
60
+ require "#{File.dirname(__FILE__)}/spec/support/database_connection"
61
+
62
+ shard_symbols = [:master, :brazil, :canada, :russia, :alone_shard, :postgresql_shard, :sqlite_shard]
63
+ shard_symbols << :protocol_shard
64
+ shard_symbols.each do |shard_symbol|
65
+ # Rails 3.1 needs to do some introspection around the base class, which requires
66
+ # the model be a descendent of ActiveRecord::Base.
67
+ class BlankModel < ActiveRecord::Base; end
68
+
69
+ BlankModel.using(shard_symbol).connection.initialize_schema_migrations_table
70
+ BlankModel.using(shard_symbol).connection.initialize_metadata_table if Octopus.atleast_rails50?
71
+
72
+ BlankModel.using(shard_symbol).connection.create_table(:users) do |u|
73
+ u.string :name
74
+ u.integer :number
75
+ u.boolean :admin
76
+ u.datetime :created_at
77
+ u.datetime :updated_at
78
+ end
79
+
80
+ BlankModel.using(shard_symbol).connection.create_table(:clients) do |u|
81
+ u.string :country
82
+ u.string :name
83
+ end
84
+
85
+ BlankModel.using(shard_symbol).connection.create_table(:cats) do |u|
86
+ u.string :name
87
+ end
88
+
89
+ BlankModel.using(shard_symbol).connection.create_table(:items) do |u|
90
+ u.string :name
91
+ u.integer :client_id
92
+ end
93
+
94
+ BlankModel.using(shard_symbol).connection.create_table(:computers) do |u|
95
+ u.string :name
96
+ end
97
+
98
+ BlankModel.using(shard_symbol).connection.create_table(:keyboards) do |u|
99
+ u.string :name
100
+ u.integer :computer_id
101
+ end
102
+
103
+ BlankModel.using(shard_symbol).connection.create_table(:roles) do |u|
104
+ u.string :name
105
+ end
106
+
107
+ BlankModel.using(shard_symbol).connection.create_table(:permissions) do |u|
108
+ u.string :name
109
+ end
110
+
111
+ BlankModel.using(shard_symbol).connection.create_table(:permissions_roles, :id => false) do |u|
112
+ u.integer :role_id
113
+ u.integer :permission_id
114
+ end
115
+
116
+ BlankModel.using(shard_symbol).connection.create_table(:assignments) do |u|
117
+ u.integer :programmer_id
118
+ u.integer :project_id
119
+ end
120
+
121
+ BlankModel.using(shard_symbol).connection.create_table(:programmers) do |u|
122
+ u.string :name
123
+ end
124
+
125
+ BlankModel.using(shard_symbol).connection.create_table(:projects) do |u|
126
+ u.string :name
127
+ end
128
+
129
+ BlankModel.using(shard_symbol).connection.create_table(:comments) do |u|
130
+ u.string :name
131
+ u.string :commentable_type
132
+ u.integer :commentable_id
133
+ u.boolean :open, default: false
134
+ end
135
+
136
+ BlankModel.using(shard_symbol).connection.create_table(:parts) do |u|
137
+ u.string :name
138
+ u.integer :item_id
139
+ end
140
+
141
+ BlankModel.using(shard_symbol).connection.create_table(:yummy) do |u|
142
+ u.string :name
143
+ end
144
+
145
+ BlankModel.using(shard_symbol).connection.create_table(:adverts) do |u|
146
+ u.string :name
147
+ end
148
+
149
+ BlankModel.using(shard_symbol).connection.create_table(:custom) do |u|
150
+ u.string :value
151
+ end
152
+
153
+ if shard_symbol == :alone_shard
154
+ BlankModel.using(shard_symbol).connection.create_table(:mmorpg_players) do |u|
155
+ u.string :player_name
156
+ end
157
+
158
+ BlankModel.using(shard_symbol).connection.create_table(:weapons) do |u|
159
+ u.integer :mmorpg_player_id
160
+ u.string :name
161
+ u.string :hand
162
+ end
163
+
164
+ BlankModel.using(shard_symbol).connection.create_table(:skills) do |u|
165
+ u.integer :mmorpg_player_id
166
+ u.integer :weapon_id
167
+ u.string :name
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ desc 'Prepare the test databases'
174
+ task :prepare => [:build_databases, :create_tables]
175
+ end
data/TODO.txt ADDED
@@ -0,0 +1,7 @@
1
+ - Find the best way to move data along shards.
2
+ - Improve documentation.
3
+ - Automatic configuration file for shards.
4
+ - Support specifics cases of sharding/replication.
5
+ - Basic algorithm to do load balancing between slaves server (handling servers down)
6
+ - Support replication + sharding
7
+ - Wiki about validation
@@ -0,0 +1,44 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'octopus/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'ar-octopus-ruby-3'
7
+ s.version = Octopus::VERSION
8
+ s.authors = ['Thiago Pradi', 'Mike Perham', 'Gabriel Sobrinho']
9
+ s.email = ['tchandy@gmail.com', 'mperham@gmail.com', 'gabriel.sobrinho@gmail.com']
10
+ s.homepage = 'https://github.com/tchandy/octopus'
11
+ s.summary = 'Easy Database Sharding for ActiveRecord'
12
+ s.description = 'This gem allows you to use sharded databases with ActiveRecord. This also provides a interface for replication and for running migrations with multiples shards.'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
17
+ s.require_paths = ['lib']
18
+
19
+ s.post_install_message = "Important: If you are upgrading from < Octopus 0.5.0 you need to run:\n" \
20
+ "$ rake octopus:copy_scha_versions\n\n" \
21
+ 'Octopus now stores schema version information in each shard and migrations will not ' \
22
+ 'work properly unless this task is invoked.'
23
+
24
+ s.required_ruby_version = '>= 2.2.0'
25
+
26
+ s.add_dependency 'activerecord', "~> 6.0.6.1"
27
+ s.add_dependency 'activesupport', "~> 6.0.6.1"
28
+ # s.add_dependency 'pg', "~> 1.1.0"
29
+
30
+ s.add_development_dependency 'appraisal', '>= 0.3.8'
31
+ # To install the mysql2 gem its necessary execute the following command before run bundle
32
+ # $ sudo apt-get install libmysqlclient-dev
33
+ s.add_development_dependency 'mysql2', '~> 0.5'
34
+ s.add_development_dependency 'pg', '~> 0.18'
35
+ s.add_development_dependency 'rake'
36
+ s.add_development_dependency 'rspec', '>= 3'
37
+ s.add_development_dependency 'rubocop'
38
+ # To install the sqlite3 gem its necessary execute the following command before run bundle
39
+ # $ sudo apt-get install -y sqlite3 libsqlite3-dev
40
+ s.add_development_dependency 'sqlite3', '~> 1.4'
41
+ s.add_development_dependency 'pry-byebug'
42
+
43
+ s.license = 'MIT'
44
+ end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2.0"
6
+ gem "mysql2", "0.4.10"
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.2.0"
6
+
7
+ gemspec path: "../"