Class: Cauchy::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/cauchy/migration.rb

Constant Summary collapse

CREATION_SETTINGS =
%w[
  codec
  number_of_shards
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, new_schema) ⇒ Migration

Returns a new instance of Migration.



13
14
15
16
# File 'lib/cauchy/migration.rb', line 13

def initialize(schema, new_schema)
  @schema = schema
  @new_schema = new_schema
end

Instance Attribute Details

#new_schemaObject

Returns the value of attribute new_schema.



11
12
13
# File 'lib/cauchy/migration.rb', line 11

def new_schema
  @new_schema
end

#schemaObject

Returns the value of attribute schema.



11
12
13
# File 'lib/cauchy/migration.rb', line 11

def schema
  @schema
end

Instance Method Details

#changed_settingsObject



35
36
37
38
39
# File 'lib/cauchy/migration.rb', line 35

def changed_settings
  new_schema.settings.select do |name, setting|
    schema.settings[name] != setting
  end.to_h
end

#changes_creation_settings?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/cauchy/migration.rb', line 45

def changes_creation_settings?
  schema.settings.slice(*CREATION_SETTINGS).except(*removed_settings) !=
    new_schema.settings.slice(*CREATION_SETTINGS)
end

#changes_existing_mappings?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/cauchy/migration.rb', line 27

def changes_existing_mappings?
  (new_schema.types & schema.types).any? do |type|
    mapping, new_mapping = schema.mapping_for(type), new_schema.mapping_for(type)
    common_fields = mapping.keys & new_mapping.keys
    mapping.slice(*common_fields) != new_mapping.slice(*common_fields)
  end
end

#changes_mappings?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/cauchy/migration.rb', line 18

def changes_mappings?
  (new_schema.types - schema.types).any? ||
    (new_schema.types & schema.types).any? do |type|
      mapping, new_mapping = schema.mapping_for(type), new_schema.mapping_for(type)
      removed_fields = mapping.keys - new_mapping.keys
      mapping.except(*removed_fields) != new_mapping
    end
end

#changes_settings?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/cauchy/migration.rb', line 41

def changes_settings?
  changed_settings.present?
end

#mappings_diffsObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/cauchy/migration.rb', line 62

def mappings_diffs
  (schema.mappings.keys | new_schema.mappings.keys).map do |type|
    diff = Diffy::Diff.new(
      JSON.pretty_generate(schema.mappings[type] || {}) + "\n",
      JSON.pretty_generate(new_schema.mappings[type] || {}) + "\n",
      context: 3
    )
    [type, diff]
  end.to_h
end

#removed_settingsObject



50
51
52
# File 'lib/cauchy/migration.rb', line 50

def removed_settings
  schema.settings.keys - new_schema.settings.keys
end

#requires_reindex?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/cauchy/migration.rb', line 54

def requires_reindex?
  changes_creation_settings? || changes_existing_mappings?
end

#settings_diffObject



73
74
75
76
77
78
79
# File 'lib/cauchy/migration.rb', line 73

def settings_diff
  Diffy::Diff.new(
    JSON.pretty_generate(schema.settings.except(*removed_settings)) + "\n",
    JSON.pretty_generate(new_schema.settings.except(*removed_settings)) + "\n",
    context: 3
  )
end

#up_to_date?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cauchy/migration.rb', line 58

def up_to_date?
  !(changes_settings? || changes_mappings?)
end