Class: ActiveRecord::ConnectionAdapters::Elasticsearch::UpdateTableDefinition

Inherits:
TableDefinition
  • Object
show all
Defined in:
lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb

Constant Summary collapse

COMPOSITE_DEFINITIONS =

defines which definitions can be executed composite

[
  AddMappingDefinition,
  ChangeMappingDefinition,
  ChangeMetaDefinition,
  AddSettingDefinition,
  ChangeSettingDefinition,
  RemoveSettingDefinition,
  RemoveAliasDefinition
].freeze

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from ActiveRecord::ConnectionAdapters::Elasticsearch::TableDefinition

Instance Method Details

#add_alias(name, if_not_exists: false, **options, &block) ⇒ Object



102
103
104
105
106
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 102

def add_alias(name, if_not_exists: false, **options, &block)
  return if if_not_exists && alias_exists?(self.name, name)

  define! AddAliasDefinition, new_alias_definition(name, **options, &block)
end

#add_mapping(name, type, if_not_exists: false, **options, &block) ⇒ Object Also known as: add_column, mapping, column



22
23
24
25
26
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 22

def add_mapping(name, type, if_not_exists: false, **options, &block)
  return if if_not_exists && mapping_exists?(self.name, name, type)

  define! AddMappingDefinition, new_mapping_definition(name, type, **options, &block)
end

#add_setting(name, value, if_not_exists: false, **options, &block) ⇒ Object



84
85
86
87
88
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 84

def add_setting(name, value, if_not_exists: false, **options, &block)
  return if if_not_exists && setting_exists?(self.name, name)

  define! AddSettingDefinition, new_setting_definition(name, value, **options, &block)
end

#change_alias(name, if_exists: false, **options, &block) ⇒ Object



108
109
110
111
112
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 108

def change_alias(name, if_exists: false, **options, &block)
  return if if_exists && !alias_exists?(self.name, name)

  define! ChangeAliasDefinition, new_alias_definition(name, **options, &block)
end

#change_mapping(name, type, **options, &block) ⇒ Object Also known as: change_column, change

Raises:

  • (ArgumentError)


44
45
46
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 44

def change_mapping(name, type, **options, &block)
  raise ArgumentError, "you cannot change the mapping '#{name}' without the 'recreate: true' flag in the '#change_table' call"
end

#change_mapping_attributes(name, if_exists: false, **options, &block) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 61

def change_mapping_attributes(name, if_exists: false, **options, &block)
  return if if_exists && !mapping_exists?(self.name, name)

  # receive current mapping
  current_mapping = table_mappings(self.name).dig('properties', name.to_s)
  raise ArgumentError, "you cannot change an unknown mapping '#{name}'" if current_mapping.blank?

  # build new mapping
  mapping = new_mapping_definition(name, current_mapping[:type], **options, &block)
  define! ChangeMappingDefinition, mapping

  # check if the mapping is assigned as new primary_key
  if mapping.primary_key?
    change_meta :primary_key, mapping.name
    change_meta(:auto_increment, mapping.auto_increment) if mapping.auto_increment?
  end
end

#change_mapping_meta(name, **options) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 51

def change_mapping_meta(name, **options)
  mapping = table_mappings(self.name).dig('properties', name.to_s)
  raise ArgumentError, "you cannot change the 'meta' parameter for an unknown mapping '#{name}'" if mapping.blank?

  # resolve existing meta & merge with new
  meta = (mapping['meta'] || {}).merge(options)

  define! ChangeMappingDefinition, new_mapping_definition(name, mapping['type'], meta: meta)
end

#change_meta(name, value, **options) ⇒ Object



32
33
34
35
36
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 32

def change_meta(name, value, **options)
  load_meta_definition!

  define! ChangeMetaDefinition, new_meta_definition(name, value, **options)
end

#change_setting(name, value, if_exists: false, **options, &block) ⇒ Object



90
91
92
93
94
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 90

def change_setting(name, value, if_exists: false, **options, &block)
  return if if_exists && !setting_exists?(self.name, name)

  define! ChangeSettingDefinition, new_setting_definition(name, value, **options, &block)
end

#definitionsObject

returns the definitions hash



130
131
132
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 130

def definitions
  @definitions ||= {}
end

#remove_alias(name, if_exists: false, &block) ⇒ Object



114
115
116
117
118
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 114

def remove_alias(name, if_exists: false, **, &block)
  return if if_exists && !alias_exists?(self.name, name)

  define! RemoveAliasDefinition, new_alias_definition(name, &block)
end

#remove_mapping(name) ⇒ Object Also known as: remove_column

Raises:

  • (ArgumentError)


79
80
81
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 79

def remove_mapping(name)
  raise ArgumentError, "you cannot remove the mapping '#{name}' without the 'recreate: true' flag in the '#change_table' call"
end

#remove_meta(name, **options) ⇒ Object



38
39
40
41
42
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 38

def remove_meta(name, **options)
  load_meta_definition!

  define! ChangeMetaDefinition, new_meta_definition(name, nil, **options)
end

#remove_setting(name, if_exists: false, **options, &block) ⇒ Object



96
97
98
99
100
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 96

def remove_setting(name, if_exists: false, **options, &block)
  return if if_exists && !setting_exists?(self.name, name)

  define! RemoveSettingDefinition, new_setting_definition(name, nil, **options, &block)
end

#timestamps(**options) ⇒ Object

Appends :datetime columns :created_at and :updated_at to the table. See ActiveRecord::ConnectionAdapters::Elasticsearch::UpdateTableDefinition.connectionconnection.add_timestamps[rdoc-ref:SchemaStatements#add_timestamps]

t.timestamps



124
125
126
127
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/update_table_definition.rb', line 124

def timestamps(**options)
  add_mapping(:created_at, :datetime, if_not_exists: true, **options)
  add_mapping(:updated_at, :datetime, if_not_exists: true, **options)
end