Class: ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition

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

Instance Method Summary collapse

Constructor Details

#initialize(conn, name, settings: nil, mappings: nil, aliases: nil, metas: nil, **opts) ⇒ CreateTableDefinition

Returns a new instance of CreateTableDefinition.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 9

def initialize(conn, name, settings: nil, mappings: nil, aliases: nil, metas: nil, **opts)
  super(conn, name, **opts)

  @settings = HashWithIndifferentAccess.new
  @mappings = HashWithIndifferentAccess.new
  @aliases  = HashWithIndifferentAccess.new
  @metas    = HashWithIndifferentAccess.new

  transform_settings!(settings) if settings.present?
  transform_mappings!(mappings) if mappings.present?
  transform_aliases!(aliases) if aliases.present?
  # PLEASE NOTE: metas are already provided through the mappings (['_meta']),
  # but this will support individually provided key<->values...
  transform_metas!(metas) if metas.present?
end

Instance Method Details

#[](name) ⇒ Object

Returns a MappingDefinition for the mapping with name +name+.



53
54
55
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 53

def [](name)
  @mappings[name]
end

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

Raises:

  • (ArgumentError)


160
161
162
163
164
165
166
167
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 160

def add_alias(name, if_not_exists: false, force: false, **options, &block)
  return if if_not_exists && @aliases.key?(name)
  raise ArgumentError, "you cannot define an already defined alias '#{name}'." if @aliases.key?(name) && !force?(force)

  @aliases[name] = new_alias_definition(name, **options, &block)

  self
end

#aliasesArray

returns an array with all +TableAliasDefinition+.

Returns:

  • (Array)


48
49
50
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 48

def aliases
  @aliases.values
end

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



169
170
171
172
173
174
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 169

def change_alias(name, if_exists: false, **options, &block)
  return if if_exists && !@aliases.key?(name)

  # simply force full overwrite
  add_alias(name, force: true, **options, &block)
end

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

Raises:

  • (ArgumentError)


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

def change_mapping(name, type, if_exists: false, **options, &block)
  return if if_exists && !@mappings.key?(name)

  raise ArgumentError, "you cannot change an unknown mapping '#{name}'" unless @mappings.key?(name)

  mapping(name, type, force: true, **options, &block)
end

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

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 122

def change_mapping_attributes(name, if_exists: false, **options, &block)
  return if if_exists && !@mappings.key?(name)

  raise ArgumentError, "you cannot change an unknown mapping '#{name}'" unless @mappings.key?(name)

  # merge mapping attributes
  opts = @mappings[name].attributes.merge(options)

  mapping(name, @mappings[name].type, force: true, **opts, &block)
end

#change_mapping_meta(name, **options) ⇒ Object

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 112

def change_mapping_meta(name, **options)
  raise ArgumentError, "you cannot change the 'meta' parameter for an unknown mapping '#{name}'" unless @mappings.key?(name)

  # merge mapping options
  opts = @mappings[name].attributes
  opts['meta'] = @mappings[name].meta.merge(options)

  mapping(name, @mappings[name].type, force: true, **opts)
end

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



70
71
72
73
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 70

def change_meta(name, value, **options)
  # simply force full overwrite
  meta(name, value, force: true, **options)
end

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



149
150
151
152
153
154
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 149

def change_setting(name, value, if_exists: false, **options, &block)
  return if if_exists && !@settings.key?(name)

  # simply force full overwrite
  setting(name, value, force: true, **options, &block)
end

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

adds a new mapping

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 80

def mapping(name, type, if_not_exists: false, force: false, **options, &block)
  return if if_not_exists && @mappings.key?(name)
  raise ArgumentError, "you cannot define an already defined mapping '#{name}'!" if @mappings.key?(name) && !force?(force)

  mapping = new_mapping_definition(name, type, **options, &block)
  @mappings[name] = mapping

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

  self
end

#mappingsArray Also known as: columns

returns an array with all +TableMappingDefinition+.

Returns:

  • (Array)


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

def mappings
  @mappings.values
end

#meta(name, value, force: false, **options) ⇒ Object

adds a new meta

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 62

def meta(name, value, force: false, **options)
  raise ArgumentError, "you cannot define an already defined meta '#{name}'!" if @metas.key?(name) && !force?(force)

  @metas[name] = new_meta_definition(name, value, **options)

  self
end

#metasArray

returns an array with all +TableMetaDefinition+.

Returns:

  • (Array)


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

def metas
  @metas.values
end

#references(*args, **options) ⇒ Object Also known as: belongs_to

Adds a reference.

t.references(:user) t.belongs_to(:supplier)

See ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition.connectionconnection.add_reference[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use.



186
187
188
189
190
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 186

def references(*args, **options)
  args.each do |ref_name|
    ActiveRecord::ConnectionAdapters::ReferenceDefinition.new(ref_name, **options).add_to(self)
  end
end

#remove_alias(name) ⇒ Object



176
177
178
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 176

def remove_alias(name)
  @aliases.delete name
end

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



133
134
135
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 133

def remove_mapping(name)
  @mappings.delete(name)
end

#remove_meta(name) ⇒ Object



75
76
77
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 75

def remove_meta(name)
  @metas.delete(name)
end

#remove_setting(name) ⇒ Object



156
157
158
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 156

def remove_setting(name)
  @settings.delete name
end

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

Raises:

  • (ArgumentError)


140
141
142
143
144
145
146
147
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 140

def setting(name, value, if_not_exists: false, force: false, **options, &block)
  return if if_not_exists && @settings.key?(name)
  raise ArgumentError, "you cannot define an already defined setting '#{name}'!" if @settings.key?(name) && !force?(force)

  @settings[name] = new_setting_definition(name, value, **options, &block)

  self
end

#settingsArray

returns an array with all +TableSettingDefinition+.

Returns:

  • (Array)


27
28
29
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 27

def settings
  @settings.values
end

#timestamps(**options) ⇒ Object

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

t.timestamps



198
199
200
201
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 198

def timestamps(**options)
  column(:created_at, :datetime, **options)
  column(:updated_at, :datetime, **options)
end