Class: Brut::CLI::Apps::DB::Create

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/db.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #args_description, #argv, #commands, #default_command, #default_command_class, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#bootstrap?Boolean

Returns:

  • (Boolean)


61
# File 'lib/brut/cli/apps/db.rb', line 61

def bootstrap? = false

#default_rack_envObject



60
# File 'lib/brut/cli/apps/db.rb', line 60

def default_rack_env = "development"

#descriptionObject



59
# File 'lib/brut/cli/apps/db.rb', line 59

def description = "Create the database if it does not exist"

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/brut/cli/apps/db.rb', line 63

def run
  connection = Sequel.connect(Brut.container.database_url)
  stdout.puts "Database already exists"
  connection.disconnect
  0
rescue Sequel::DatabaseConnectionError => ex
  uri_no_database = URI(Brut.container.database_url.to_s)
  database_name = uri_no_database.path.gsub(/^\//,"")
  uri_no_database.path = ""
  begin
    connection = Sequel.connect(uri_no_database.to_s)
    stdout.puts "Database #{database_name} does not exist. Creating..."
    connection.run("CREATE DATABASE \"#{database_name}\"")
    connection.disconnect
    0
  rescue Sequel::DatabaseConnectionError => ex2
    stderr.puts "Database server is not running at #{uri_no_database}: #{ex2.message}"
    stderr.puts "This could be a problem with your dev environment generally, or your .env.test or .env.test.local files"
    1
  end
end