adaptive_alias 1.1.2 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a49038a810d4dcbbd42effa1664ff3495966c9a097df8939f92632e5fae54ac
4
- data.tar.gz: 3bee0518e9915af89ac616be205a73da6155e09d9f3d28947cd3cf9145e18660
3
+ metadata.gz: 6ac3bfeadde0c8fed89cc4c1803939cfcb5ebbd8f33bb9ecff1401fa0f33393c
4
+ data.tar.gz: e716802e6b36217f0bb94f056183887812f7c488a310d6b7b38e206a2d29e471
5
5
  SHA512:
6
- metadata.gz: '094892c502fe7343c74a5dae66ab57b1bd21405a73cf38c390c41b12bfd8d01f39c5d9cec78920ca1a388e93be68e280c95041a3fae4d43f9a7ea9ed931f6d22'
7
- data.tar.gz: 1c7e437b160e7ae6cbb8bb8624f654a220ed35fdbedae3a5a45a306f616bab4f7e62153c08cc874e59a806a7fd5bdc269a86c231180b597b333e9e2556e13acd
6
+ metadata.gz: 14726e4300d86f5f5093c6682d8f8be1f864f7fd29cd4f0978567025fb442f04c4276d3f1e61e8ca90f89429dd6c998fec194e92586f362f56da1c1241d166d2
7
+ data.tar.gz: c0b89da78c766cbaf1208b17f2858d35c1b04b972cf13822fd88b6fa9b3b72ad8b28f628cf4fbcfab28290e0fa95df02c03d71510e5acbf6ba77f89c77557208
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## Change Log
2
2
 
3
+ ### [v1.2.0](https://github.com/khiav223577/adaptive_alias/compare/v1.1.2...v1.2.0) 2022/10/12
4
+ - [#28](https://github.com/khiav223577/adaptive_alias/pull/28) [Fix] Deal with OR-query (@khiav223577)
5
+ - [#27](https://github.com/khiav223577/adaptive_alias/pull/27) [Enhance] make sure `mark_removable` will be called when manually remove patch (@khiav223577)
6
+ - [#26](https://github.com/khiav223577/adaptive_alias/pull/26) [Fix] Deal with old attribute sets come from cache (identity_cache) (@khiav223577)
7
+
8
+ ### [v1.1.2](https://github.com/khiav223577/adaptive_alias/compare/v1.1.1...v1.1.2) 2022/10/11
9
+ - [#25](https://github.com/khiav223577/adaptive_alias/pull/25) [Fix] Deal with count, sum, and group (@khiav223577)
10
+
3
11
  ### [v1.1.1](https://github.com/khiav223577/adaptive_alias/compare/v1.1.0...v1.1.1) 2022/10/07
4
12
  - [#24](https://github.com/khiav223577/adaptive_alias/pull/24) [Fix] Disappeared value when instantiating all and then create / update multiple models one by one (@khiav223577)
5
13
  - [#23](https://github.com/khiav223577/adaptive_alias/pull/23) [Enhance] No need to determine missing value manually (@khiav223577)
data/README.md CHANGED
@@ -25,10 +25,12 @@ We need a way to automatically adjust the patch to adapt current schema.
25
25
 
26
26
  ```
27
27
 
28
- This is what this gem wants to achieve. We don't want to do complex migrations, take care of backward-compatibiliy, and have any downtime.
28
+ This is what this gem wants to achieve. We don't want to do complex migrations, take care of backward-compatibility, and have any downtime.
29
29
  Just rely on this gem.
30
30
 
31
31
 
32
+ Note that it is not thread-safe when swapping patches, please make sure there is no multi-thread during migration.
33
+
32
34
  ## Supports
33
35
  - Ruby 2.6 ~ 2.7, 3.0 ~ 3.1
34
36
  - Rails 6.0, 6.1, 7.0
data/gemfiles/6.0.gemfile CHANGED
@@ -6,6 +6,7 @@ group :test do
6
6
  gem 'simplecov', '< 0.18'
7
7
  gem 'mysql2', '0.5.3'
8
8
  gem 'acts-as-taggable-on', '~> 7.0.0'
9
+ gem 'identity_cache', '~> 1.2.0'
9
10
  end
10
11
 
11
12
  gemspec path: '../'
data/gemfiles/6.1.gemfile CHANGED
@@ -6,6 +6,7 @@ group :test do
6
6
  gem 'simplecov', '< 0.18'
7
7
  gem 'mysql2', '0.5.3'
8
8
  gem 'acts-as-taggable-on', '~> 7.0.0'
9
+ gem 'identity_cache', '~> 1.2.0'
9
10
  end
10
11
 
11
12
  gemspec path: '../'
data/gemfiles/7.0.gemfile CHANGED
@@ -7,6 +7,7 @@ group :test do
7
7
  gem 'simplecov', '< 0.18'
8
8
  gem 'mysql2', '0.5.3'
9
9
  gem 'acts-as-taggable-on', '~> 9.0.0'
10
+ gem 'identity_cache', '~> 1.2.0'
10
11
  end
11
12
 
12
13
  gemspec path: '../'
@@ -18,6 +18,7 @@ module AdaptiveAlias
18
18
  name = self.class.attribute_aliases[name] || name
19
19
 
20
20
  sync_with_transaction_state if @transaction_state&.finalized?
21
+ attribute_set_fix!
21
22
  return @attributes.fetch_value(name, &block)
22
23
  end
23
24
  end
@@ -5,6 +5,14 @@ module AdaptiveAlias
5
5
  AdaptiveAlias.rescue_statement_invalid(relation: self){ super }
6
6
  end
7
7
 
8
+ def update_all(*)
9
+ AdaptiveAlias.rescue_statement_invalid(relation: self){ super }
10
+ end
11
+
12
+ def exists?(*)
13
+ AdaptiveAlias.rescue_statement_invalid(relation: self){ super }
14
+ end
15
+
8
16
  def select_all(*)
9
17
  AdaptiveAlias.rescue_statement_invalid(relation: self){ super }
10
18
  end
@@ -11,7 +11,7 @@ module AdaptiveAlias
11
11
  add_hooks!(current_column: @new_column, alias_column: @old_column, log_warning: true)
12
12
  end
13
13
 
14
- def remove!
14
+ def do_remove!
15
15
  super
16
16
  @klass.remove_alias_attribute(@old_column)
17
17
  @klass.define_attribute_method(@old_column)
@@ -3,8 +3,8 @@
3
3
  module AdaptiveAlias
4
4
  module Patches
5
5
  class Base
6
- attr_reader :fix_association
7
- attr_reader :fix_missing_attribute
6
+ attr_reader :check_matched
7
+ attr_reader :remove_and_fix_association
8
8
  attr_reader :removed
9
9
  attr_reader :removable
10
10
 
@@ -23,24 +23,24 @@ module AdaptiveAlias
23
23
  AdaptiveAlias.get_or_create_model_module(klass).instance_exec do
24
24
  remove_method(new_column) if method_defined?(new_column)
25
25
  define_method(new_column) do
26
- AdaptiveAlias.rescue_missing_attribute(klass){ self[new_column] }
26
+ self[new_column]
27
27
  end
28
28
 
29
29
  remove_method("#{new_column}=") if method_defined?("#{new_column}=")
30
30
  define_method("#{new_column}=") do |*args|
31
- AdaptiveAlias.rescue_missing_attribute(klass){ super(*args) }
31
+ super(*args)
32
32
  end
33
33
 
34
34
  remove_method(old_column) if method_defined?(old_column)
35
35
  define_method(old_column) do
36
36
  patch.log_warning if log_warning
37
- AdaptiveAlias.rescue_missing_attribute(klass){ self[old_column] }
37
+ self[old_column]
38
38
  end
39
39
 
40
40
  remove_method("#{old_column}=") if method_defined?("#{old_column}=")
41
41
  define_method("#{old_column}=") do |*args|
42
42
  patch.log_warning if log_warning
43
- AdaptiveAlias.rescue_missing_attribute(klass){ super(*args) }
43
+ super(*args)
44
44
  end
45
45
  end
46
46
 
@@ -54,24 +54,6 @@ module AdaptiveAlias
54
54
  "Mysql2::Error: Unknown column '#{current_column}' in 'field list'".freeze,
55
55
  ].freeze
56
56
 
57
- expected_attribute_err_msgs = [
58
- "can't write unknown attribute `#{current_column}`".freeze,
59
- "missing attribute: #{current_column}".freeze,
60
- ].freeze
61
-
62
- @fix_missing_attribute = proc do |error_klass, error|
63
- next false if not patch.removable
64
- next false if patch.removed
65
- next false if klass.table_name != error_klass.table_name
66
-
67
- # Error highlight behavior in Ruby 3.1 pollutes the error message
68
- error_msg = error.respond_to?(:original_message) ? error.original_message : error.message
69
- next false if not expected_attribute_err_msgs.include?(error_msg)
70
-
71
- patch.remove!
72
- next true
73
- end
74
-
75
57
  fix_arel_attributes = proc do |attr|
76
58
  next if not attr.is_a?(Arel::Attributes::Attribute)
77
59
  next if attr.name != current_column.to_s
@@ -87,9 +69,8 @@ module AdaptiveAlias
87
69
  end
88
70
  end
89
71
 
90
- @fix_association = proc do |relation, reflection, model, error|
72
+ @check_matched = proc do |relation, reflection, model, error|
91
73
  next false if not patch.removable
92
- next false if patch.removed
93
74
 
94
75
  # Error highlight behavior in Ruby 3.1 pollutes the error message
95
76
  error_msg = error.respond_to?(:original_message) ? error.original_message : error.message
@@ -103,25 +84,28 @@ module AdaptiveAlias
103
84
  end
104
85
 
105
86
  next false if not expected_association_err_msgs.include?(error_msg) and not ambiguous
87
+ next true
88
+ end
106
89
 
107
- patch.remove!
90
+ @remove_and_fix_association = proc do |relation, reflection, &block|
91
+ patch.remove! do
92
+ if relation
93
+ relation.reset # reset @arel
108
94
 
109
- if relation
110
- relation.reset # reset @arel
95
+ joins = relation.arel.source.right # @ctx.source.right << create_join(relation, nil, klass)
111
96
 
112
- joins = relation.arel.source.right # @ctx.source.right << create_join(relation, nil, klass)
97
+ # adjust select fields
98
+ index = relation.select_values.index(current_column)
99
+ relation.select_values[index] = alias_column if index
113
100
 
114
- # adjust select fields
115
- index = relation.select_values.index(current_column)
116
- relation.select_values[index] = alias_column if index
101
+ fix_arel_nodes.call(joins.map{|s| s.right.expr })
102
+ fix_arel_nodes.call(relation.where_clause.send(:predicates))
103
+ end
117
104
 
118
- fix_arel_nodes.call(joins.map{|s| s.right.expr })
119
- fix_arel_nodes.call(relation.where_clause.send(:predicates))
120
- end
105
+ reflection.clear_association_scope_cache if reflection
121
106
 
122
- reflection.clear_association_scope_cache if reflection
123
-
124
- next true
107
+ block.call
108
+ end
125
109
  end
126
110
  end
127
111
 
@@ -133,15 +117,24 @@ module AdaptiveAlias
133
117
  end
134
118
 
135
119
  def remove!
136
- @removed = true
120
+ if not @removed
121
+ @removed = true
122
+ new_patch = do_remove!
123
+ end
124
+
125
+ yield if block_given?
126
+ ensure
127
+ new_patch.mark_removable if new_patch
128
+ end
137
129
 
130
+ def do_remove!
138
131
  reset_caches(@klass)
139
132
  ActiveRecord::Base.descendants.each do |model_klass|
140
133
  reset_caches(model_klass) if model_klass.table_name == @klass.table_name
141
134
  end
142
135
 
143
- @fix_association = nil
144
- @fix_missing_attribute = nil
136
+ @check_matched = nil
137
+ @remove_and_fix_association = nil
145
138
  end
146
139
 
147
140
  def mark_removable
@@ -162,10 +155,14 @@ module AdaptiveAlias
162
155
  def each_nodes(nodes, &block)
163
156
  nodes.each do |node|
164
157
  case node
158
+ when Arel::Nodes::Grouping
159
+ each_nodes([node.expr], &block)
165
160
  when Arel::Nodes::Equality
166
161
  yield(node)
167
162
  when Arel::Nodes::And
168
163
  each_nodes(node.children, &block)
164
+ when Arel::Nodes::Or
165
+ each_nodes([node.left, node.right], &block)
169
166
  end
170
167
  end
171
168
  end
@@ -11,7 +11,7 @@ module AdaptiveAlias
11
11
  add_hooks!(current_column: @old_column, alias_column: @new_column)
12
12
  end
13
13
 
14
- def remove!
14
+ def do_remove!
15
15
  super
16
16
  @klass.remove_alias_attribute(@new_column)
17
17
  @klass.define_attribute_method(@new_column)
@@ -1,3 +1,3 @@
1
1
  module AdaptiveAlias
2
- VERSION = '1.1.2'
2
+ VERSION = '1.2.1'
3
3
  end
@@ -10,8 +10,6 @@ require 'adaptive_alias/patches/backward_patch'
10
10
  require 'adaptive_alias/patches/forward_patch'
11
11
 
12
12
  require 'adaptive_alias/hooks/association'
13
- require 'adaptive_alias/hooks/association_scope'
14
- require 'adaptive_alias/hooks/singular_association'
15
13
  require 'adaptive_alias/hooks/relation'
16
14
  require 'adaptive_alias/hooks/active_record_core'
17
15
  require 'adaptive_alias/hooks/active_record_persistence'
@@ -47,21 +45,12 @@ module AdaptiveAlias
47
45
  def rescue_statement_invalid(relation: nil, reflection: nil, model: nil, &block)
48
46
  yield
49
47
  rescue ActiveRecord::StatementInvalid => error
50
- raise error if AdaptiveAlias.current_patches.all?{|_key, patch| !patch.fix_association.call(relation, reflection, model, error) }
48
+ _key, patch = AdaptiveAlias.current_patches.find{|_key, patch| patch.check_matched.call(relation, reflection, model, error) }
49
+ raise error if patch == nil
51
50
 
52
- result = rescue_statement_invalid(relation: relation, reflection: reflection, model: model, &block)
53
- AdaptiveAlias.current_patches.each_value(&:mark_removable)
54
- return result
55
- end
56
-
57
- def rescue_missing_attribute(klass, &block)
58
- yield
59
- rescue ActiveModel::MissingAttributeError => error
60
- raise error if AdaptiveAlias.current_patches.all?{|_key, patch| !patch.fix_missing_attribute.call(klass, error) }
61
-
62
- result = rescue_missing_attribute(klass, &block)
63
- AdaptiveAlias.current_patches.each_value(&:mark_removable)
64
- return result
51
+ patch.remove_and_fix_association.call(relation, reflection) do
52
+ return rescue_statement_invalid(relation: relation, reflection: reflection, model: model, &block)
53
+ end
65
54
  end
66
55
 
67
56
  def get_or_create_model_module(klass)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adaptive_alias
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-11 00:00:00.000000000 Z
11
+ date: 2022-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -131,10 +131,8 @@ files:
131
131
  - lib/adaptive_alias/hooks/active_record_core.rb
132
132
  - lib/adaptive_alias/hooks/active_record_persistence.rb
133
133
  - lib/adaptive_alias/hooks/association.rb
134
- - lib/adaptive_alias/hooks/association_scope.rb
135
134
  - lib/adaptive_alias/hooks/calculations.rb
136
135
  - lib/adaptive_alias/hooks/relation.rb
137
- - lib/adaptive_alias/hooks/singular_association.rb
138
136
  - lib/adaptive_alias/patches/backward_patch.rb
139
137
  - lib/adaptive_alias/patches/base.rb
140
138
  - lib/adaptive_alias/patches/forward_patch.rb
@@ -1,13 +0,0 @@
1
- module AdaptiveAlias
2
- module Hooks
3
- module AssociationScope
4
- def last_chain_scope(_scope, reflection, owner)
5
- AdaptiveAlias.rescue_missing_attribute(owner.class){ super }
6
- end
7
- end
8
- end
9
- end
10
-
11
- class ActiveRecord::Associations::AssociationScope
12
- prepend AdaptiveAlias::Hooks::AssociationScope
13
- end
@@ -1,13 +0,0 @@
1
- module AdaptiveAlias
2
- module Hooks
3
- module SingularAssociation
4
- def reader(*)
5
- AdaptiveAlias.rescue_missing_attribute(owner.class){ super }
6
- end
7
- end
8
- end
9
- end
10
-
11
- class ActiveRecord::Associations::SingularAssociation
12
- prepend AdaptiveAlias::Hooks::SingularAssociation
13
- end