Class: Cauchy::Migrator
- Inherits:
-
Object
- Object
- Cauchy::Migrator
- Defined in:
- lib/cauchy/migrator.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
- .migrate(client, index_paths = nil, target_index = nil, options = {}) ⇒ Object
- .status(client, index_paths = nil, target_index = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(client, schema) ⇒ Migrator
constructor
A new instance of Migrator.
- #inspect ⇒ Object
- #migrate(options = {}) ⇒ Object
- #migration ⇒ Object
- #new_index ⇒ Object
- #new_index_name ⇒ Object
- #old_index ⇒ Object
- #old_schema ⇒ Object
- #reindex(options = {}) ⇒ Object
- #reindex_data ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(client, schema) ⇒ Migrator
Returns a new instance of Migrator.
70 71 72 73 |
# File 'lib/cauchy/migrator.rb', line 70 def initialize(client, schema) @client = client @schema = schema end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
68 69 70 |
# File 'lib/cauchy/migrator.rb', line 68 def client @client end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
68 69 70 |
# File 'lib/cauchy/migrator.rb', line 68 def schema @schema end |
Class Method Details
.migrate(client, index_paths = nil, target_index = nil, options = {}) ⇒ Object
36 37 38 39 40 |
# File 'lib/cauchy/migrator.rb', line 36 def migrate(client, index_paths = nil, target_index = nil, = {}) with_schemas(index_paths, target_index) do |schema| new(client, schema).migrate end end |
.status(client, index_paths = nil, target_index = nil) ⇒ Object
42 43 44 45 46 |
# File 'lib/cauchy/migrator.rb', line 42 def status(client, index_paths = nil, target_index = nil) with_schemas(index_paths, target_index) do |schema| new(client, schema).status end end |
Instance Method Details
#inspect ⇒ Object
190 191 192 193 194 |
# File 'lib/cauchy/migrator.rb', line 190 def inspect "#<#{self.class.name}"\ " index_alias=\"#{index_alias}\""\ " version=\"#{version[0..6]}\">" end |
#migrate(options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cauchy/migrator.rb', line 93 def migrate( = {}) raise MultipleIndexAliasError, index_alias if index_aliases.keys.length > 1 if [:reindex] || !old_index.exists? return reindex() end if migration.up_to_date? log 'Index is up-to-date.' return true end if migration.requires_reindex? log migration_diff, :unknown log 'Requires reindexing!', :warn return false end if migration.changes_settings? log 'Updating index settings... ' do begin old_index.close if [:close_index] old_index.settings = migration.changed_settings rescue Elastic::CannotUpdateNonDynamicSettingsError log migration_diff, :unknown raise ensure old_index.open if [:close_index] end 'done.' end end if migration.changes_mappings? log 'Updating index mappings... ' do old_index.mappings = migration.new_schema.mappings 'done.' end end end |
#migration ⇒ Object
89 90 91 |
# File 'lib/cauchy/migrator.rb', line 89 def migration @migration ||= Migration.new(old_schema, schema) end |
#new_index ⇒ Object
81 82 83 |
# File 'lib/cauchy/migrator.rb', line 81 def new_index @new_index ||= client.index(new_index_name) end |
#new_index_name ⇒ Object
77 78 79 |
# File 'lib/cauchy/migrator.rb', line 77 def new_index_name "#{index_alias}_#{version}" end |
#old_index ⇒ Object
85 86 87 |
# File 'lib/cauchy/migrator.rb', line 85 def old_index @old_index ||= client.index(resolve_index) end |
#old_schema ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/cauchy/migrator.rb', line 196 def old_schema @old_schema ||= begin IndexSchema.new(index_alias).tap do |schema| schema.mappings = old_index.mappings schema.settings = old_index.settings end end end |
#reindex(options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cauchy/migrator.rb', line 134 def reindex( = {}) if old_index.exists? if migration.up_to_date? log 'Index is up-to-date.' return true elsif !migration.requires_reindex? log 'Does not require reindexing... skipping.' return false end end begin log "Creating new index #{new_index_name}" new_index.create settings: schema.settings, mappings: schema.mappings rescue Elastic::IndexAlreadyExistsError log "Index #{new_index_name} already exists, continuing...", :warn end reindex_data if old_index.exists? cleanup true end |
#reindex_data ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cauchy/migrator.rb', line 159 def reindex_data log 'Reindexing...' = nil old_index.scroll do |documents, total| docs = documents.map do |h| { index: { _index: new_index.name, _type: h['_type'], _id: h['_id'], data: h['_source'] } } end client.bulk docs ||= ProgressBar.create(total: total, throttle_rate: 0.1, format: '%a |%B| %e') .progress += docs.size end end |
#status ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'lib/cauchy/migrator.rb', line 181 def status if migration.up_to_date? log 'Index is up-to-date.' else log migration_diff, :unknown log 'Requires reindexing!', :warn if migration.requires_reindex? end end |