activerecord-turntable 3.0.0 → 3.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cfc2457a7b8212bbeef4d49d4b84b8b40877396
4
- data.tar.gz: ed7b1f0e36e61aa3104ec0bebd1cd4d67e31dcdb
3
+ metadata.gz: 65f26f1f0d729de81434f01e9bcaaff91ed996e7
4
+ data.tar.gz: 789697728baf93d532fdd47b711473debe693437
5
5
  SHA512:
6
- metadata.gz: ba0296ccb842d6f10069cc875a2aacd0bd2bd7b3b3766cd7fc3701b795c16c3f05f64531b279a1cac2a13649dac832ce932c7ba3478326aaf03f1698f5757e46
7
- data.tar.gz: 497202d000a232456c5c786ec1202920773069a684760d2c3bcb9eb3e6e55c761d19ff1fb64ac6f3f333401ebc31a1b527b7cb299d603ab22b52413f9376e9ba
6
+ metadata.gz: 4ba8a9f58defbc75afc069b299d5bd27e3a7688bf176f86b7f5670a56bd129bae4d7e0988ef04d78edea863105be3445e5b07f68f306ee24e427b90c03dc842d
7
+ data.tar.gz: '08c739c86e694240129f47c0f7dcd039107f2ca8630356796695c4e7faa13a8784da4d61b54be5804def5f6b7e041f55c02218ee4e64bca4260d8a3ac4338e08'
data/.travis.yml CHANGED
@@ -5,13 +5,16 @@ cache:
5
5
  bundler: true
6
6
 
7
7
  rvm:
8
- - 2.2.6
9
- - 2.3.3
10
- - 2.4.0
8
+ - 2.2.7
9
+ - 2.3.4
10
+ - 2.4.1
11
11
  - ruby-head
12
12
 
13
13
  gemfile:
14
- - gemfiles/rails5_0.gemfile
14
+ - gemfiles/rails5_0_0.gemfile
15
+ - gemfiles/rails5_0_1.gemfile
16
+ - gemfiles/rails5_0_2.gemfile
17
+ - gemfiles/rails5_0_3.gemfile
15
18
  - gemfiles/rails_edge.gemfile
16
19
 
17
20
  env:
@@ -27,6 +30,17 @@ script:
27
30
  - bundle exec rake $BUILD_TASK
28
31
 
29
32
  matrix:
33
+ exclude:
34
+ - rvm: ruby-head
35
+ - gemfile: gemfiles/rails_edge.gemfile
36
+ - rvm: 2.3.4
37
+ include:
38
+ - rvm: ruby-head
39
+ gemfile: gemfiles/rails_edge.gemfile
40
+ - rvm: 2.4.1
41
+ gemfile: gemfiles/rails_edge.gemfile
42
+ - rvm: 2.3.4
43
+ gemfile: gemfiles/rails5_0_3.gemfile
30
44
  allow_failures:
31
45
  - rvm: ruby-head
32
46
  - gemfile: gemfiles/rails_edge.gemfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## activerecord-turntable 3.0.1 ##
2
+
3
+ ### Minor Changes
4
+
5
+ * Restore LogSubscriber log format to the same as 2.x
6
+
7
+ ### Bugfixes
8
+
9
+ * Fix ActiveRecord 5.0.x compatibilities
10
+ * Fixes SchemaDumper fails dumping sequence tables on v5.0.0
11
+ * Follow AbstractAdapter#log implementation changes on v5.0.3
12
+ * Fix QueryCache to work with v5.0.1 or later
13
+
1
14
  ## activerecord-turntable 3.0.0 ##
2
15
 
3
16
  ### Bugfixes
data/Rakefile CHANGED
@@ -148,6 +148,26 @@ namespace :turntable do
148
148
  File.open("test/cases/helper.rb", "a") do |f|
149
149
  f << "require '#{File.expand_path("spec/activerecord_helper", __dir__)}'"
150
150
  end
151
+
152
+ # FIXME: Disable a part of tests about internal metadata and validations on 5.0.x because it randomly fails.
153
+ if ActiveRecord.gem_version.release < Gem::Version.new("5.1.0")
154
+ File.open("test/cases/migration_test.rb", "a") do |f|
155
+ f << <<-EOS
156
+ class MigrationTest
157
+ undef :test_migration_sets_internal_metadata_even_when_fully_migrated,
158
+ :test_internal_metadata_stores_environment
159
+ end
160
+ EOS
161
+ end
162
+
163
+ File.open("test/cases/validations_test.rb", "a") do |f|
164
+ f << <<-EOS
165
+ class ValidationsTest
166
+ undef :test_numericality_validation_with_mutation
167
+ end
168
+ EOS
169
+ end
170
+ end
151
171
  end
152
172
 
153
173
  task :db => :rails do
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "5.0.0"
4
+ gem "activesupport", "5.0.0"
5
+
6
+ gemspec :path => '../'
File without changes
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "5.0.2"
4
+ gem "activesupport", "5.0.2"
5
+
6
+ gemspec :path => '../'
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "activerecord", "5.0.3"
4
+ gem "activesupport", "5.0.3"
5
+
6
+ gemspec :path => '../'
@@ -28,7 +28,6 @@ module ActiveRecord::Turntable
28
28
  autoload :Migration
29
29
  autoload :Mixer
30
30
  autoload :PoolProxy
31
- autoload :QueryCache
32
31
  autoload :Shard
33
32
  autoload :ShardingCondition
34
33
  autoload :SeqShard
@@ -16,6 +16,7 @@ module ActiveRecord::Turntable
16
16
  autoload :AssociationPreloader
17
17
  autoload :Association
18
18
  autoload :LockingOptimistic
19
+ autoload :QueryCache
19
20
  end
20
21
 
21
22
  included do
@@ -31,6 +32,7 @@ module ActiveRecord::Turntable
31
32
  ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(ConnectionHandlerExtension)
32
33
  ActiveRecord::Associations::Preloader::Association.prepend(AssociationPreloader)
33
34
  ActiveRecord::Associations::Association.prepend(Association)
35
+ ActiveRecord::QueryCache.prepend(QueryCache)
34
36
  require "active_record/turntable/active_record_ext/fixtures"
35
37
  require "active_record/turntable/active_record_ext/migration_proxy"
36
38
  require "active_record/turntable/active_record_ext/activerecord_import_ext"
@@ -15,17 +15,33 @@ module ActiveRecord::Turntable
15
15
 
16
16
  # @note override for append current shard name
17
17
  # rubocop:disable Style/HashSyntax, Style/MultilineMethodCallBraceLayout
18
- def log(sql, name = "SQL", binds = [], statement_name = nil)
19
- @instrumenter.instrument(
20
- "sql.active_record",
21
- :sql => sql,
22
- :name => name,
23
- :connection_id => object_id,
24
- :statement_name => statement_name,
25
- :binds => binds,
26
- :turntable_shard_name => turntable_shard_name) { yield }
27
- rescue => e
28
- raise translate_exception_class(e, sql)
18
+ if ActiveRecord::Turntable::Util.ar_version_equals_or_later?("5.0.3")
19
+ def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil)
20
+ @instrumenter.instrument(
21
+ "sql.active_record",
22
+ sql: sql,
23
+ name: name,
24
+ binds: binds,
25
+ type_casted_binds: type_casted_binds,
26
+ statement_name: statement_name,
27
+ connection_id: object_id,
28
+ turntable_shard_name: turntable_shard_name) { yield }
29
+ rescue => e
30
+ raise translate_exception_class(e, sql)
31
+ end
32
+ else
33
+ def log(sql, name = "SQL", binds = [], statement_name = nil)
34
+ @instrumenter.instrument(
35
+ "sql.active_record",
36
+ :sql => sql,
37
+ :name => name,
38
+ :connection_id => object_id,
39
+ :statement_name => statement_name,
40
+ :binds => binds,
41
+ :turntable_shard_name => turntable_shard_name) { yield }
42
+ rescue => e
43
+ raise translate_exception_class(e, sql)
44
+ end
29
45
  end
30
46
  # rubocop:enable Style/HashSyntax, Style/MultilineMethodCallBraceLayout
31
47
 
@@ -5,17 +5,34 @@ module ActiveRecord::Turntable
5
5
  module LogSubscriber
6
6
  # @note prepend to add shard name logging
7
7
  def sql(event)
8
+ self.class.runtime += event.duration
9
+ return unless logger.debug?
10
+
8
11
  payload = event.payload
9
12
 
10
- if self.class::IGNORE_PAYLOAD_NAMES.include?(payload[:name])
11
- self.class.runtime += event.duration
12
- return
13
- end
13
+ return if ActiveRecord::LogSubscriber::IGNORE_PAYLOAD_NAMES.include?(payload[:name])
14
+
15
+ name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
16
+ name = "#{name} [Shard: #{payload[:turntable_shard_name]}]" if payload[:turntable_shard_name]
17
+ name = "CACHE #{name}" if payload[:cached]
18
+ sql = payload[:sql]
19
+ binds = nil
14
20
 
15
- if payload[:turntable_shard_name]
16
- payload[:name] = "#{payload[:name]} [Shard: #{payload[:turntable_shard_name]}]"
21
+ unless (payload[:binds] || []).empty?
22
+ if Util.ar_version_equals_or_later?("5.0.3")
23
+ casted_params = type_casted_binds(payload[:binds], payload[:type_casted_binds])
24
+ binds = " " + payload[:binds].zip(casted_params).map { |attr, value|
25
+ render_bind(attr, value)
26
+ }.inspect
27
+ else
28
+ binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
29
+ end
17
30
  end
18
- super
31
+
32
+ name = colorize_payload_name(name, payload[:name])
33
+ sql = color(sql, sql_color(sql), true)
34
+
35
+ debug " #{name} #{sql}#{binds}"
19
36
  end
20
37
  end
21
38
  end
@@ -0,0 +1,76 @@
1
+ module ActiveRecord::Turntable
2
+ module ActiveRecordExt
3
+ module QueryCache
4
+ def self.prepended(klass)
5
+ class << klass
6
+ if Util.ar_version_equals_or_later?("5.0.1")
7
+ prepend(ClassMethods::AR5_0_1)
8
+ else
9
+ prepend(ClassMethods::AR5_0)
10
+ end
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ module AR5_0
16
+ def run
17
+ result = super
18
+
19
+ pools = ActiveRecord::Base.turntable_connections.values
20
+ pools.each do |k|
21
+ k.connection.enable_query_cache!
22
+ end
23
+
24
+ result
25
+ end
26
+
27
+ def complete(state)
28
+ enabled, _connection_id = state
29
+ super
30
+
31
+ klasses = ActiveRecord::Base.turntable_connections.values
32
+ klasses.each do |k|
33
+ k.connection.clear_query_cache
34
+ k.connection.disable_query_cache! unless enabled
35
+ end
36
+ end
37
+ end
38
+
39
+ module AR5_0_1
40
+ def run
41
+ result = super
42
+
43
+ pools = ActiveRecord::Base.turntable_connections.values
44
+ pools.each do |pool|
45
+ pool.enable_query_cache!
46
+ end
47
+
48
+ [*result, pools]
49
+ end
50
+
51
+ def complete(state)
52
+ caching_pool, caching_was_enabled, connection_id, turntable_pools = state
53
+ super([caching_pool, caching_was_enabled, connection_id])
54
+
55
+ turntable_pools.each do |pool|
56
+ pool.disable_query_cache! unless caching_was_enabled
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def self.install_turntable_executor_hooks(executor = ActiveSupport::Executor)
63
+ return if Util.ar_version_equals_or_later?("5.0.1")
64
+
65
+ executor.to_complete do
66
+ klasses = ActiveRecord::Base.turntable_connection_classes
67
+ klasses.each do |k|
68
+ unless k.connected? && k.connection.transaction_open?
69
+ k.clear_active_connections!
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -20,7 +20,12 @@ module ActiveRecord::Turntable
20
20
 
21
21
  table_options = @connection.table_options(table)
22
22
  if table_options.present?
23
- tbl.print ", #{format_options(table_options)}"
23
+ options = respond_to?(:format_options) ? format_options(table_options) : table_options.inspect
24
+ tbl.print ", options: #{options}"
25
+ end
26
+
27
+ if comment = @connection.table_comment(table).presence
28
+ tbl.print ", comment: #{comment.inspect}"
24
29
  end
25
30
  tbl.puts
26
31
 
@@ -1,4 +1,5 @@
1
1
  require "active_record/turntable/connection_proxy/mixable"
2
+
2
3
  module ActiveRecord::Turntable
3
4
  class ConnectionProxy
4
5
  include Mixable
@@ -24,35 +25,61 @@ module ActiveRecord::Turntable
24
25
  delegate :create_table, :rename_table, :drop_table, :add_column, :remove_colomn,
25
26
  :change_column, :change_column_default, :rename_column, :add_index,
26
27
  :remove_index, :initialize_schema_information,
27
- :dump_schema_information, :execute_ignore_duplicate, to: :master_connection
28
+ :dump_schema_information, :execute_ignore_duplicate,
29
+ :query_cache_enabled, to: :master_connection
28
30
 
29
31
  def transaction(options = {}, &block)
30
32
  connection.transaction(options, &block)
31
33
  end
32
34
 
33
35
  def cache
36
+ old = query_cache_enabled
34
37
  enable_query_cache!
35
38
  yield
36
39
  ensure
37
- clear_query_cache
40
+ unless old
41
+ disable_query_cache!
42
+ clear_query_cache
43
+ end
44
+ end
45
+
46
+ def uncached
47
+ old = query_cache_enabled
48
+ disable_query_cache!
49
+ yield
50
+ ensure
51
+ enable_query_cache! if old
38
52
  end
39
53
 
40
54
  def enable_query_cache!
55
+ master_connection.enable_query_cache!
56
+
41
57
  klass.turntable_connections.each do |_k, v|
42
58
  v.connection.enable_query_cache!
43
59
  end
44
60
  end
45
61
 
62
+ def disable_query_cache!
63
+ master_connection.disable_query_cache!
64
+
65
+ klass.turntable_connections.each do |_k, v|
66
+ v.connection.disable_query_cache!
67
+ end
68
+ end
69
+
46
70
  def clear_query_cache_if_needed(method)
47
71
  clear_query_cache if CLEAR_CACHE_METHODS.include?(method)
48
72
  end
49
73
 
50
74
  def clear_query_cache
75
+ master_connection.clear_query_cache
76
+
51
77
  klass.turntable_connections.each do |_k, v|
52
78
  v.connection.clear_query_cache
53
79
  end
54
80
  end
55
81
 
82
+
56
83
  # rubocop:disable Style/MethodMissing
57
84
  def method_missing(method, *args, &block)
58
85
  clear_query_cache_if_needed(method)
@@ -104,30 +104,33 @@ module ActiveRecord::Turntable::Migration
104
104
 
105
105
  module ClassMethods
106
106
  def up(migrations_paths, target_version = nil)
107
- super
107
+ result = super
108
108
 
109
109
  ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected(current_environment) do |name, configuration|
110
110
  puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
111
111
  super(migrations_paths, target_version)
112
112
  end
113
+ result
113
114
  end
114
115
 
115
116
  def down(migrations_paths, target_version = nil, &block)
116
- super
117
+ result = super
117
118
 
118
119
  ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected(current_environment) do |name, configuration|
119
120
  puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
120
121
  super(migrations_paths, target_version, &block)
121
122
  end
123
+ result
122
124
  end
123
125
 
124
126
  def run(*args)
125
- super
127
+ result = super
126
128
 
127
129
  ActiveRecord::Tasks::DatabaseTasks.each_current_turntable_cluster_connected(current_environment) do |name, configuration|
128
130
  puts "[turntable] *** Migrating database: #{configuration['database']}(Shard: #{name})"
129
131
  super(*args)
130
132
  end
133
+ result
131
134
  end
132
135
  end
133
136
  end
@@ -24,9 +24,9 @@ module ActiveRecord::Turntable
24
24
  end
25
25
 
26
26
  # set QueryCache executor hooks for turntable clusters
27
- initializer "active_record.set_executor_hooks" do
27
+ initializer "turntable.set_executor_hooks" do
28
28
  ActiveSupport.on_load(:active_record) do
29
- ActiveRecord::Turntable::QueryCache.install_executor_hooks
29
+ ActiveRecord::Turntable::ActiveRecordExt::QueryCache.install_turntable_executor_hooks
30
30
  end
31
31
  end
32
32
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Turntable
3
- VERSION = "3.0.0".freeze
3
+ VERSION = "3.0.1".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-turntable
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gussan
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-05-25 00:00:00.000000000 Z
12
+ date: 2017-06-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -509,7 +509,10 @@ files:
509
509
  - README.md
510
510
  - Rakefile
511
511
  - activerecord-turntable.gemspec
512
- - gemfiles/rails5_0.gemfile
512
+ - gemfiles/rails5_0_0.gemfile
513
+ - gemfiles/rails5_0_1.gemfile
514
+ - gemfiles/rails5_0_2.gemfile
515
+ - gemfiles/rails5_0_3.gemfile
513
516
  - gemfiles/rails_edge.gemfile
514
517
  - lib/active_record/turntable.rb
515
518
  - lib/active_record/turntable/active_record_ext.rb
@@ -527,6 +530,7 @@ files:
527
530
  - lib/active_record/turntable/active_record_ext/log_subscriber.rb
528
531
  - lib/active_record/turntable/active_record_ext/migration_proxy.rb
529
532
  - lib/active_record/turntable/active_record_ext/persistence.rb
533
+ - lib/active_record/turntable/active_record_ext/query_cache.rb
530
534
  - lib/active_record/turntable/active_record_ext/relation.rb
531
535
  - lib/active_record/turntable/active_record_ext/schema_dumper.rb
532
536
  - lib/active_record/turntable/active_record_ext/sequencer.rb
@@ -555,7 +559,6 @@ files:
555
559
  - lib/active_record/turntable/mixer/fader/update_shards_merge_result.rb
556
560
  - lib/active_record/turntable/plugin.rb
557
561
  - lib/active_record/turntable/pool_proxy.rb
558
- - lib/active_record/turntable/query_cache.rb
559
562
  - lib/active_record/turntable/railtie.rb
560
563
  - lib/active_record/turntable/railties/databases.rake
561
564
  - lib/active_record/turntable/seq_shard.rb
@@ -592,7 +595,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
592
595
  version: '0'
593
596
  requirements: []
594
597
  rubyforge_project: activerecord-turntable
595
- rubygems_version: 2.5.2
598
+ rubygems_version: 2.6.11
596
599
  signing_key:
597
600
  specification_version: 4
598
601
  summary: ActiveRecord sharding extension
@@ -1,41 +0,0 @@
1
- require "rack/body_proxy"
2
- require "active_record/query_cache"
3
-
4
- module ActiveRecord
5
- module Turntable
6
- class QueryCache < ActiveRecord::QueryCache
7
- def self.run
8
- klasses = ActiveRecord::Base.turntable_connections.values
9
- enables = klasses.map do |k|
10
- enabled = k.connection.query_cache_enabled
11
- k.connection.enable_query_cache!
12
-
13
- enabled
14
- end
15
-
16
- enables.all?
17
- end
18
-
19
- def self.complete(enabled)
20
- klasses = ActiveRecord::Base.turntable_connections.values
21
- klasses.each do |k|
22
- k.connection.clear_query_cache
23
- k.connection.disable_query_cache! unless enabled
24
- end
25
- end
26
-
27
- def self.install_executor_hooks(executor = ActiveSupport::Executor)
28
- executor.register_hook(self)
29
-
30
- executor.to_complete do
31
- klasses = ActiveRecord::Base.turntable_connection_classes
32
- klasses.each do |k|
33
- unless k.connected? && k.connection.transaction_open?
34
- k.clear_active_connections!
35
- end
36
- end
37
- end
38
- end
39
- end
40
- end
41
- end