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

Inherits:
Command show all
Defined in:
lib/hanami/cli/commands/app/db/prepare.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

Constant Summary

Constants inherited from Command

Command::ACTION_SEPARATOR

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, inherited, #measure, #run_command

Methods inherited from Hanami::CLI::Command

#initialize, new

Constructor Details

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

Instance Method Details

#call(app: false, slice: nil) ⇒ 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



12
13
14
15
16
17
18
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
# File 'lib/hanami/cli/commands/app/db/prepare.rb', line 12

def call(app: false, slice: nil, **)
  command_exit = -> code { throw :command_exited, code }
  command_exit_arg = {command_exit: command_exit}

  # Since any slice may have multiple databases, we need to run the steps below in a
  # particular order to satisfy our ROM/Sequel's migrator, which requires _all_ the
  # databases in a slice to be created before we can use it.
  #
  # So before we do anything else, make sure to create/load every database first.
  databases(app: app, slice: slice).each do |database|
    command_args = {
      **command_exit_arg,
      app: database.slice.app?,
      slice: database.slice,
      gateway: database.gateway_name.to_s
    }

    exit_code = catch :command_exited do
      unless database.exists?
        run_command(DB::Create, **command_args)
        run_command(DB::Structure::Load, **command_args)
      end

      nil
    end

    return exit exit_code if exit_code.to_i > 1
  end

  # Once all databases are created, the migrator will properly load for each slice, and
  # we can migrate each database.
  databases(app: app, slice: slice).each do |database|
    command_args = {
      **command_exit_arg,
      app: database.slice.app?,
      slice: database.slice,
      gateway: database.gateway_name.to_s
    }

    exit_code = catch :command_exited do
      run_command(DB::Migrate, **command_args)

      nil
    end

    return exit exit_code if exit_code.to_i > 1
  end

  # Finally, load the seeds for the slice overall, which is a once-per-slice operation.
  run_command(DB::Seed, app: app, slice: slice) unless re_running_in_test?

  re_run_development_command_in_test
end