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, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stdin, #system!, #theme

Instance Method Details

#bootstrap?Boolean

Returns:

  • (Boolean)


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

def bootstrap? = false

#default_rack_envObject



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

def default_rack_env = "development"

#descriptionObject



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

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

#runObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/brut/cli/apps/db.rb', line 99

def run
  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(Brut.container.database_url)
    puts [
      theme.success.render("✅ Database"),
      theme.code.render(database_name),
      theme.success.render("already exists"),
    ].join(" ")
    connection.disconnect
    0
  rescue Sequel::DatabaseConnectionError => ex
    begin
      connection = Sequel.connect(uri_no_database.to_s)
      puts [
        "Database",
        theme.code.render(database_name),
        "does not exist. Creating...",
      ].join(" ")
      connection.run("CREATE DATABASE \"#{database_name}\"")
      connection.disconnect
      puts [
        theme.success.render("✅ Database"),
        theme.code.render(database_name),
        theme.success.render("created"),
      ].join(" ")
      0
    rescue Sequel::DatabaseConnectionError => ex2
      puts [
        theme.error.render("Database server is not running at"),
        theme.code.render(uri_no_database.to_s),
      ].join(" ")
      puts [
        theme.error.render(ex2.class.name),
        theme.exception.render(ex2.message),
      ].join(": ")

      puts theme.error.render("This could be a problem with your dev environment")
      puts [
        theme.error.render("Check"),
        theme.code.render(".env.test"),
        theme.error.render("and"),
        theme.code.render(".env.test.local"),
        theme.error.render("to see if "),
        theme.code.render("DATABASE_URL"),
        theme.error.render("is set correctly"),
      ].join(" ")
      1
    end
  end
end