Class: Discharger::SetupRunner::Commands::DatabaseCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/discharger/setup_runner/commands/database_command.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#app_root, #config, #logger

Instance Method Summary collapse

Methods inherited from BaseCommand

#initialize

Constructor Details

This class inherits a constructor from Discharger::SetupRunner::Commands::BaseCommand

Instance Method Details

#can_execute?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/discharger/setup_runner/commands/database_command.rb', line 68

def can_execute?
  File.exist?(File.join(app_root, "bin/rails"))
end

#descriptionObject



72
73
74
# File 'lib/discharger/setup_runner/commands/database_command.rb', line 72

def description
  "Setup database"
end

#executeObject



10
11
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
65
66
# File 'lib/discharger/setup_runner/commands/database_command.rb', line 10

def execute
  # Drop and recreate development database
  terminate_database_connections
  with_spinner("Dropping and recreating development database") do
    stdout, stderr, status = Open3.capture3(db_env, "bin/rails", "db:drop", "db:create")
    if status.success?
      {success: true}
    else
      error_msg = stderr.empty? ? stdout : stderr
      {success: false, error: "Failed to drop/create database: #{error_msg}"}
    end
  end

  # Load schema and run migrations
  with_spinner("Loading database schema and running migrations") do
    _stdout, stderr, status = Open3.capture3(db_env, "bin/rails", "db:schema:load", "db:migrate")
    if status.success?
      {success: true}
    else
      {success: false, error: "Failed to load schema: #{stderr}"}
    end
  end

  # Seed the database
  seed_env = db_env.merge((config.respond_to?(:seed_env) && config.seed_env) ? {"SEED_DEV_ENV" => "true"} : {})
  with_spinner("Seeding the database") do
    _stdout, stderr, status = Open3.capture3(seed_env, "bin/rails", "db:seed")
    if status.success?
      {success: true}
    else
      {success: false, error: "Failed to seed database: #{stderr}"}
    end
  end

  # Setup test database
  terminate_database_connections("test")
  with_spinner("Setting up test database") do
    test_env = db_env.merge({"RAILS_ENV" => "test"})
    stdout, stderr, status = Open3.capture3(test_env, "bin/rails", "db:drop", "db:create", "db:schema:load")
    if status.success?
      {success: true}
    else
      error_msg = stderr.empty? ? stdout : stderr
      {success: false, error: "Failed to setup test database: #{error_msg}"}
    end
  end

  # Clear logs and temp files
  with_spinner("Clearing logs and temp files") do
    _stdout, _stderr, status = Open3.capture3("bash", "-c", "bin/rails log:clear tmp:clear > /dev/null 2>&1")
    if status.success?
    else
      # Don't fail for log clearing
    end
    {success: true}
  end
end