Class: Glossarist::SchemaMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/glossarist/schema_migration.rb

Constant Summary collapse

CURRENT_SCHEMA_VERSION =
"1"
ENTRY_STATUS_MAP =
{
  "Standard" => "valid",
  "Confirmed" => "valid",
  "Proposed" => "draft",
}.freeze
LANG_CODES =
Glossarist::LANG_CODES
IEV_PATTERN =
/\{\{([^,}]+),\s*IEV:([^}]+)\}\}/.freeze
URN_PATTERN =
/\{urn:iso:std:iso:(\d+):([^,}]+),([^}]+)\}/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concept_hash, from_version: "0", to_version: CURRENT_SCHEMA_VERSION, ref_maps: {}) ⇒ SchemaMigration

Returns a new instance of SchemaMigration.



61
62
63
64
65
66
67
# File 'lib/glossarist/schema_migration.rb', line 61

def initialize(concept_hash, from_version: "0",
                         to_version: CURRENT_SCHEMA_VERSION, ref_maps: {})
  @concept = concept_hash
  @from_version = from_version
  @to_version = to_version
  @ref_maps = ref_maps
end

Instance Attribute Details

#from_versionObject (readonly)

Returns the value of attribute from_version.



59
60
61
# File 'lib/glossarist/schema_migration.rb', line 59

def from_version
  @from_version
end

#to_versionObject (readonly)

Returns the value of attribute to_version.



59
60
61
# File 'lib/glossarist/schema_migration.rb', line 59

def to_version
  @to_version
end

Class Method Details

.concept_version(concept) ⇒ Object



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

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

Raises:



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

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 Error, "No concept migration step from version #{current}"
    end
  end

  raise Error, "Migration chain too long or unresolvable" unless current == target

  concept.schema_version = target
  concept
end

.step_v2_to_v3(concept) ⇒ Object



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

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

.upgrade_directory(source_dir, output:, target_version: CURRENT_SCHEMA_VERSION, cross_references: nil, dry_run: false) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/ParameterLists



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/glossarist/schema_migration.rb', line 78

def self.upgrade_directory(source_dir, output:, # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
                          target_version: CURRENT_SCHEMA_VERSION,
                          cross_references: nil,
                          dry_run: false)
  source_dir = File.expand_path(source_dir)

  concepts_dir = find_concepts_dir(source_dir)
  unless File.directory?(source_dir)
    raise ArgumentError,
          "#{source_dir} is not a directory"
  end
  unless concepts_dir
    raise ArgumentError,
          "No concept YAML files found in #{source_dir}"
  end

  source_version = detect_schema_version(source_dir)
  ref_maps = load_ref_maps(cross_references)
  concepts = read_and_migrate_concepts(concepts_dir, source_version,
                                       target_version, ref_maps)
  register_data = read_register_yaml(source_dir, target_version)

  write_output(concepts, register_data, output, dry_run)

  {
    concepts: concepts,
    register_data: register_data,
    source_version: source_version,
    target_version: target_version,
    output: File.expand_path(output),
    count: concepts.length,
  }
end

Instance Method Details

#migrateObject



69
70
71
72
73
74
75
76
# File 'lib/glossarist/schema_migration.rb', line 69

def migrate
  case [from_version, to_version]
  when ["0", "1"] then migrate_v0_to_v1
  else
    raise Error, "Unsupported migration: #{from_version} -> #{to_version}"
  end
  @concept
end