Class: ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition
- Inherits:
-
TableDefinition
- Object
- TableDefinition
- ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition
- Defined in:
- lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns a MappingDefinition for the mapping with name +name+.
- #add_alias(name, if_not_exists: false, force: false, **options, &block) ⇒ Object
-
#aliases ⇒ Array
returns an array with all +TableAliasDefinition+.
- #change_alias(name, if_exists: false, **options, &block) ⇒ Object
- #change_mapping(name, type, if_exists: false, **options, &block) ⇒ Object (also: #change_column, #change)
- #change_mapping_attributes(name, if_exists: false, **options, &block) ⇒ Object
- #change_mapping_meta(name, **options) ⇒ Object
- #change_meta(name, value, **options) ⇒ Object
- #change_setting(name, value, if_exists: false, **options, &block) ⇒ Object
-
#initialize(conn, name, settings: nil, mappings: nil, aliases: nil, metas: nil, **opts) ⇒ CreateTableDefinition
constructor
A new instance of CreateTableDefinition.
-
#mapping(name, type, if_not_exists: false, force: false, **options, &block) ⇒ Object
(also: #column, #add_mapping, #add_column)
adds a new mapping.
-
#mappings ⇒ Array
(also: #columns)
returns an array with all +TableMappingDefinition+.
-
#meta(name, value, force: false, **options) ⇒ Object
adds a new meta.
-
#metas ⇒ Array
returns an array with all +TableMetaDefinition+.
-
#references(*args, **options) ⇒ Object
(also: #belongs_to)
Adds a reference.
- #remove_alias(name) ⇒ Object
- #remove_mapping(name) ⇒ Object (also: #remove_column)
- #remove_meta(name) ⇒ Object
- #remove_setting(name) ⇒ Object
- #setting(name, value, if_not_exists: false, force: false, **options, &block) ⇒ Object
-
#settings ⇒ Array
returns an array with all +TableSettingDefinition+.
-
#timestamps(**options) ⇒ Object
Appends :datetime columns :created_at and :updated_at to the table.
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... () if .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
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, **, &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, **, &block) self end |
#aliases ⇒ Array
returns an array with all +TableAliasDefinition+.
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, **, &block) return if if_exists && !@aliases.key?(name) # simply force full overwrite add_alias(name, force: true, **, &block) end |
#change_mapping(name, type, if_exists: false, **options, &block) ⇒ Object Also known as: change_column, change
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, **, &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, **, &block) end |
#change_mapping_attributes(name, if_exists: false, **options, &block) ⇒ Object
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, **, &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() mapping(name, @mappings[name].type, force: true, **opts, &block) end |
#change_mapping_meta(name, **options) ⇒ Object
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 (name, **) 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]..merge() 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 (name, value, **) # simply force full overwrite (name, value, force: true, **) 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, **, &block) return if if_exists && !@settings.key?(name) # simply force full overwrite setting(name, value, force: true, **, &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
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, **, &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, **, &block) @mappings[name] = mapping # check if the mapping is assigned as primary_key if mapping.primary_key? :primary_key, mapping.name (:auto_increment, mapping.auto_increment) if mapping.auto_increment? end self end |
#mappings ⇒ Array Also known as: columns
returns an array with all +TableMappingDefinition+.
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
62 63 64 65 66 67 68 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 62 def (name, value, force: false, **) raise ArgumentError, "you cannot define an already defined meta '#{name}'!" if @metas.key?(name) && !force?(force) @metas[name] = (name, value, **) self end |
#metas ⇒ Array
returns an array with all +TableMetaDefinition+.
39 40 41 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 39 def @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, **) args.each do |ref_name| ActiveRecord::ConnectionAdapters::ReferenceDefinition.new(ref_name, **).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 (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
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, **, &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, **, &block) self end |
#settings ⇒ Array
returns an array with all +TableSettingDefinition+.
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 (**) column(:created_at, :datetime, **) column(:updated_at, :datetime, **) end |