Module: Spree::Upgrade

Defined in:
lib/spree/upgrade.rb

Overview

Reads the upgrade manifests shipped inside spree_core — the list of data steps an installation must run after db:migrate for a given release boundary (docs/plans/6.0-maintenance-tasks.md).

Extracted from lib/tasks/upgrade.rake so the dashboard and the maintenance task runner can read the same manifests the rake walk does; the rake file keeps the walking and printing.

Constant Summary collapse

FIRST_TRACKED_RELEASE =

The first release whose upgrade steps run as maintenance tasks. Nothing recorded the steps of earlier releases, so the dashboard starts here; the command line still walks every manifest.

'6.0'.freeze

Class Method Summary collapse

Class Method Details

.available?(step) ⇒ Boolean

Whether a step can run here. An optional step belongs to a gem that may not be installed — a payment gateway, for instance — and is skipped where it is absent rather than offered as work that cannot start.

Parameters:

  • step (Hash) —

    a manifest step

Returns:

  • (Boolean)


144
145
146
147
148
149
# File 'lib/spree/upgrade.rb', line 144

def self.available?(step)
  return true unless step['optional']
  return Spree::MaintenanceTask.find_registered(step['task_class']).present? if step['task_class']

  Spree::MaintenanceTask.new.rake_file_defining(step['task']).present?
end

.available_manifests ⇒ Array<Hash>

All available manifest directories, parsed into { from:, to:, dir: }. Sorted by to ascending, with from as a tiebreaker (smallest first) for the rare case where two manifests share a to boundary.

Returns:

  • (Array<Hash>)


35
36
37
38
39
40
41
42
43
# File 'lib/spree/upgrade.rb', line 35

def self.available_manifests
  Dir.glob(File.join(manifests_root, '*_to_*')).filter_map do |dir|
    name = File.basename(dir)
    match = name.match(/\A([\d_]+)_to_([\d_]+)\z/)
    next unless match

    { from: match[1].tr('_', '.'), to: match[2].tr('_', '.'), dir: dir }
  end.sort_by { |manifest| version_parts(manifest[:to]) + version_parts(manifest[:from]) }
end

.compare(first, second) ⇒ Object

Compare two dotted-version strings (returns -1, 0, +1).



185
186
187
# File 'lib/spree/upgrade.rb', line 185

def self.compare(first, second)
  version_parts(first) <=> version_parts(second)
end

.completed_boundary ⇒ String?

The boundary this installation has already completed, and therefore the point a pending list starts from.

Falls back to the release before the installed one when nothing has been stamped: an installation running 6.0 has, by far most often, come from 5.6, and showing every step of every manifest ever written is worse than showing the one boundary that is almost certainly right. A store that skipped releases runs the earlier steps from the command line, which walks every manifest whatever the boundary.

Returns:

  • (String, nil)


113
114
115
# File 'lib/spree/upgrade.rb', line 113

def self.completed_boundary
  Spree::UpgradeRecord.current_version || previous_boundary
end

.find_step(step_id) ⇒ Hash?

Parameters:

  • step_id (String)

Returns:

  • (Hash, nil)


96
97
98
99
100
# File 'lib/spree/upgrade.rb', line 96

def self.find_step(step_id)
  return nil if step_id.blank?

  steps.find { |step| step['id'] == step_id.to_s }
end

.installed_minor_version ⇒ String

Two-segment "5.5" form of the installed Spree version.

Returns:

  • (String)


18
19
20
# File 'lib/spree/upgrade.rb', line 18

def self.installed_minor_version
  Spree.version.split('.').first(2).join('.')
end

.manifests ⇒ Array<Hash>

Every manifest with its steps loaded, in walk order. Read once per process: the files ship inside the gem and change only with a deploy.

Returns:

  • (Array<Hash>)


49
50
51
52
53
# File 'lib/spree/upgrade.rb', line 49

def self.manifests
  @manifests ||= available_manifests.map do |entry|
    YAML.safe_load_file(File.join(entry[:dir], 'manifest.yml')).freeze
  end.freeze
end

.manifests_root ⇒ String

Root directory containing N_M_to_O_P/manifest.yml files inside the spree_core gem.

Returns:

  • (String)


26
27
28
# File 'lib/spree/upgrade.rb', line 26

def self.manifests_root
  File.expand_path('upgrades', __dir__)
end

.pending_steps ⇒ Array<Hash>

Steps this installation still has to run: every manifest above the boundary it has completed, up to and including the installed version.

Returns:

  • (Array<Hash>)


155
156
157
158
159
160
161
162
163
164
165
# File 'lib/spree/upgrade.rb', line 155

def self.pending_steps
  boundary = completed_boundary

  steps.select do |step|
    next false if compare(step['to'], installed_minor_version) > 0
    next false unless available?(step)
    next true if boundary.nil?

    compare(step['from'], boundary) >= 0
  end
end

.previous_boundary ⇒ String?

The to of the manifest immediately below the installed version.

Returns:

  • (String, nil)


120
121
122
123
124
# File 'lib/spree/upgrade.rb', line 120

def self.previous_boundary
  boundaries = manifests.map { |manifest| manifest['from'] }.uniq
  boundaries.select { |version| compare(version, installed_minor_version) < 0 }.
    max_by { |version| version_parts(version) }
end

.relevant? ⇒ Boolean

Whether an upgrade applies to this installation at all.

False for a store installed fresh at its current version: it has no historical data to convert, so neither the outstanding steps nor the history of past releases describes anything real for it. True again once a later release ships steps it has not run.

Returns:

  • (Boolean)


134
135
136
# File 'lib/spree/upgrade.rb', line 134

def self.relevant?
  !Spree::UpgradeRecord.exists? || Spree::UpgradeRecord.upgraded? || pending_steps.any?
end

.steps ⇒ Array<Hash>

Every step of every manifest, in walk order, each carrying the manifest boundary it belongs to so callers can group without re-reading the files.

Returns:

  • (Array<Hash>)


59
60
61
62
63
64
65
# File 'lib/spree/upgrade.rb', line 59

def self.steps
  @steps ||= manifests.flat_map do |manifest|
    manifest['steps'].map do |step|
      step.merge('from' => manifest['from'], 'to' => manifest['to'], 'docs' => manifest['docs'])
    end
  end.freeze
end

.superseded_steps ⇒ Array<Hash>

Steps of manifests below the completed boundary — the ones a fresh or already-upgraded installation does not need. When the boundary was assumed rather than recorded the dashboard says so, and points a store that skipped a release at the command line, which still runs them.

Returns:

  • (Array<Hash>)


173
174
175
176
177
178
# File 'lib/spree/upgrade.rb', line 173

def self.superseded_steps
  boundary = completed_boundary
  return [] if boundary.nil?

  steps.select { |step| compare(step['from'], boundary) < 0 }
end

.task_arguments(step) ⇒ Hash

The arguments that start a step's task: a rake-backed step is named by its id, so the shared runner knows which rake task to invoke.

Parameters:

  • step (Hash) —

    a manifest step

Returns:

  • (Hash)


90
91
92
# File 'lib/spree/upgrade.rb', line 90

def self.task_arguments(step)
  step['task_class'] ? {} : { 'step_id' => step['id'] }
end

.task_class_name(step) ⇒ String

The task a step runs: its own class, or the shared runner for a step that names a rake task.

Parameters:

  • step (Hash) —

    a manifest step

Returns:

  • (String) —

    a task class name



81
82
83
# File 'lib/spree/upgrade.rb', line 81

def self.task_class_name(step)
  step['task_class'] || 'Spree::MaintenanceTasks::UpgradeStep'
end

.tracked?(step) ⇒ Boolean

Whether a step belongs to a release whose runs are recorded, and so has a place in the dashboard's checklist.

Parameters:

  • step (Hash) —

    a manifest step

Returns:

  • (Boolean)


72
73
74
# File 'lib/spree/upgrade.rb', line 72

def self.tracked?(step)
  compare(step['to'], FIRST_TRACKED_RELEASE) >= 0
end

.version_parts(version) ⇒ Object



180
181
182
# File 'lib/spree/upgrade.rb', line 180

def self.version_parts(version)
  version.split('.').map { |segment| Integer(segment, 10) rescue 0 }
end