Module: Glossarist::SchemaMigration::V2ToV3

Defined in:
lib/glossarist/schema_migration/v2_to_v3.rb

Class Method Summary collapse

Class Method Details

.concept_version(concept) ⇒ Object



33
34
35
36
37
38
# File 'lib/glossarist/schema_migration/v2_to_v3.rb', line 33

def self.concept_version(concept)
  version = concept.schema_version
  return version.to_s if version && !version.to_s.empty?

  ManagedConcept.detect_schema_version(concept)
end

.migrate_concept(concept, target_version: Glossarist::SCHEMA_VERSION) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/glossarist/schema_migration/v2_to_v3.rb', line 6

def self.migrate_concept(concept, target_version: Glossarist::SCHEMA_VERSION)
  current = concept_version(concept)
  target = target_version.to_s

  return concept if current == target

  max_steps = 5
  max_steps.times do
    break if current == target

    case current
    when "2" then current = step_v2_to_v3(concept)
    else
      raise Errors::Base,
            "No concept migration step from version #{current}"
    end
  end

  unless current == target
    raise Errors::Base,
          "Migration chain too long or unresolvable"
  end

  concept.schema_version = target
  concept
end

.step_v2_to_v3(concept) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/glossarist/schema_migration/v2_to_v3.rb', line 40

def self.step_v2_to_v3(concept)
  if concept.data&.related&.any?
    concept.related ||= []
    concept.related = (concept.related + concept.data.related).uniq
    concept.data.related = []
  end
  "3"
end