Module: Browserctl::Commands::Migrate

Defined in:
lib/browserctl/commands/migrate.rb

Overview

‘browserctl migrate <path> [–to-version N] [–dry-run]` — operator entry point for the Migrations registry. Detects the artifact’s format and version, plans a chain of registered upgraders, and applies them in order (unless ‘–dry-run`).

The registry ships empty in v0.12; this command exists so operators have a stable invocation the moment a real migration lands. On an already-current artifact the command is a no-op and exits 0.

Constant Summary collapse

USAGE =
"Usage: browserctl migrate <path> [--to-version N] [--dry-run]"

Class Method Summary collapse

Class Method Details

.execute(path, target_version:, dry_run:, out:, err:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/browserctl/commands/migrate.rb', line 46

def self.execute(path, target_version:, dry_run:, out:, err:)
  format = detect_format!(path, err: err)
  current = Browserctl::Migrations.detect_version(path, format)
  emit_detected(format, current, path, out)

  return plan_dry_run(format, current, target_version, out) if dry_run

  result = Browserctl::Migrations.run(path, target_version: target_version)
  emit_applied(format, current, result, out)
end

.latest_target(format, current) ⇒ Object



132
133
134
135
# File 'lib/browserctl/commands/migrate.rb', line 132

def self.latest_target(format, current)
  targets = registered_for(format)
  targets.empty? ? current : targets.max
end

.plan_dry_run(format, current, target_version, out) ⇒ Object



92
93
94
95
96
# File 'lib/browserctl/commands/migrate.rb', line 92

def self.plan_dry_run(format, current, target_version, out)
  target = target_version || latest_target(format, current)
  chain  = Browserctl::Migrations.find_path(format: format, from: current, to: target)
  emit_plan(format, current, target, chain, out)
end

.registered_for(format) ⇒ Object



137
138
139
# File 'lib/browserctl/commands/migrate.rb', line 137

def self.registered_for(format)
  Browserctl::Migrations.all.select { |m| m.format == format }.map(&:to_version)
end

.run(args, out: $stdout, err: $stderr) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/browserctl/commands/migrate.rb', line 22

def self.run(args, out: $stdout, err: $stderr)
  abort USAGE if args.empty? || args.include?("-h") || args.include?("--help")
  args = args.dup

  dry_run    = !args.delete("--dry-run").nil?
  target_idx = args.index("--to-version")
  target = if target_idx
             args.delete_at(target_idx)
             Integer(args.delete_at(target_idx))
           end
  path = args.shift
  abort USAGE unless path

  unless File.exist?(path)
    err.puts "Error: file not found: #{path}"
    exit Browserctl::Error::ExitCodes::GENERIC
  end

  execute(path, target_version: target, dry_run: dry_run, out: out, err: err)
rescue Browserctl::ProtocolMismatch => e
  err.puts "Error: #{e.message}"
  exit Browserctl::Error::ExitCodes.for(e.code)
end