Class: Hanami::CLI::Commands::App::DB::Rollback Private

Inherits:
Command show all
Defined in:
lib/hanami/cli/commands/app/db/rollback.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

API:

  • private

Instance Attribute Summary

Attributes inherited from Command

#system_call, #test_env_executor

Instance Method Summary collapse

Methods inherited from Command

#initialize, #nested_command?, #run_command

Methods inherited from Command

#app, #inflector, inherited, #measure, #run_command

Methods inherited from Hanami::CLI::Command

#inflector, #initialize, new

Constructor Details

This class inherits a constructor from Hanami::CLI::Commands::App::DB::Command

Instance Method Details

#call(steps: nil, app: false, slice: nil, gateway: nil, target: nil, dump: true, command_exit: method(:exit)) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

API:

  • private



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hanami/cli/commands/app/db/rollback.rb', line 19

def call(
  steps: nil,
  app: false,
  slice: nil,
  gateway: nil,
  target: nil,
  dump: true,
  command_exit: method(:exit),
  **
)
  # We allow either a number of steps or a target migration number to be provided
  # If steps is provided and target is not, we use steps as the target migration number, but we also have to
  # make sure steps is a number, hence some additional logic around checking and converting to number
  target = steps if steps && !target && !code_is_number?(steps)
  steps_count = steps && code_is_number?(steps) ? Integer(steps) : 1

  database = resolve_target_database(app: app, slice: slice, gateway: gateway, command_exit: command_exit)
  return unless database

  migration_code, migration_name = find_migration_target(target, steps_count, database)

  if migration_name.nil?
    output = if steps && code_is_number?(steps)
               "==> migration file for #{steps} steps back was not found"
             elsif target
               "==> migration file for target #{target} was not found"
             else
               "==> no migrations to rollback"
             end

    out.puts output
    return
  end

  measure "database #{database.name} rolled back to #{migration_name}" do
    database.run_migrations(target: Integer(migration_code))
    true
  end

  if dump && !re_running_in_test?
    run_command(
      Structure::Dump,
      app: database.slice == self.app,
      slice: database.slice == self.app ? nil : database.slice.slice_name.to_s,
      gateway: database.gateway_name == :default ? nil : database.gateway_name.to_s,
      command_exit: command_exit
    )
  end

  re_run_development_command_in_test
end