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:([^}]+)\}\}/
URN_PATTERN =
/\{urn:iso:std:iso:(\d+):([^,}]+),([^}]+)\}/

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.



64
65
66
67
68
69
70
# File 'lib/glossarist/schema_migration.rb', line 64

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.



62
63
64
# File 'lib/glossarist/schema_migration.rb', line 62

def from_version
  @from_version
end

#to_versionObject (readonly)

Returns the value of attribute to_version.



62
63
64
# File 'lib/glossarist/schema_migration.rb', line 62

def to_version
  @to_version
end

Class Method Details

.concept_version(concept) ⇒ Object



35
36
37
38
39
40
# File 'lib/glossarist/schema_migration.rb', line 35

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



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

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

  concept.schema_version = target
  concept
end

.step_v2_to_v3(concept) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/glossarist/schema_migration.rb', line 42

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



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
111
112
113
# File 'lib/glossarist/schema_migration.rb', line 81

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



72
73
74
75
76
77
78
79
# File 'lib/glossarist/schema_migration.rb', line 72

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