adaptive_alias 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +70 -70
- data/.gitignore +62 -62
- data/.rubocop.yml +1227 -1227
- data/CHANGELOG.md +4 -1
- data/CODE_OF_CONDUCT.md +49 -49
- data/LICENSE +21 -21
- data/README.md +67 -46
- data/Rakefile +10 -10
- data/adaptive_alias.gemspec +44 -45
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/gemfiles/6.0.gemfile +10 -10
- data/gemfiles/6.1.gemfile +10 -10
- data/gemfiles/7.0.gemfile +11 -11
- data/lib/adaptive_alias/active_model_patches/read_attribute.rb +20 -20
- data/lib/adaptive_alias/active_model_patches/remove_alias_attribute.rb +27 -27
- data/lib/adaptive_alias/hooks/association.rb +13 -13
- data/lib/adaptive_alias/hooks/relation.rb +21 -21
- data/lib/adaptive_alias/hooks/singular_association.rb +13 -13
- data/lib/adaptive_alias/patches/{back_patch.rb → backward_patch.rb} +21 -21
- data/lib/adaptive_alias/patches/base.rb +75 -75
- data/lib/adaptive_alias/patches/forward_patch.rb +21 -21
- data/lib/adaptive_alias/version.rb +3 -3
- data/lib/adaptive_alias.rb +2 -2
- metadata +12 -26
@@ -1,21 +1,21 @@
|
|
1
|
-
module AdaptiveAlias
|
2
|
-
module Hooks
|
3
|
-
module Relation
|
4
|
-
def pluck(*)
|
5
|
-
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
6
|
-
end
|
7
|
-
|
8
|
-
def select_all(*)
|
9
|
-
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
10
|
-
end
|
11
|
-
|
12
|
-
def exec_queries(*)
|
13
|
-
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class ActiveRecord::Relation
|
20
|
-
prepend AdaptiveAlias::Hooks::Relation
|
21
|
-
end
|
1
|
+
module AdaptiveAlias
|
2
|
+
module Hooks
|
3
|
+
module Relation
|
4
|
+
def pluck(*)
|
5
|
+
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
6
|
+
end
|
7
|
+
|
8
|
+
def select_all(*)
|
9
|
+
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
10
|
+
end
|
11
|
+
|
12
|
+
def exec_queries(*)
|
13
|
+
AdaptiveAlias.rescue_statement_invalid(self){ super }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class ActiveRecord::Relation
|
20
|
+
prepend AdaptiveAlias::Hooks::Relation
|
21
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
module AdaptiveAlias
|
2
|
-
module Hooks
|
3
|
-
module SingularAssociation
|
4
|
-
def reader(*)
|
5
|
-
AdaptiveAlias.rescue_missing_attribute{ super }
|
6
|
-
end
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class ActiveRecord::Associations::SingularAssociation
|
12
|
-
prepend AdaptiveAlias::Hooks::SingularAssociation
|
13
|
-
end
|
1
|
+
module AdaptiveAlias
|
2
|
+
module Hooks
|
3
|
+
module SingularAssociation
|
4
|
+
def reader(*)
|
5
|
+
AdaptiveAlias.rescue_missing_attribute{ super }
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ActiveRecord::Associations::SingularAssociation
|
12
|
+
prepend AdaptiveAlias::Hooks::SingularAssociation
|
13
|
+
end
|
@@ -1,21 +1,21 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'adaptive_alias/patches/base'
|
4
|
-
|
5
|
-
module AdaptiveAlias
|
6
|
-
module Patches
|
7
|
-
class
|
8
|
-
def apply!
|
9
|
-
AdaptiveAlias.current_patches[[@klass, @old_column, @new_column]] = self
|
10
|
-
@klass.alias_attribute(@old_column, @new_column)
|
11
|
-
add_hooks!(current_column: @new_column, alias_column: @old_column, log_warning: true)
|
12
|
-
end
|
13
|
-
|
14
|
-
def remove!
|
15
|
-
super
|
16
|
-
@klass.remove_alias_attribute(@old_column)
|
17
|
-
ForwardPatch.new(@klass, @old_column, @new_column).apply!
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'adaptive_alias/patches/base'
|
4
|
+
|
5
|
+
module AdaptiveAlias
|
6
|
+
module Patches
|
7
|
+
class BackwardPatch < Base
|
8
|
+
def apply!
|
9
|
+
AdaptiveAlias.current_patches[[@klass, @old_column, @new_column]] = self
|
10
|
+
@klass.alias_attribute(@old_column, @new_column)
|
11
|
+
add_hooks!(current_column: @new_column, alias_column: @old_column, log_warning: true)
|
12
|
+
end
|
13
|
+
|
14
|
+
def remove!
|
15
|
+
super
|
16
|
+
@klass.remove_alias_attribute(@old_column)
|
17
|
+
ForwardPatch.new(@klass, @old_column, @new_column).apply!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,75 +1,75 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module AdaptiveAlias
|
4
|
-
module Patches
|
5
|
-
class Base
|
6
|
-
attr_reader :fix_association
|
7
|
-
attr_reader :fix_missing_attribute
|
8
|
-
attr_reader :removed
|
9
|
-
|
10
|
-
def initialize(klass, old_column, new_column)
|
11
|
-
@klass = klass
|
12
|
-
@old_column = old_column
|
13
|
-
@new_column = new_column
|
14
|
-
end
|
15
|
-
|
16
|
-
def add_hooks!(current_column:, alias_column:, log_warning: false)
|
17
|
-
patch = self
|
18
|
-
old_column = @old_column
|
19
|
-
new_column = @new_column
|
20
|
-
|
21
|
-
AdaptiveAlias.get_or_create_model_module(@klass).instance_exec do
|
22
|
-
remove_method(new_column) if method_defined?(new_column)
|
23
|
-
define_method(new_column) do
|
24
|
-
AdaptiveAlias.rescue_missing_attribute{ self[new_column] }
|
25
|
-
end
|
26
|
-
|
27
|
-
remove_method(old_column) if method_defined?(old_column)
|
28
|
-
define_method(old_column) do
|
29
|
-
patch.log_warning if log_warning
|
30
|
-
AdaptiveAlias.rescue_missing_attribute{ self[old_column] }
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
expected_error_message = "Mysql2::Error: Unknown column '#{@klass.table_name}.#{current_column}' in 'where clause'".freeze
|
35
|
-
|
36
|
-
@fix_missing_attribute = proc do
|
37
|
-
next false if patch.removed
|
38
|
-
|
39
|
-
patch.remove!
|
40
|
-
next true
|
41
|
-
end
|
42
|
-
|
43
|
-
@fix_association = proc do |target, error|
|
44
|
-
next false if patch.removed || error.message != expected_error_message
|
45
|
-
|
46
|
-
patch.remove!
|
47
|
-
|
48
|
-
if target
|
49
|
-
hash = target.where_values_hash
|
50
|
-
hash[alias_column] = hash.delete(current_column) if hash.key?(current_column)
|
51
|
-
target.instance_variable_set(:@arel, nil)
|
52
|
-
target.unscope!(:where).where!(hash)
|
53
|
-
end
|
54
|
-
|
55
|
-
next true
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def log_warning
|
60
|
-
if @prev_warning_time == nil || @prev_warning_time < AdaptiveAlias.log_interval.ago
|
61
|
-
@prev_warning_time = Time.now
|
62
|
-
AdaptiveAlias.unexpected_old_column_proc&.call
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def remove!
|
67
|
-
@removed = true
|
68
|
-
@klass.send(:reload_schema_from_cache)
|
69
|
-
@klass.initialize_find_by_cache
|
70
|
-
@fix_association = nil
|
71
|
-
@fix_missing_attribute = nil
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AdaptiveAlias
|
4
|
+
module Patches
|
5
|
+
class Base
|
6
|
+
attr_reader :fix_association
|
7
|
+
attr_reader :fix_missing_attribute
|
8
|
+
attr_reader :removed
|
9
|
+
|
10
|
+
def initialize(klass, old_column, new_column)
|
11
|
+
@klass = klass
|
12
|
+
@old_column = old_column
|
13
|
+
@new_column = new_column
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_hooks!(current_column:, alias_column:, log_warning: false)
|
17
|
+
patch = self
|
18
|
+
old_column = @old_column
|
19
|
+
new_column = @new_column
|
20
|
+
|
21
|
+
AdaptiveAlias.get_or_create_model_module(@klass).instance_exec do
|
22
|
+
remove_method(new_column) if method_defined?(new_column)
|
23
|
+
define_method(new_column) do
|
24
|
+
AdaptiveAlias.rescue_missing_attribute{ self[new_column] }
|
25
|
+
end
|
26
|
+
|
27
|
+
remove_method(old_column) if method_defined?(old_column)
|
28
|
+
define_method(old_column) do
|
29
|
+
patch.log_warning if log_warning
|
30
|
+
AdaptiveAlias.rescue_missing_attribute{ self[old_column] }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
expected_error_message = "Mysql2::Error: Unknown column '#{@klass.table_name}.#{current_column}' in 'where clause'".freeze
|
35
|
+
|
36
|
+
@fix_missing_attribute = proc do
|
37
|
+
next false if patch.removed
|
38
|
+
|
39
|
+
patch.remove!
|
40
|
+
next true
|
41
|
+
end
|
42
|
+
|
43
|
+
@fix_association = proc do |target, error|
|
44
|
+
next false if patch.removed || error.message != expected_error_message
|
45
|
+
|
46
|
+
patch.remove!
|
47
|
+
|
48
|
+
if target
|
49
|
+
hash = target.where_values_hash
|
50
|
+
hash[alias_column] = hash.delete(current_column) if hash.key?(current_column)
|
51
|
+
target.instance_variable_set(:@arel, nil)
|
52
|
+
target.unscope!(:where).where!(hash)
|
53
|
+
end
|
54
|
+
|
55
|
+
next true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def log_warning
|
60
|
+
if @prev_warning_time == nil || @prev_warning_time < AdaptiveAlias.log_interval.ago
|
61
|
+
@prev_warning_time = Time.now
|
62
|
+
AdaptiveAlias.unexpected_old_column_proc&.call
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def remove!
|
67
|
+
@removed = true
|
68
|
+
@klass.send(:reload_schema_from_cache)
|
69
|
+
@klass.initialize_find_by_cache
|
70
|
+
@fix_association = nil
|
71
|
+
@fix_missing_attribute = nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -1,21 +1,21 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'adaptive_alias/patches/base'
|
4
|
-
|
5
|
-
module AdaptiveAlias
|
6
|
-
module Patches
|
7
|
-
class ForwardPatch < Base
|
8
|
-
def apply!
|
9
|
-
AdaptiveAlias.current_patches[[@klass, @old_column, @new_column]] = self
|
10
|
-
@klass.alias_attribute(@new_column, @old_column)
|
11
|
-
add_hooks!(current_column: @old_column, alias_column: @new_column)
|
12
|
-
end
|
13
|
-
|
14
|
-
def remove!
|
15
|
-
super
|
16
|
-
@klass.remove_alias_attribute(@new_column)
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'adaptive_alias/patches/base'
|
4
|
+
|
5
|
+
module AdaptiveAlias
|
6
|
+
module Patches
|
7
|
+
class ForwardPatch < Base
|
8
|
+
def apply!
|
9
|
+
AdaptiveAlias.current_patches[[@klass, @old_column, @new_column]] = self
|
10
|
+
@klass.alias_attribute(@new_column, @old_column)
|
11
|
+
add_hooks!(current_column: @old_column, alias_column: @new_column)
|
12
|
+
end
|
13
|
+
|
14
|
+
def remove!
|
15
|
+
super
|
16
|
+
@klass.remove_alias_attribute(@new_column)
|
17
|
+
BackwardPatch.new(@klass, @old_column, @new_column).apply!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module AdaptiveAlias
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
1
|
+
module AdaptiveAlias
|
2
|
+
VERSION = '0.0.2'
|
3
|
+
end
|
data/lib/adaptive_alias.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'adaptive_alias/version'
|
4
4
|
require 'adaptive_alias/active_model_patches/read_attribute'
|
5
5
|
require 'adaptive_alias/active_model_patches/remove_alias_attribute'
|
6
|
-
require 'adaptive_alias/patches/
|
6
|
+
require 'adaptive_alias/patches/backward_patch'
|
7
7
|
require 'adaptive_alias/patches/forward_patch'
|
8
8
|
|
9
9
|
require 'adaptive_alias/hooks/association'
|
@@ -32,7 +32,7 @@ module AdaptiveAlias
|
|
32
32
|
|
33
33
|
included do
|
34
34
|
if column_names.include?(new_column)
|
35
|
-
Patches::
|
35
|
+
Patches::BackwardPatch.new(self, old_column, new_column).apply!
|
36
36
|
else
|
37
37
|
Patches::ForwardPatch.new(self, old_column, new_column).apply!
|
38
38
|
end
|
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: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07-
|
11
|
+
date: 2022-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,19 +73,19 @@ dependencies:
|
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '5.0'
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
76
|
+
name: rails_compatibility
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- - "
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
|
-
version:
|
81
|
+
version: 0.0.8
|
82
82
|
type: :development
|
83
83
|
prerelease: false
|
84
84
|
version_requirements: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
88
|
+
version: 0.0.8
|
89
89
|
- !ruby/object:Gem::Dependency
|
90
90
|
name: activerecord
|
91
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,20 +100,6 @@ dependencies:
|
|
100
100
|
- - ">="
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '3'
|
103
|
-
- !ruby/object:Gem::Dependency
|
104
|
-
name: rails_compatibility
|
105
|
-
requirement: !ruby/object:Gem::Requirement
|
106
|
-
requirements:
|
107
|
-
- - ">="
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: 0.0.8
|
110
|
-
type: :runtime
|
111
|
-
prerelease: false
|
112
|
-
version_requirements: !ruby/object:Gem::Requirement
|
113
|
-
requirements:
|
114
|
-
- - ">="
|
115
|
-
- !ruby/object:Gem::Version
|
116
|
-
version: 0.0.8
|
117
103
|
description: Extend attribute_alias and make it be adaptive with realtime database
|
118
104
|
schema
|
119
105
|
email:
|
@@ -143,7 +129,7 @@ files:
|
|
143
129
|
- lib/adaptive_alias/hooks/association_scope.rb
|
144
130
|
- lib/adaptive_alias/hooks/relation.rb
|
145
131
|
- lib/adaptive_alias/hooks/singular_association.rb
|
146
|
-
- lib/adaptive_alias/patches/
|
132
|
+
- lib/adaptive_alias/patches/backward_patch.rb
|
147
133
|
- lib/adaptive_alias/patches/base.rb
|
148
134
|
- lib/adaptive_alias/patches/forward_patch.rb
|
149
135
|
- lib/adaptive_alias/version.rb
|
@@ -156,7 +142,7 @@ metadata:
|
|
156
142
|
source_code_uri: https://github.com/khiav223577/adaptive_alias
|
157
143
|
documentation_uri: https://www.rubydoc.info/gems/adaptive_alias
|
158
144
|
bug_tracker_uri: https://github.com/khiav223577/adaptive_alias/issues
|
159
|
-
post_install_message:
|
145
|
+
post_install_message:
|
160
146
|
rdoc_options: []
|
161
147
|
require_paths:
|
162
148
|
- lib
|
@@ -171,8 +157,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
157
|
- !ruby/object:Gem::Version
|
172
158
|
version: '0'
|
173
159
|
requirements: []
|
174
|
-
rubygems_version: 3.
|
175
|
-
signing_key:
|
160
|
+
rubygems_version: 3.1.4
|
161
|
+
signing_key:
|
176
162
|
specification_version: 4
|
177
163
|
summary: Extend attribute_alias and make it be adaptive with realtime database schema
|
178
164
|
test_files: []
|