elasticsearch_record 1.2.3 → 1.3.0
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 +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +22 -6
- data/docs/CHANGELOG.md +25 -0
- data/lib/active_record/connection_adapters/elasticsearch/schema_definitions/clone_table_definition.rb +9 -0
- data/lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb +110 -9
- data/lib/active_record/connection_adapters/elasticsearch/schema_definitions/table_definition.rb +0 -34
- data/lib/active_record/connection_adapters/elasticsearch/schema_definitions/table_mapping_definition.rb +6 -0
- data/lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb +34 -29
- data/lib/active_record/connection_adapters/elasticsearch/schema_definitions.rb +2 -2
- data/lib/active_record/connection_adapters/elasticsearch/schema_dumper.rb +14 -2
- data/lib/active_record/connection_adapters/elasticsearch/schema_statements.rb +7 -1
- data/lib/active_record/connection_adapters/elasticsearch/table_statements.rb +74 -34
- data/lib/arel/collectors/elasticsearch_query.rb +10 -3
- data/lib/arel/visitors/elasticsearch_query.rb +35 -9
- data/lib/arel/visitors/elasticsearch_schema.rb +5 -4
- data/lib/elasticsearch_record/gem_version.rb +2 -2
- data/lib/elasticsearch_record/relation/calculation_methods.rb +3 -3
- data/lib/elasticsearch_record/relation/query_methods.rb +78 -49
- data/lib/elasticsearch_record/relation/result_methods.rb +1 -1
- data/lib/elasticsearch_record/result.rb +28 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e7302cf1310d0d583125bf9a9e526203feb42d000569b6daca266d88ae7bb50
|
4
|
+
data.tar.gz: e67b0946f566b44232827244c323aa2e221796f4d0ac91adf4696179f4ba56e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fbf9f25937e5c77520af89de811c72bc9e2a51a49cbb1c6f7a77d0b2af8d2e533b3cc2fc11acf3f8b9a78ed5616c97c068cc88a1171e85bcc5611252960b345
|
7
|
+
data.tar.gz: f8d77b26a20ddd8db5495638fdd98da72bcd1bd6978f17d6a1a794ab436fc184e3dafa153c67b364f6c89e59c5bca1eb0a59e09b240c4ea8e6055c017eb4738c
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
elasticsearch_record (1.2.
|
4
|
+
elasticsearch_record (1.2.4)
|
5
5
|
activerecord (~> 7.0.0)
|
6
6
|
elasticsearch (~> 8.4)
|
7
7
|
|
@@ -28,7 +28,7 @@ GEM
|
|
28
28
|
elasticsearch-api (= 8.5.2)
|
29
29
|
elasticsearch-api (8.5.2)
|
30
30
|
multi_json
|
31
|
-
faraday (2.7.
|
31
|
+
faraday (2.7.2)
|
32
32
|
faraday-net_http (>= 2.0, < 3.1)
|
33
33
|
ruby2_keywords (>= 0.0.4)
|
34
34
|
faraday-net_http (3.0.2)
|
data/README.md
CHANGED
@@ -198,6 +198,7 @@ total = scope.total
|
|
198
198
|
- kind
|
199
199
|
- configure
|
200
200
|
- aggregate
|
201
|
+
- refresh
|
201
202
|
- query
|
202
203
|
- filter
|
203
204
|
- must_not
|
@@ -274,6 +275,8 @@ Access these methods through the model's connection.
|
|
274
275
|
- meta_exists?
|
275
276
|
- max_result_window
|
276
277
|
- cluster_info
|
278
|
+
- cluster_settings
|
279
|
+
- cluster_health
|
277
280
|
|
278
281
|
## Active Record Schema migration methods
|
279
282
|
Access these methods through the model's connection or within any `Migration`.
|
@@ -285,26 +288,30 @@ Access these methods through the model's connection or within any `Migration`.
|
|
285
288
|
- close_tables
|
286
289
|
- truncate_table
|
287
290
|
- truncate_tables
|
291
|
+
- refresh_table
|
292
|
+
- refresh_tables
|
288
293
|
- drop_table
|
289
294
|
- block_table
|
290
295
|
- unblock_table
|
291
296
|
- clone_table
|
292
297
|
- create_table
|
293
298
|
- change_table
|
299
|
+
- rename_table
|
294
300
|
|
295
301
|
**table actions:**
|
296
302
|
- change_meta
|
297
|
-
-
|
303
|
+
- remove_meta
|
298
304
|
- add_mapping
|
299
305
|
- change_mapping
|
300
306
|
- change_mapping_meta
|
301
307
|
- change_mapping_attributes
|
308
|
+
- remove_mapping
|
302
309
|
- add_setting
|
303
310
|
- change_setting
|
304
|
-
-
|
311
|
+
- remove_setting
|
305
312
|
- add_alias
|
306
313
|
- change_alias
|
307
|
-
-
|
314
|
+
- remove_alias
|
308
315
|
|
309
316
|
```ruby
|
310
317
|
# Example migration
|
@@ -322,6 +329,11 @@ class AddTests < ActiveRecord::Migration[7.0]
|
|
322
329
|
# changes the auto-increment value
|
323
330
|
change_meta "assignments", :auto_increment, 3625
|
324
331
|
|
332
|
+
# removes the mapping 'updated_at' from the 'assignments' index.
|
333
|
+
# the flag 'recreate' is required, since 'remove' is not supported for elasticsearch.
|
334
|
+
# this will recreate the whole index (data will be LOST!!!)
|
335
|
+
remove_mapping :assignments, :updated_at, recreate: true
|
336
|
+
|
325
337
|
create_table "settings", force: true do |t|
|
326
338
|
t.mapping :created_at, :date
|
327
339
|
t.mapping :key, :integer do |m|
|
@@ -347,8 +359,8 @@ class AddTests < ActiveRecord::Migration[7.0]
|
|
347
359
|
t.add_alias('supersettings')
|
348
360
|
end
|
349
361
|
|
350
|
-
|
351
|
-
|
362
|
+
remove_alias('settings', :supersettings)
|
363
|
+
remove_setting('settings', 'index.search.idle.after')
|
352
364
|
|
353
365
|
change_table 'settings', force: true do |t|
|
354
366
|
t.integer :amount_of_newbies
|
@@ -357,10 +369,14 @@ class AddTests < ActiveRecord::Migration[7.0]
|
|
357
369
|
create_table "vintage", force: true do |t|
|
358
370
|
t.primary_key :number
|
359
371
|
t.string :name
|
372
|
+
t.string :comments
|
360
373
|
t.timestamps
|
361
374
|
end
|
362
375
|
|
363
|
-
|
376
|
+
change_table 'vintage', if_exists: true, recreate: true do |t|
|
377
|
+
t.change_mapping :number, fields: {raw: {type: :keyword}}
|
378
|
+
t.remove_mapping :number
|
379
|
+
end
|
364
380
|
end
|
365
381
|
|
366
382
|
def down
|
data/docs/CHANGELOG.md
CHANGED
@@ -1,5 +1,30 @@
|
|
1
1
|
# ElasticsearchRecord - CHANGELOG
|
2
2
|
|
3
|
+
## [1.3.0] - 2023-01-17
|
4
|
+
* [add] 'metas: {}' param for `CreateTableDefinition` to provide individual meta information without providing them through 'mappings'
|
5
|
+
* [add] 'change_'- & 'remove_'-methods for _mapping, setting & alias_ for `CreateTableDefinition`
|
6
|
+
* [add] `ActiveRecord::ConnectionAdapters::Elasticsearch::TableMappingDefinition#meta` for easier access
|
7
|
+
* [add] `UpdateTableDefinition#remove_mapping` to always raise an ArgumentError (now created @ `CreateTableDefinition` through change_table(x, recreate: true) )
|
8
|
+
* [add] `_env_table_name`-syntax to `ActiveRecord::ConnectionAdapters::Elasticsearch::SchemaDumper#table` for prefixed & suffixed tables in connections
|
9
|
+
* [add] `#cluster_health`, `#refresh_table`, `#refresh_tables` & `#rename_table` methods to 'connection'
|
10
|
+
* [add] `#change_table` 'recreate: false' parameter to switch between a 'change' or 'recreate' of an index
|
11
|
+
* [add] `#refresh` method to relations to explicit set the refresh value of the generated query
|
12
|
+
* [ref] `CloneTableDefinition` to adapt settings (number_of_shards & number_of_replicas) from source table by default
|
13
|
+
* [ref] `CreateTableDefinition#transform_mappings!` also support simple 'key->attributes' assignment of custom provided mappings
|
14
|
+
* [ref] 'delete_'-methods into more common 'remove_' for `UpdateTableDefinition`
|
15
|
+
* [ref] `UpdateTableDefinition#change_mapping` to always raise an ArgumentError (now moved to `CreateTableDefinition` through change_table(x, recreate: true) )
|
16
|
+
* [ref] `#change_table` missing '&block'
|
17
|
+
* [ref] 'delete_'-methods into more common 'remove_' to 'connection'
|
18
|
+
* [ref] ':\_\_claim\_\_'-operator to ':\_\_query\_\_' within `Arel::Collectors::ElasticsearchQuery`
|
19
|
+
* [ref] update & delete queries to preset a 'refresh: true' as default _(can be overwritten through 'relation.refresh(false)' )_
|
20
|
+
* [rem] `#clone_table` unusable '&block'
|
21
|
+
* [rem] `#compute_table_name`-method - must be explicit provided with `#_env_table_name(name)`
|
22
|
+
* [fix] `#where!` & `#build_where_clause` methods to also build a valid 'where_clause' in nested 'where' & 'or'
|
23
|
+
|
24
|
+
## [1.2.4] - 2022-12-15
|
25
|
+
* [fix] missing `#visit_Arel_Nodes_In` method in `Arel::Visitors::ElasticsearchQuery` to build array conditions
|
26
|
+
* [fix] resolving buckets from relation `ElasticsearchRecord::Result#buckets` not recognizing sub-buckets
|
27
|
+
|
3
28
|
## [1.2.3] - 2022-12-12
|
4
29
|
* [fix] `change_table` 'if_exists: true' returns at the wrong state
|
5
30
|
|
@@ -16,6 +16,9 @@ module ActiveRecord
|
|
16
16
|
@settings = HashWithIndifferentAccess.new
|
17
17
|
@aliases = HashWithIndifferentAccess.new
|
18
18
|
|
19
|
+
# before assigning new settings, we need to resolve some defaults
|
20
|
+
assign_default_clone_settings!
|
21
|
+
|
19
22
|
transform_settings!(settings) if settings.present?
|
20
23
|
transform_aliases!(aliases) if aliases.present?
|
21
24
|
end
|
@@ -64,6 +67,12 @@ module ActiveRecord
|
|
64
67
|
|
65
68
|
private
|
66
69
|
|
70
|
+
def assign_default_clone_settings!
|
71
|
+
settings = table_settings(name)
|
72
|
+
setting('index.number_of_shards', (settings.dig('index.number_of_shards') || 1))
|
73
|
+
setting('index.number_of_replicas', (settings.dig('index.number_of_replicas') || 0))
|
74
|
+
end
|
75
|
+
|
67
76
|
def _before_exec
|
68
77
|
block_table(self.name, :write)
|
69
78
|
end
|
@@ -6,7 +6,7 @@ module ActiveRecord
|
|
6
6
|
module ConnectionAdapters
|
7
7
|
module Elasticsearch
|
8
8
|
class CreateTableDefinition < TableDefinition
|
9
|
-
def initialize(conn, name, settings: nil, mappings: nil, aliases: nil, **opts)
|
9
|
+
def initialize(conn, name, settings: nil, mappings: nil, aliases: nil, metas: nil, **opts)
|
10
10
|
super(conn, name, **opts)
|
11
11
|
|
12
12
|
@settings = HashWithIndifferentAccess.new
|
@@ -17,6 +17,9 @@ module ActiveRecord
|
|
17
17
|
transform_settings!(settings) if settings.present?
|
18
18
|
transform_mappings!(mappings) if mappings.present?
|
19
19
|
transform_aliases!(aliases) if aliases.present?
|
20
|
+
# PLEASE NOTE: metas are already provided through the mappings (['_meta']),
|
21
|
+
# but this will support individually provided key<->values...
|
22
|
+
transform_metas!(metas) if metas.present?
|
20
23
|
end
|
21
24
|
|
22
25
|
# returns an array with all +TableSettingDefinition+.
|
@@ -64,18 +67,23 @@ module ActiveRecord
|
|
64
67
|
self
|
65
68
|
end
|
66
69
|
|
70
|
+
def change_meta(name, value, **options)
|
71
|
+
# simply force full overwrite
|
72
|
+
meta(name, value, force: true, **options)
|
73
|
+
end
|
74
|
+
|
67
75
|
def remove_meta(name)
|
68
76
|
@metas.delete(name)
|
69
77
|
end
|
70
78
|
|
71
79
|
# adds a new mapping
|
72
|
-
def mapping(name, type, force: false, **options, &block)
|
80
|
+
def mapping(name, type, if_not_exists: false, force: false, **options, &block)
|
81
|
+
return if if_not_exists && @mappings.key?(name)
|
73
82
|
raise ArgumentError, "you cannot define an already defined mapping '#{name}'!" if @mappings.key?(name) && !force?(force)
|
74
83
|
|
75
84
|
mapping = new_mapping_definition(name, type, **options, &block)
|
76
85
|
@mappings[name] = mapping
|
77
86
|
|
78
|
-
|
79
87
|
# check if the mapping is assigned as primary_key
|
80
88
|
if mapping.primary_key?
|
81
89
|
meta :primary_key, mapping.name
|
@@ -86,16 +94,51 @@ module ActiveRecord
|
|
86
94
|
end
|
87
95
|
|
88
96
|
# provide backwards compatibility to columns
|
89
|
-
alias column mapping
|
97
|
+
alias :column :mapping
|
98
|
+
alias :add_mapping :mapping
|
99
|
+
alias :add_column :mapping
|
100
|
+
|
101
|
+
def change_mapping(name, type, if_exists: false, **options, &block)
|
102
|
+
return if if_exists && !@mappings.key?(name)
|
103
|
+
|
104
|
+
raise ArgumentError, "you cannot change an unknown mapping '#{name}'" unless @mappings.key?(name)
|
105
|
+
|
106
|
+
mapping(name, type, force: true, **options, &block)
|
107
|
+
end
|
108
|
+
|
109
|
+
alias :change_column :change_mapping
|
110
|
+
alias :change :change_mapping
|
111
|
+
|
112
|
+
def change_mapping_meta(name, **options)
|
113
|
+
raise ArgumentError, "you cannot change the 'meta' parameter for an unknown mapping '#{name}'" unless @mappings.key?(name)
|
114
|
+
|
115
|
+
# merge mapping options
|
116
|
+
opts = @mappings[name].attributes
|
117
|
+
opts['meta'] = @mappings[name].meta.merge(options)
|
118
|
+
|
119
|
+
mapping(name, @mappings[name].type, force: true, **opts)
|
120
|
+
end
|
121
|
+
|
122
|
+
def change_mapping_attributes(name, if_exists: false, **options, &block)
|
123
|
+
return if if_exists && !@mappings.key?(name)
|
124
|
+
|
125
|
+
raise ArgumentError, "you cannot change an unknown mapping '#{name}'" unless @mappings.key?(name)
|
126
|
+
|
127
|
+
# merge mapping attributes
|
128
|
+
opts = @mappings[name].attributes.merge(options)
|
129
|
+
|
130
|
+
mapping(name, @mappings[name].type, force: true, **opts, &block)
|
131
|
+
end
|
90
132
|
|
91
133
|
def remove_mapping(name)
|
92
134
|
@mappings.delete(name)
|
93
135
|
end
|
94
136
|
|
95
137
|
# provide backwards compatibility to columns
|
96
|
-
alias remove_column remove_mapping
|
138
|
+
alias :remove_column :remove_mapping
|
97
139
|
|
98
|
-
def setting(name, value, force: false, **options, &block)
|
140
|
+
def setting(name, value, if_not_exists: false, force: false, **options, &block)
|
141
|
+
return if if_not_exists && @settings.key?(name)
|
99
142
|
raise ArgumentError, "you cannot define an already defined setting '#{name}'!" if @settings.key?(name) && !force?(force)
|
100
143
|
|
101
144
|
@settings[name] = new_setting_definition(name, value, **options, &block)
|
@@ -103,13 +146,19 @@ module ActiveRecord
|
|
103
146
|
self
|
104
147
|
end
|
105
148
|
|
149
|
+
def change_setting(name, value, if_exists: false, **options, &block)
|
150
|
+
return if if_exists && !@settings.key?(name)
|
151
|
+
|
152
|
+
# simply force full overwrite
|
153
|
+
setting(name, value, force: true, **options, &block)
|
154
|
+
end
|
155
|
+
|
106
156
|
def remove_setting(name)
|
107
157
|
@settings.delete name
|
108
158
|
end
|
109
159
|
|
110
|
-
|
111
|
-
|
112
|
-
def alias(name, force: false, **options, &block)
|
160
|
+
def add_alias(name, if_not_exists: false, force: false, **options, &block)
|
161
|
+
return if if_not_exists && @aliases.key?(name)
|
113
162
|
raise ArgumentError, "you cannot define an already defined alias '#{name}'." if @aliases.key?(name) && !force?(force)
|
114
163
|
|
115
164
|
@aliases[name] = new_alias_definition(name, **options, &block)
|
@@ -117,6 +166,13 @@ module ActiveRecord
|
|
117
166
|
self
|
118
167
|
end
|
119
168
|
|
169
|
+
def change_alias(name, if_exists: false, **options, &block)
|
170
|
+
return if if_exists && !@aliases.key?(name)
|
171
|
+
|
172
|
+
# simply force full overwrite
|
173
|
+
add_alias(name, force: true, **options, &block)
|
174
|
+
end
|
175
|
+
|
120
176
|
def remove_alias(name)
|
121
177
|
@aliases.delete name
|
122
178
|
end
|
@@ -154,6 +210,51 @@ module ActiveRecord
|
|
154
210
|
def state
|
155
211
|
nil
|
156
212
|
end
|
213
|
+
|
214
|
+
def transform_mappings!(mappings)
|
215
|
+
# transform +_meta+ mappings
|
216
|
+
if mappings['_meta'].present?
|
217
|
+
mappings['_meta'].each do |name, value|
|
218
|
+
self.meta(name, value, force: true)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# transform properties (=columns)
|
223
|
+
if mappings['properties'].present?
|
224
|
+
mappings['properties'].each do |name, attributes|
|
225
|
+
self.mapping(name, attributes.delete('type'), force: true, **attributes)
|
226
|
+
end
|
227
|
+
elsif mappings.present? && mappings.values[0].is_a?(Hash)
|
228
|
+
# raw settings where provided with just (key => attributes)
|
229
|
+
mappings.each do |name, attributes|
|
230
|
+
self.mapping(name, attributes.delete('type'), force: true, **attributes)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def transform_settings!(settings)
|
236
|
+
# exclude settings, that are provided through the API but are not part of the index-settings
|
237
|
+
settings
|
238
|
+
.with_indifferent_access
|
239
|
+
.each { |name, value|
|
240
|
+
# don't transform ignored names
|
241
|
+
next if ActiveRecord::ConnectionAdapters::Elasticsearch::TableSettingDefinition.match_ignore_names?(name)
|
242
|
+
|
243
|
+
self.setting(name, value, force: true)
|
244
|
+
}
|
245
|
+
end
|
246
|
+
|
247
|
+
def transform_aliases!(aliases)
|
248
|
+
aliases.each do |name, attributes|
|
249
|
+
self.alias(name, force: true, **attributes)
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def transform_metas!(metas)
|
254
|
+
metas.each do |name, value|
|
255
|
+
self.meta(name, value, force: true)
|
256
|
+
end
|
257
|
+
end
|
157
258
|
end
|
158
259
|
end
|
159
260
|
end
|
data/lib/active_record/connection_adapters/elasticsearch/schema_definitions/table_definition.rb
CHANGED
@@ -147,40 +147,6 @@ module ActiveRecord
|
|
147
147
|
def strict?(strict = nil)
|
148
148
|
opts.fetch(:strict, false) || strict
|
149
149
|
end
|
150
|
-
|
151
|
-
def transform_mappings!(mappings)
|
152
|
-
# transform +_meta+ mappings
|
153
|
-
if mappings['_meta'].present?
|
154
|
-
mappings['_meta'].each do |name, value|
|
155
|
-
self.meta(name, value)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
# transform properties (=columns)
|
160
|
-
if mappings['properties'].present?
|
161
|
-
mappings['properties'].each do |name, attributes|
|
162
|
-
self.mapping(name, attributes.delete('type'), **attributes)
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
def transform_settings!(settings)
|
168
|
-
# exclude settings, that are provided through the API but are not part of the index-settings
|
169
|
-
settings
|
170
|
-
.with_indifferent_access
|
171
|
-
.each { |name, value|
|
172
|
-
# don't transform ignored names
|
173
|
-
next if ActiveRecord::ConnectionAdapters::Elasticsearch::TableSettingDefinition.match_ignore_names?(name)
|
174
|
-
|
175
|
-
self.setting(name, value)
|
176
|
-
}
|
177
|
-
end
|
178
|
-
|
179
|
-
def transform_aliases!(aliases)
|
180
|
-
aliases.each do |name, attributes|
|
181
|
-
self.alias(name, **attributes)
|
182
|
-
end
|
183
|
-
end
|
184
150
|
end
|
185
151
|
end
|
186
152
|
end
|
@@ -99,6 +99,12 @@ module ActiveRecord
|
|
99
99
|
_lazy_attributes[:auto_increment] = value
|
100
100
|
end
|
101
101
|
|
102
|
+
# returns the meta hash
|
103
|
+
# @param [Hash]
|
104
|
+
def meta
|
105
|
+
__get_attribute(:meta) || {}
|
106
|
+
end
|
107
|
+
|
102
108
|
def meta=(value)
|
103
109
|
if value.nil?
|
104
110
|
__remove_attribute(:meta)
|
@@ -8,15 +8,13 @@ module ActiveRecord
|
|
8
8
|
module Elasticsearch
|
9
9
|
class UpdateTableDefinition < TableDefinition
|
10
10
|
|
11
|
-
attr_reader :definitions
|
12
|
-
|
13
11
|
# defines which definitions can be executed composite
|
14
12
|
COMPOSITE_DEFINITIONS = [
|
15
13
|
AddMappingDefinition,
|
16
14
|
ChangeMappingDefinition,
|
17
15
|
ChangeMetaDefinition,
|
18
16
|
AddSettingDefinition,
|
19
|
-
|
17
|
+
RemoveAliasDefinition
|
20
18
|
].freeze
|
21
19
|
|
22
20
|
def add_mapping(name, type, if_not_exists: false, **options, &block)
|
@@ -35,23 +33,14 @@ module ActiveRecord
|
|
35
33
|
define! ChangeMetaDefinition, new_meta_definition(name, value, **options)
|
36
34
|
end
|
37
35
|
|
38
|
-
def
|
36
|
+
def remove_meta(name, **options)
|
39
37
|
load_meta_definition!
|
40
38
|
|
41
39
|
define! ChangeMetaDefinition, new_meta_definition(name, nil, **options)
|
42
40
|
end
|
43
41
|
|
44
|
-
def change_mapping(name, type,
|
45
|
-
|
46
|
-
|
47
|
-
mapping = new_mapping_definition(name, type, **options, &block)
|
48
|
-
define! ChangeMappingDefinition, mapping
|
49
|
-
|
50
|
-
# check if the mapping is assigned as primary_key
|
51
|
-
if mapping.primary_key?
|
52
|
-
change_meta :primary_key, mapping.name
|
53
|
-
change_meta(:auto_increment, mapping.auto_increment) if mapping.auto_increment?
|
54
|
-
end
|
42
|
+
def change_mapping(name, type, **options, &block)
|
43
|
+
raise ArgumentError, "you cannot change the mapping '#{name}' without the 'recreate: true' flag in the '#change_table' call"
|
55
44
|
end
|
56
45
|
|
57
46
|
alias :change_column :change_mapping
|
@@ -67,16 +56,28 @@ module ActiveRecord
|
|
67
56
|
define! ChangeMappingDefinition, new_mapping_definition(name, mapping['type'], meta: meta)
|
68
57
|
end
|
69
58
|
|
70
|
-
def change_mapping_attributes(name, **options, &block)
|
71
|
-
|
72
|
-
|
59
|
+
def change_mapping_attributes(name, if_exists: false, **options, &block)
|
60
|
+
return if if_exists && !mapping_exists?(self.name, name)
|
61
|
+
|
62
|
+
# receive current mapping
|
63
|
+
current_mapping = table_mappings(self.name).dig('properties', name.to_s)
|
64
|
+
raise ArgumentError, "you cannot change an unknown mapping '#{name}'" if current_mapping.blank?
|
73
65
|
|
74
|
-
|
66
|
+
# build new mapping
|
67
|
+
mapping = new_mapping_definition(name, current_mapping[:type], **options, &block)
|
68
|
+
define! ChangeMappingDefinition, mapping
|
75
69
|
|
76
|
-
|
70
|
+
# check if the mapping is assigned as new primary_key
|
71
|
+
if mapping.primary_key?
|
72
|
+
change_meta :primary_key, mapping.name
|
73
|
+
change_meta(:auto_increment, mapping.auto_increment) if mapping.auto_increment?
|
74
|
+
end
|
77
75
|
end
|
78
76
|
|
79
|
-
|
77
|
+
def remove_mapping(name)
|
78
|
+
raise ArgumentError, "you cannot remove the mapping '#{name}' without the 'recreate: true' flag in the '#change_table' call"
|
79
|
+
end
|
80
|
+
alias :remove_column :remove_mapping
|
80
81
|
|
81
82
|
def add_setting(name, value, if_not_exists: false, **options, &block)
|
82
83
|
return if if_not_exists && setting_exists?(self.name, name)
|
@@ -90,10 +91,10 @@ module ActiveRecord
|
|
90
91
|
define! ChangeSettingDefinition, new_setting_definition(name, value, **options, &block)
|
91
92
|
end
|
92
93
|
|
93
|
-
def
|
94
|
+
def remove_setting(name, if_exists: false, **options, &block)
|
94
95
|
return if if_exists && !setting_exists?(self.name, name)
|
95
96
|
|
96
|
-
define!
|
97
|
+
define! RemoveSettingDefinition, new_setting_definition(name, nil, **options, &block)
|
97
98
|
end
|
98
99
|
|
99
100
|
def add_alias(name, if_not_exists: false, **options, &block)
|
@@ -108,10 +109,10 @@ module ActiveRecord
|
|
108
109
|
define! ChangeAliasDefinition, new_alias_definition(name, **options, &block)
|
109
110
|
end
|
110
111
|
|
111
|
-
def
|
112
|
+
def remove_alias(name, if_exists: false, **, &block)
|
112
113
|
return if if_exists && !alias_exists?(self.name, name)
|
113
114
|
|
114
|
-
define!
|
115
|
+
define! RemoveAliasDefinition, new_alias_definition(name, &block)
|
115
116
|
end
|
116
117
|
|
117
118
|
# Appends <tt>:datetime</tt> columns <tt>:created_at</tt> and
|
@@ -123,6 +124,11 @@ module ActiveRecord
|
|
123
124
|
add_mapping(:updated_at, :datetime, if_not_exists: true, **options)
|
124
125
|
end
|
125
126
|
|
127
|
+
# returns the definitions hash
|
128
|
+
def definitions
|
129
|
+
@definitions ||= {}
|
130
|
+
end
|
131
|
+
|
126
132
|
private
|
127
133
|
|
128
134
|
# loads existing meta definitions from table_mappings
|
@@ -130,16 +136,14 @@ module ActiveRecord
|
|
130
136
|
return if @load_meta_definition
|
131
137
|
@load_meta_definition = true
|
132
138
|
|
133
|
-
|
134
139
|
table_metas(self.name).each do |name, value|
|
135
140
|
define! ChangeMetaDefinition, new_meta_definition(name, value)
|
136
141
|
end
|
137
142
|
end
|
138
143
|
|
139
144
|
def define!(klass, item)
|
140
|
-
|
141
|
-
|
142
|
-
@definitions[klass] << item
|
145
|
+
self.definitions[klass] ||= []
|
146
|
+
self.definitions[klass] << item
|
143
147
|
end
|
144
148
|
|
145
149
|
def _before_assign
|
@@ -157,6 +161,7 @@ module ActiveRecord
|
|
157
161
|
# reset table state
|
158
162
|
clear_state!
|
159
163
|
end
|
164
|
+
|
160
165
|
alias :_rescue_assign :_after_exec
|
161
166
|
alias :_rescue_exec :_after_exec
|
162
167
|
|
@@ -14,12 +14,12 @@ module ActiveRecord
|
|
14
14
|
# setting definitions
|
15
15
|
AddSettingDefinition = Struct.new(:items) # composite
|
16
16
|
ChangeSettingDefinition = Struct.new(:items) # composite
|
17
|
-
|
17
|
+
RemoveSettingDefinition = Struct.new(:items) # composite
|
18
18
|
|
19
19
|
# alias definitions
|
20
20
|
AddAliasDefinition = Struct.new(:item) # single
|
21
21
|
ChangeAliasDefinition = Struct.new(:item) # single
|
22
|
-
|
22
|
+
RemoveAliasDefinition = Struct.new(:items) # composite
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -20,6 +20,11 @@ module ActiveRecord
|
|
20
20
|
@options[:table_name_suffix] = @connection.table_name_suffix if @options[:table_name_suffix].blank?
|
21
21
|
end
|
22
22
|
|
23
|
+
# returns true if the connection has table-names, that are environment related
|
24
|
+
def _has_env_table_names?
|
25
|
+
@options[:table_name_prefix].present? || @options[:table_name_suffix].present?
|
26
|
+
end
|
27
|
+
|
23
28
|
# overwrite the method to 'fix' a possible ActiveRecord bug:
|
24
29
|
# If a +table_name_prefix+ or +table_name_suffix+ was provided we also only want to dump those tables,
|
25
30
|
# which matches the prefix/suffix. So possible, environment-related tables will be ignored
|
@@ -35,7 +40,7 @@ module ActiveRecord
|
|
35
40
|
return true if @options[:table_name_prefix].present? && !table_name.start_with?(@options[:table_name_prefix].to_s)
|
36
41
|
|
37
42
|
# if the table ends NOT with +suffix+ it must be ignored
|
38
|
-
return true if
|
43
|
+
return true if @options[:table_name_suffix].present? && !table_name.end_with?(@options[:table_name_suffix].to_s)
|
39
44
|
|
40
45
|
false
|
41
46
|
end
|
@@ -50,7 +55,14 @@ module ActiveRecord
|
|
50
55
|
# resolve string printer
|
51
56
|
tbl = StringIO.new
|
52
57
|
|
53
|
-
tbl.print " create_table
|
58
|
+
tbl.print " create_table"
|
59
|
+
|
60
|
+
if _has_env_table_names?
|
61
|
+
tbl.print " _env_table_name(#{remove_prefix_and_suffix(table).inspect})"
|
62
|
+
else
|
63
|
+
tbl.print " #{remove_prefix_and_suffix(table).inspect}"
|
64
|
+
end
|
65
|
+
|
54
66
|
tbl.print ", force: true do |t|"
|
55
67
|
|
56
68
|
# META
|
@@ -399,6 +399,12 @@ module ActiveRecord
|
|
399
399
|
settings['persistent'].merge(settings['transient'])
|
400
400
|
end
|
401
401
|
|
402
|
+
# returns the cluster health
|
403
|
+
# @return [Hash]
|
404
|
+
def cluster_health(**options)
|
405
|
+
api(:cluster, :health, options, 'CLUSTER HEALTH').to_h
|
406
|
+
end
|
407
|
+
|
402
408
|
# transforms provided schema-type to a sql-type
|
403
409
|
# overwrite original methods to provide a elasticsearch version
|
404
410
|
# @param [String, Symbol] type
|
@@ -467,7 +473,7 @@ module ActiveRecord
|
|
467
473
|
|
468
474
|
# overwrite original methods to provide a elasticsearch version
|
469
475
|
def extract_table_options!(options)
|
470
|
-
options.extract!(:settings, :mappings, :aliases, :force, :strict)
|
476
|
+
options.extract!(:settings, :mappings, :aliases, :metas, :force, :strict)
|
471
477
|
end
|
472
478
|
end
|
473
479
|
end
|