activerecord-turntable 2.5.0 → 3.0.0.alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +7 -0
  3. data/.travis.yml +3 -7
  4. data/CHANGELOG.md +4 -14
  5. data/Gemfile +3 -0
  6. data/Guardfile +12 -7
  7. data/README.md +11 -19
  8. data/Rakefile +14 -15
  9. data/activerecord-turntable.gemspec +24 -27
  10. data/gemfiles/rails5_0.gemfile +6 -0
  11. data/lib/active_record/turntable/active_record_ext/abstract_adapter.rb +24 -20
  12. data/lib/active_record/turntable/active_record_ext/activerecord_import_ext.rb +6 -16
  13. data/lib/active_record/turntable/active_record_ext/acts_as_archive_extension.rb +25 -16
  14. data/lib/active_record/turntable/active_record_ext/association.rb +33 -14
  15. data/lib/active_record/turntable/active_record_ext/association_preloader.rb +4 -24
  16. data/lib/active_record/turntable/active_record_ext/clever_load.rb +2 -2
  17. data/lib/active_record/turntable/active_record_ext/connection_handler_extension.rb +11 -15
  18. data/lib/active_record/turntable/active_record_ext/database_tasks.rb +9 -9
  19. data/lib/active_record/turntable/active_record_ext/fixtures.rb +11 -41
  20. data/lib/active_record/turntable/active_record_ext/locking_optimistic.rb +40 -147
  21. data/lib/active_record/turntable/active_record_ext/log_subscriber.rb +6 -37
  22. data/lib/active_record/turntable/active_record_ext/migration_proxy.rb +1 -1
  23. data/lib/active_record/turntable/active_record_ext/persistence.rb +54 -148
  24. data/lib/active_record/turntable/active_record_ext/relation.rb +17 -45
  25. data/lib/active_record/turntable/active_record_ext/schema_dumper.rb +80 -78
  26. data/lib/active_record/turntable/active_record_ext/sequencer.rb +6 -15
  27. data/lib/active_record/turntable/active_record_ext/transactions.rb +14 -9
  28. data/lib/active_record/turntable/active_record_ext.rb +15 -16
  29. data/lib/active_record/turntable/algorithm/modulo_algorithm.rb +1 -1
  30. data/lib/active_record/turntable/algorithm/range_algorithm.rb +9 -9
  31. data/lib/active_record/turntable/algorithm/range_bsearch_algorithm.rb +12 -12
  32. data/lib/active_record/turntable/base.rb +27 -14
  33. data/lib/active_record/turntable/cluster.rb +10 -13
  34. data/lib/active_record/turntable/cluster_helper_methods.rb +6 -6
  35. data/lib/active_record/turntable/config.rb +3 -3
  36. data/lib/active_record/turntable/connection_proxy/mixable.rb +1 -1
  37. data/lib/active_record/turntable/connection_proxy.rb +23 -22
  38. data/lib/active_record/turntable/helpers/test_helper.rb +4 -4
  39. data/lib/active_record/turntable/master_shard.rb +12 -7
  40. data/lib/active_record/turntable/migration.rb +41 -47
  41. data/lib/active_record/turntable/mixer/fader/calculate_shards_sum_result.rb +7 -7
  42. data/lib/active_record/turntable/mixer/fader/select_shards_merge_result.rb +12 -12
  43. data/lib/active_record/turntable/mixer.rb +121 -121
  44. data/lib/active_record/turntable/plugin.rb +1 -1
  45. data/lib/active_record/turntable/pool_proxy.rb +7 -19
  46. data/lib/active_record/turntable/query_cache.rb +41 -0
  47. data/lib/active_record/turntable/railtie.rb +7 -5
  48. data/lib/active_record/turntable/railties/databases.rake +19 -20
  49. data/lib/active_record/turntable/seq_shard.rb +15 -15
  50. data/lib/active_record/turntable/sequencer/api.rb +7 -7
  51. data/lib/active_record/turntable/sequencer/barrage.rb +6 -7
  52. data/lib/active_record/turntable/sequencer/mysql.rb +2 -2
  53. data/lib/active_record/turntable/sequencer.rb +15 -15
  54. data/lib/active_record/turntable/shard.rb +23 -20
  55. data/lib/active_record/turntable/sql_tree_patch.rb +59 -57
  56. data/lib/active_record/turntable/util.rb +1 -21
  57. data/lib/active_record/turntable/version.rb +1 -1
  58. data/lib/active_record/turntable.rb +14 -19
  59. data/lib/activerecord-turntable.rb +2 -2
  60. data/script/performance/algorithm +8 -9
  61. data/spec/active_record/turntable/active_record_ext/association_preloader_spec.rb +4 -4
  62. data/spec/active_record/turntable/active_record_ext/association_spec.rb +5 -14
  63. data/spec/active_record/turntable/active_record_ext/clever_load_spec.rb +6 -6
  64. data/spec/active_record/turntable/active_record_ext/fixture_set_spec.rb +4 -4
  65. data/spec/active_record/turntable/active_record_ext/locking_optimistic_spec.rb +2 -15
  66. data/spec/active_record/turntable/active_record_ext/migration_spec.rb +4 -4
  67. data/spec/active_record/turntable/active_record_ext/persistence_spec.rb +17 -15
  68. data/spec/active_record/turntable/active_record_ext/sequencer_spec.rb +1 -1
  69. data/spec/active_record/turntable/active_record_ext/test_fixtures_spec.rb +3 -3
  70. data/spec/active_record/turntable/algorithm/modulo_algorithm_spec.rb +2 -3
  71. data/spec/active_record/turntable/algorithm/range_algorithm_spec.rb +2 -3
  72. data/spec/active_record/turntable/algorithm/range_bsearch_algorithm_spec.rb +2 -3
  73. data/spec/active_record/turntable/algorithm_spec.rb +5 -5
  74. data/spec/active_record/turntable/base_spec.rb +1 -1
  75. data/spec/active_record/turntable/cluster_spec.rb +1 -1
  76. data/spec/active_record/turntable/config_spec.rb +1 -1
  77. data/spec/active_record/turntable/connection_proxy_spec.rb +16 -17
  78. data/spec/active_record/turntable/finder_spec.rb +1 -1
  79. data/spec/active_record/turntable/mixer/fader_spec.rb +1 -1
  80. data/spec/active_record/turntable/mixer_spec.rb +2 -4
  81. data/spec/active_record/turntable/query_cache_spec.rb +28 -0
  82. data/spec/active_record/turntable/sequencer/api_spec.rb +4 -4
  83. data/spec/active_record/turntable/sequencer/barrage_spec.rb +2 -2
  84. data/spec/active_record/turntable/sequencer/mysql_spec.rb +1 -1
  85. data/spec/active_record/turntable/shard_spec.rb +1 -1
  86. data/spec/active_record/turntable/sql_tree_patch_spec.rb +2 -2
  87. data/spec/active_record/turntable/transaction_spec.rb +2 -2
  88. data/spec/active_record/turntable_spec.rb +3 -3
  89. data/spec/migrations/002_create_user_statuses.rb +4 -4
  90. data/spec/migrations/003_create_cards.rb +3 -3
  91. data/spec/migrations/004_create_cards_users.rb +2 -2
  92. data/spec/models/user_status.rb +0 -1
  93. data/spec/spec_helper.rb +15 -14
  94. data/spec/support/turntable_helper.rb +4 -4
  95. metadata +98 -59
  96. data/gemfiles/rails4_0.gemfile +0 -7
  97. data/gemfiles/rails4_1.gemfile +0 -7
  98. data/gemfiles/rails4_2.gemfile +0 -7
  99. data/lib/active_record/turntable/rack/connection_management.rb +0 -18
  100. data/lib/active_record/turntable/rack/query_cache.rb +0 -40
  101. data/lib/active_record/turntable/rack.rb +0 -8
  102. data/spec/active_record/turntable/rack/query_cache_spec.rb +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bae4fd19e4b7b55c12f1793b954e711e0c9693b
4
- data.tar.gz: b5da25ad60b9b02b3d6483f59d940a87a832d65f
3
+ metadata.gz: af3117e04f949c697cf878879a85c2205f7e19b6
4
+ data.tar.gz: 5437cc1aec6b98bb507639d81e9687eac526d8d1
5
5
  SHA512:
6
- metadata.gz: 7bef5cc2a9020bfb5f7158cef8c6935359c3bc77d8e9f7b28dfc88375eccdb9dfde5f7ae2becbf48fc07af6fd793b492f3c13735c982cfab9f1e501939c6550a
7
- data.tar.gz: 3fda72ec78a460652cd460426f04a35bdb473f96f3bcd1241e594fc5ba401e5c619789dbe28d16652dd649eb78b98bb60ebd98919a691bee43eab4c25c8814c8
6
+ metadata.gz: 039ffd448816f9792c8fb29fb7844e08ab0c1ea7227367158810dc718d580ee047922d1e23117875611e3d606d6a1b3a3494d732e962dc84e774a0755240a8bd
7
+ data.tar.gz: 939620c1056efc1b1f843dc348c5420ea811c87987a5149565b8049ed488689f4b224c28c962316b201c58cd33ebdba0cdf85ee42fb7623225561091ce1275a9
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ inherit_gem:
2
+ onkcop:
3
+ - "config/rubocop.yml"
4
+ - "config/rspec.yml"
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.2
data/.travis.yml CHANGED
@@ -1,14 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
- - 2.1
5
- - 2.2
6
- - 2.3.3
3
+ - 2.2.5
4
+ - 2.3.1
7
5
  - ruby-head
8
6
  gemfile:
9
- - gemfiles/rails4_0.gemfile
10
- - gemfiles/rails4_1.gemfile
11
- - gemfiles/rails4_2.gemfile
7
+ - gemfiles/rails5_0.gemfile
12
8
  before_script:
13
9
  - bundle exec rake turntable:db:reset
14
10
  script: bundle exec rake spec
data/CHANGELOG.md CHANGED
@@ -1,19 +1,9 @@
1
- ## activerecord-turntable 2.5.0 ##
1
+ ## activerecord-turntable 3.0.0.alpha (unreleased) ##
2
2
 
3
- ### Improvement
4
-
5
- * Fix to propagate shard conditions to `AssociationRelation` too
6
-
7
- ## activerecord-turntable 2.4.0 ##
8
-
9
- ### Incompatible Change
10
-
11
- * Drop support for ruby 1.9.3
12
-
13
- ### Bugfix
3
+ * Rails5 compatibility
4
+ * Minimum ruby requirement version is `2.2.2`
5
+ * Rails 4.x support has been dropped.
14
6
 
15
- * Update activerecord 4.2 patches
16
- * Fixes optimistic locking with a serialized column causes JSON::Error.
17
7
 
18
8
  ## activerecord-turntable 2.3.3 ##
19
9
 
data/Gemfile CHANGED
@@ -1,3 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ gem "activerecord", "5.0.0.1"
6
+ gem "activesupport", "5.0.0.1"
data/Guardfile CHANGED
@@ -1,11 +1,16 @@
1
1
  # A sample Guardfile
2
2
  # More info at https://github.com/guard/guard#readme
3
3
 
4
- guard 'rspec',
5
- cmd: "bundle exec rspec",
6
- all_after_pass: true,
7
- all_on_start: true do
8
- watch(%r{^spec/.+_spec\.rb$})
9
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
10
- watch('spec/spec_helper.rb') { "spec" }
4
+ guard "rspec",
5
+ cmd: "bundle exec rspec",
6
+ all_after_pass: true,
7
+ all_on_start: true do
8
+ watch(%r{^spec/.+_spec\.rb$})
9
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
10
+ watch("spec/spec_helper.rb") { "spec" }
11
+ end
12
+
13
+ guard :rubocop do
14
+ watch(%r{.+\.rb$})
15
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
11
16
  end
data/README.md CHANGED
@@ -9,9 +9,12 @@ ActiveRecord::Turntable is a database sharding extension for ActiveRecord.
9
9
 
10
10
  ## Dependencies
11
11
 
12
- activerecord(>=4.0.0)
12
+ activerecord(>=5.0.0, <6.0)
13
13
 
14
- if you are using activerecord 3.x, please use activerecord-turntable version 1.x.
14
+ If you are using with older activerecord versions, use following versions.
15
+
16
+ * activerecord 3.x - use activerecord-turntable version 1.x.
17
+ * activerecord 4.x - use activerecord-turntable version 2.x.
15
18
 
16
19
  ## Supported Database
17
20
 
@@ -22,7 +25,7 @@ Currently supports mysql only.
22
25
  Add to Gemfile:
23
26
 
24
27
  ```ruby
25
- gem 'activerecord-turntable', '~> 2.1.1'
28
+ gem 'activerecord-turntable', '~> 3.0.0.alpha1'
26
29
  ```
27
30
 
28
31
  Run a bundle install:
@@ -136,7 +139,7 @@ Edit turntable.yml and database.yml. See below example config.
136
139
  database: sample_app_user3_development
137
140
  ```
138
141
 
139
- ### Example Migration
142
+ ### Example Migration
140
143
 
141
144
  Generate a model:
142
145
 
@@ -282,7 +285,7 @@ First, add configuration to turntable.yml and database.yml
282
285
  seq:
283
286
  user_seq: # <-- sequencer name
284
287
  seq_type: mysql # <-- sequencer type
285
- connection: user_seq_1 # <-- sequencer database connection
288
+ connection: user_seq_1 # <-- sequencer database connection
286
289
  ```
287
290
 
288
291
  Add below to the migration:
@@ -369,8 +372,8 @@ end
369
372
  transaction helper to execute transaction to all shards in the cluster:
370
373
 
371
374
  ```ruby
372
- User.user_cluster_transaction do
373
- # Transaction is opened all shards in "user_cluster"
375
+ User.user_cluster_transaction do
376
+ # Transaction is opened all shards in "user_cluster"
374
377
  end
375
378
  ```
376
379
 
@@ -432,23 +435,12 @@ Use with_all method:
432
435
  end
433
436
  ```
434
437
 
435
- ### Connection Management
436
-
437
- Rails's ConnectionManagement middleware keeps ActiveRecord's connection during the process is alive, but Turntable keeps more connections.
438
- This may cause flooding max connections on your database. So, we made a middleware that disconnects on each request.
439
-
440
- if you use turntable's ConnectionManagement middleware, add below line to your initializer.
441
-
442
- ```ruby
443
- app.middleware.swap ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::Turntable::Rack::ConnectionManagement
444
- ```
445
-
446
438
  ### Performance Exception
447
439
 
448
440
  To notice queries causing performance problem, Turntable has follow options.
449
441
 
450
442
  * raise\_on\_not\_specified\_shard\_query - raises on queries execute on all shards
451
- * raise\_on\_not\_specified\_shard\_update - raises on updates executed on all shards
443
+ * raise\_on\_not\_specified\_shard\_update - raises on updates executed on all shards
452
444
 
453
445
 
454
446
  Add to turntable.yml:
data/Rakefile CHANGED
@@ -1,25 +1,25 @@
1
1
  require "bundler/gem_tasks"
2
- require 'rubygems'
2
+ require "rubygems"
3
3
 
4
- require 'rspec/core'
5
- require 'rspec/core/rake_task'
4
+ require "rspec/core"
5
+ require "rspec/core/rake_task"
6
6
  RSpec::Core::RakeTask.new(:spec) do |spec|
7
- spec.pattern = FileList['spec/**/*_spec.rb']
7
+ spec.pattern = FileList["spec/**/*_spec.rb"]
8
8
  end
9
9
 
10
- require 'active_record'
10
+ require "active_record"
11
11
  require "active_record/turntable/active_record_ext/database_tasks"
12
12
 
13
13
  namespace :turntable do
14
14
  namespace :db do
15
15
  task :rails_env do
16
16
  unless defined? RAILS_ENV
17
- RAILS_ENV = ENV['RAILS_ENV'] ||= 'test'
17
+ RAILS_ENV = ENV["RAILS_ENV"] ||= "test"
18
18
  end
19
19
  end
20
20
 
21
21
  task :load_config => :rails_env do
22
- yaml_file = File.join(File.dirname(__FILE__), 'spec/config/database.yml')
22
+ yaml_file = File.join(File.dirname(__FILE__), "spec/config/database.yml")
23
23
  ActiveRecord::Base.configurations = YAML.load ERB.new(IO.read(yaml_file)).result
24
24
  end
25
25
 
@@ -38,12 +38,12 @@ namespace :turntable do
38
38
  desc "migrate turntable test tables"
39
39
  task :migrate => :load_config do
40
40
  ActiveRecord::Base.establish_connection RAILS_ENV.to_sym
41
- require 'active_record/turntable'
42
- ActiveRecord::Base.send(:include, ActiveRecord::Turntable)
43
- ActiveRecord::ConnectionAdapters::SchemaStatements.send(:include, ActiveRecord::Turntable::Migration::SchemaStatementsExt)
41
+ require "active_record/turntable"
42
+ ActiveRecord::Base.include(ActiveRecord::Turntable)
43
+ ActiveRecord::ConnectionAdapters::SchemaStatements.include(ActiveRecord::Turntable::Migration::SchemaStatementsExt)
44
44
 
45
45
  configurations = [ActiveRecord::Base.configurations[RAILS_ENV]]
46
- configurations += ActiveRecord::Tasks::DatabaseTasks.current_turntable_cluster_configurations(RAILS_ENV).map {|v| v[1]}.flatten.uniq
46
+ configurations += ActiveRecord::Tasks::DatabaseTasks.current_turntable_cluster_configurations(RAILS_ENV).map { |v| v[1] }.flatten.uniq
47
47
 
48
48
  configurations.each do |configuration|
49
49
  ActiveRecord::Base.establish_connection configuration
@@ -62,7 +62,6 @@ namespace :turntable do
62
62
  t.belongs_to :user, :null => false
63
63
  t.integer :hp, :null => false, :default => 0
64
64
  t.integer :mp, :null => false, :default => 0
65
- t.text :data
66
65
  t.integer :lock_version, :null => false, :default => 0
67
66
  t.datetime :deleted_at, :default => nil
68
67
  t.timestamps
@@ -101,15 +100,15 @@ namespace :turntable do
101
100
 
102
101
  ActiveRecord::Base.connection.create_table :cards_users_histories do |t|
103
102
  t.belongs_to :cards_user, :null => false
104
- t.belongs_to :user, :null => false
103
+ t.belongs_to :user, :null => false
105
104
  t.timestamps
106
105
  end
107
106
  ActiveRecord::Base.connection.create_sequence_for :cards_users_histories
108
107
 
109
108
  ActiveRecord::Base.connection.create_table :events_users_histories do |t|
110
- t.belongs_to :events_user, :null => false
109
+ t.belongs_to :events_user, :null => false
111
110
  t.belongs_to :cards_user, :null => false
112
- t.belongs_to :user, :null => false
111
+ t.belongs_to :user, :null => false
113
112
  t.timestamps
114
113
  end
115
114
  ActiveRecord::Base.connection.create_sequence_for :events_users_histories
@@ -1,56 +1,53 @@
1
- $:.push File.expand_path("../lib", __FILE__)
1
+ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
2
  require "active_record/turntable/version"
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "activerecord-turntable"
6
6
  spec.version = ActiveRecord::Turntable::VERSION
7
- spec.authors = ["gussan", "sue445"]
7
+ spec.authors = %w(gussan sue445)
8
8
  spec.homepage = "https://github.com/drecom/activerecord-turntable"
9
- spec.summary = %q{ActiveRecord sharding extension}
10
- spec.description = %q{ActiveRecord sharding extension}
9
+ spec.summary = "ActiveRecord sharding extension"
10
+ spec.description = "ActiveRecord sharding extension"
11
11
  spec.license = "MIT"
12
12
 
13
13
  spec.rubyforge_project = "activerecord-turntable"
14
14
  spec.extra_rdoc_files = [
15
15
  "LICENSE.txt",
16
16
  "README.md",
17
- "CHANGELOG.md"
17
+ "CHANGELOG.md",
18
18
  ]
19
19
 
20
- spec.files = `git ls-files`.split($/)
20
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
21
21
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
23
  spec.require_paths = ["lib"]
24
- spec.required_ruby_version = '>= 2.0.0'
24
+ spec.required_ruby_version = ">= 2.2.2"
25
25
 
26
- spec.add_runtime_dependency "activerecord", ">= 4.0.0", "< 5.0"
27
- spec.add_runtime_dependency "activesupport", ">= 4.0.0", "< 5.0"
28
- spec.add_runtime_dependency "sql_tree", "= 0.2.0"
26
+ spec.add_runtime_dependency "activerecord", ">= 5.0", "< 6.0"
27
+ spec.add_runtime_dependency "activesupport", ">= 5.0", "< 6.0"
29
28
  spec.add_runtime_dependency "bsearch", "~> 1.5"
30
29
  spec.add_runtime_dependency "httpclient", ">= 0"
30
+ spec.add_runtime_dependency "sql_tree", "= 0.2.0"
31
31
 
32
32
  # optional dependencies
33
33
  spec.add_development_dependency "activerecord-import"
34
34
  spec.add_development_dependency "barrage"
35
+ spec.add_development_dependency "coveralls"
36
+ spec.add_development_dependency "fabrication"
37
+ spec.add_development_dependency "faker"
38
+ spec.add_development_dependency "guard-rspec"
39
+ spec.add_development_dependency "guard-rubocop"
35
40
  spec.add_development_dependency "mysql2"
36
-
41
+ spec.add_development_dependency "onkcop"
42
+ spec.add_development_dependency "pry"
43
+ spec.add_development_dependency "pry-byebug"
44
+ spec.add_development_dependency "rack"
37
45
  spec.add_development_dependency "rake"
38
- spec.add_development_dependency "rack", "< 2"
39
- spec.add_development_dependency "rspec", "~> 3.5.0"
40
- spec.add_development_dependency "rspec-its"
46
+ spec.add_development_dependency "rspec", "~> 3.1.0"
41
47
  spec.add_development_dependency "rspec-collection_matchers"
42
- spec.add_development_dependency "fabrication"
43
- spec.add_development_dependency "faker"
48
+ spec.add_development_dependency "rspec-its"
49
+ spec.add_development_dependency "rubocop"
50
+ spec.add_development_dependency "rubocop-rspec"
51
+ spec.add_development_dependency "timecop"
44
52
  spec.add_development_dependency "webmock"
45
- spec.add_development_dependency "pry"
46
- if RUBY_VERSION > "2.0"
47
- spec.add_development_dependency "pry-byebug"
48
- end
49
- spec.add_development_dependency "guard-rspec"
50
- spec.add_development_dependency "listen", "= 3.0.6"
51
- spec.add_development_dependency "coveralls"
52
-
53
- if RUBY_PLATFORM =~ /darwin/
54
- spec.add_development_dependency "growl"
55
- end
56
53
  end
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "5.0.0.1"
4
+ gem "activesupport", "5.0.0.1"
5
+
6
+ gemspec :path => '../'
@@ -1,30 +1,34 @@
1
1
  module ActiveRecord::Turntable
2
2
  module ActiveRecordExt
3
3
  module AbstractAdapter
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- protected
8
-
9
- # @note override for logging current shard name
10
- def log(sql, name = "SQL", binds = [], statement_name = nil)
11
- @instrumenter.instrument(
12
- "sql.active_record",
13
- :sql => sql,
14
- :name => name,
15
- :connection_id => object_id,
16
- :statement_name => statement_name,
17
- :binds => binds,
18
- :turntable_shard_name => turntable_shard_name) { yield }
19
- rescue Exception => e
4
+ def translate_exception_class(e, sql)
5
+ begin
20
6
  message = "#{e.class.name}: #{e.message}: #{sql} : #{turntable_shard_name}"
21
- @logger.error message if @logger
22
- exception = translate_exception(e, message)
23
- exception.set_backtrace e.backtrace
24
- raise exception
7
+ rescue Encoding::CompatibilityError
8
+ message = "#{e.class.name}: #{e.message.force_encoding sql.encoding}: #{sql} : #{turntable_shard_name}"
25
9
  end
10
+
11
+ exception = translate_exception(e, message)
12
+ exception.set_backtrace e.backtrace
13
+ exception
26
14
  end
27
15
 
16
+ # @note override for append current shard name
17
+ def log(sql, name = "SQL", binds = [], statement_name = nil)
18
+ @instrumenter.instrument(
19
+ "sql.active_record",
20
+ :sql => sql,
21
+ :name => name,
22
+ :connection_id => object_id,
23
+ :statement_name => statement_name,
24
+ :binds => binds,
25
+ :turntable_shard_name => turntable_shard_name) { yield }
26
+ rescue => e
27
+ raise translate_exception_class(e, sql)
28
+ end
29
+
30
+ protected :translate_exception_class, :log
31
+
28
32
  def turntable_shard_name=(name)
29
33
  @turntable_shard_name = name.to_s
30
34
  end
@@ -2,20 +2,12 @@ module ActiveRecord::Turntable
2
2
  module ActiveRecordExt
3
3
  # activerecord-import extension
4
4
  module ActiverecordImportExt
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- alias_method_chain :values_sql_for_columns_and_attributes, :turntable
9
- end
10
-
11
- private
12
-
13
5
  # @note override for sequencer injection
14
6
  # @see https://github.com/zdennis/activerecord-import/blob/ba909fed5a4785fe9c7cce89e48e1242bb6804ea/lib/activerecord-import/import.rb#L558-L581
15
- def values_sql_for_columns_and_attributes_with_turntable(columns, array_of_attributes)
7
+ private def values_sql_for_columns_and_attributes(columns, array_of_attributes)
16
8
  connection_memo = connection
17
9
  array_of_attributes.map do |arr|
18
- my_values = arr.each_with_index.map do |val,j|
10
+ my_values = arr.each_with_index.map do |val, j|
19
11
  column = columns[j]
20
12
 
21
13
  # be sure to query sequence_name *last*, only if cheaper tests fail, because it's costly
@@ -28,10 +20,8 @@ module ActiveRecord::Turntable
28
20
  elsif column
29
21
  if respond_to?(:type_caster) && type_caster.respond_to?(:type_cast_for_database) # Rails 5.0 and higher
30
22
  connection_memo.quote(type_caster.type_cast_for_database(column.name, val))
31
- elsif column.respond_to?(:type_cast_from_user) # Rails 4.2 and higher
23
+ else # Rails 4.2 and higher
32
24
  connection_memo.quote(column.type_cast_from_user(val), column)
33
- else # Rails 3.1, 3.2, and 4.1
34
- connection_memo.quote(column.type_cast(val), column)
35
25
  end
36
26
  end
37
27
  end
@@ -41,9 +31,9 @@ module ActiveRecord::Turntable
41
31
  end
42
32
 
43
33
  begin
44
- require 'activerecord-import'
45
- require 'activerecord-import/base'
46
- (class << ActiveRecord::Base; self; end).send(:include, ActiverecordImportExt)
34
+ require "activerecord-import"
35
+ require "activerecord-import/base"
36
+ (class << ActiveRecord::Base; self; end).prepend(ActiverecordImportExt)
47
37
  rescue LoadError
48
38
  end
49
39
  end
@@ -1,22 +1,31 @@
1
- begin
2
- require 'acts_as_archive'
3
- # acts_as_archive extension
4
- class ActsAsArchive
5
- class << self
6
- # @note use the same shard which `from` shard using
7
- def move_with_turntable(config, where, merge_options={})
8
- if [config[:to], config[:from]].all? { |k| k.try(:turntable_enabled?) }
9
- current_shard = config[:from].connection.current_shard.name.to_sym
10
- config[:to].connection.with_shard(current_shard) {
11
- move_without_turntable(config, where, merge_options)
12
- }
13
- else
14
- move_without_turntable(config, where, merge_options)
1
+ module ActiveRecord::Turntable
2
+ module ActiveRecordExt
3
+ module ActsAsArchiveExt
4
+ def self.prepended(base)
5
+ class << base
6
+ prepend ClassMethods
15
7
  end
16
8
  end
17
9
 
18
- alias_method_chain :move, :turntable
10
+ module ClassMethods
11
+ # @note use the same shard which `from` shard using
12
+ def move(config, where, merge_options = {})
13
+ if [config[:to], config[:from]].all? { |k| k.try(:turntable_enabled?) }
14
+ current_shard = config[:from].connection.current_shard.name.to_sym
15
+ config[:to].connection.with_shard(current_shard) {
16
+ super
17
+ }
18
+ else
19
+ super
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ begin
26
+ require 'acts_as_archive'
27
+ ActsAsArchive.prepend ActsAsArchiveExt
28
+ rescue LoadError
19
29
  end
20
30
  end
21
- rescue LoadError
22
31
  end
@@ -3,29 +3,48 @@ require "active_record/associations"
3
3
  module ActiveRecord::Turntable
4
4
  module ActiveRecordExt
5
5
  module Association
6
+ extend ActiveSupport::Concern
6
7
  include ShardingCondition
7
8
 
8
- def self.prepended(mod)
9
- ActiveRecord::Associations::Builder::Association.valid_options << :foreign_shard_key
9
+ included do
10
+ ActiveRecord::Associations::SingularAssociation.prepend(SingularAssociationExt)
11
+ ActiveRecord::Associations::CollectionAssociation.prepend(CollectionAssociationExt)
12
+ ActiveRecord::Associations::Builder::Association::VALID_OPTIONS << :foreign_shard_key
10
13
  end
11
14
 
12
- protected
15
+ # @note Inject to add sharding condition for singular association
16
+ module SingularAssociationExt
17
+ private
18
+ def get_records
19
+ # OPTIMIZE: statement caching
20
+ if should_use_shard_key?
21
+ return turntable_scope(scope).limit(1).records
22
+ end
13
23
 
14
- # @note Override to pass shard key conditions
15
- def target_scope
16
- return super unless should_use_shard_key?
24
+ super
25
+ end
26
+ end
17
27
 
18
- scope = klass.where(
19
- klass.turntable_shard_key =>
20
- owner.send(foreign_shard_key)
21
- )
22
- super.merge!(scope)
23
- end
28
+ # @note Inject to add sharding condition for collection association
29
+ module CollectionAssociationExt
30
+ private
31
+ def get_records
32
+ # OPTIMIZE: statement caching
33
+ if should_use_shard_key?
34
+ return turntable_scope(scope).to_a
35
+ end
36
+
37
+ super
38
+ end
39
+ end
24
40
 
25
41
  private
26
42
 
27
- def skip_statement_cache?
28
- super || should_use_shard_key?
43
+ def turntable_scope(scope, bind = nil)
44
+ if should_use_shard_key?
45
+ scope = scope.where(klass.turntable_shard_key => owner.send(foreign_shard_key))
46
+ end
47
+ scope
29
48
  end
30
49
  end
31
50
  end
@@ -1,38 +1,18 @@
1
- require 'active_record/associations/preloader/association'
1
+ require "active_record/associations/preloader/association"
2
2
 
3
3
  module ActiveRecord::Turntable
4
4
  module ActiveRecordExt
5
5
  module AssociationPreloader
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- alias_method_chain :records_for, :turntable
10
- end
6
+ include ShardingCondition
11
7
 
12
8
  # @note Override to add sharding condition on preload
13
- def records_for_with_turntable(ids)
14
- returning_scope = records_for_without_turntable(ids)
9
+ def records_for(ids)
10
+ returning_scope = super
15
11
  if should_use_shard_key?
16
12
  returning_scope = returning_scope.where(klass.turntable_shard_key => owners.map(&foreign_shard_key.to_sym).uniq)
17
13
  end
18
14
  returning_scope
19
15
  end
20
-
21
- private
22
-
23
- def foreign_shard_key
24
- options[:foreign_shard_key] || model.turntable_shard_key
25
- end
26
-
27
- def should_use_shard_key?
28
- sharded_by_same_key? || !!options[:foreign_shard_key]
29
- end
30
-
31
- def sharded_by_same_key?
32
- model.turntable_enabled? &&
33
- klass.turntable_enabled? &&
34
- model.turntable_shard_key == klass.turntable_shard_key
35
- end
36
16
  end
37
17
  end
38
18
  end
@@ -5,7 +5,7 @@ module ActiveRecord::Turntable
5
5
 
6
6
  included do
7
7
  class << ActiveRecord::Base
8
- delegate :clever_load!, :to => :all
8
+ delegate :clever_load!, to: :all
9
9
  end
10
10
  end
11
11
 
@@ -13,7 +13,7 @@ module ActiveRecord::Turntable
13
13
  # load records
14
14
  records = self.to_a
15
15
  klass = records.first.class
16
- association_key = Util.ar42_or_later? ? association_name.to_s : association_name
16
+ association_key = association_name.to_s
17
17
  reflection = klass.reflections[association_key]
18
18
 
19
19
  if reflection
@@ -1,31 +1,27 @@
1
1
  module ActiveRecord::Turntable
2
2
  module ActiveRecordExt
3
3
  module ConnectionHandlerExtension
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- alias_method_chain :pool_for, :turntable
8
- end
9
-
10
- private
11
-
12
4
  # @note Override not to establish_connection destroy existing connection pool proxy object
13
- def pool_for_with_turntable(owner)
14
- owner_to_pool.fetch(owner.name) {
15
- if ancestor_pool = pool_from_any_process_for(owner)
5
+ def retrieve_connection_pool(spec_name)
6
+ owner_to_pool.fetch(spec_name) do
7
+ # Check if a connection was previously established in an ancestor process,
8
+ # which may have been forked.
9
+ if ancestor_pool = pool_from_any_process_for(spec_name)
16
10
  if ancestor_pool.is_a?(ActiveRecord::ConnectionAdapters::ConnectionPool)
17
11
  # A connection was established in an ancestor process that must have
18
12
  # subsequently forked. We can't reuse the connection, but we can copy
19
13
  # the specification and establish a new connection with it.
20
- establish_connection owner, ancestor_pool.spec
14
+ establish_connection(ancestor_pool.spec).tap do |pool|
15
+ pool.schema_cache = ancestor_pool.schema_cache if ancestor_pool.schema_cache
16
+ end
21
17
  else
22
18
  # Use same PoolProxy object
23
- owner_to_pool[owner.name] = ancestor_pool
19
+ owner_to_pool[spec_name] = ancestor_pool
24
20
  end
25
21
  else
26
- owner_to_pool[owner.name] = nil
22
+ owner_to_pool[spec_name] = nil
27
23
  end
28
- }
24
+ end
29
25
  end
30
26
  end
31
27
  end