Class: Brut::CLI::Apps::DB::Migrate

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/db.rb

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/brut/cli/apps/db.rb', line 166

def execute
  Sequel.extension :migration
  Brut.container.sequel_db_handle.extension :brut_migrations
  migrations_dir = Brut.container.migrations_dir
  if !migrations_dir.exist?
    err.puts "#{migrations_dir} doesn't exist"
    return
  elsif Dir[migrations_dir / "*.rb"].empty?
    out.puts "No migrations yet"
    return
  end
  Brut.container.sequel_db_handle.extension :pg_array

  logger = Logger.new(STDOUT)
  logger.level = ENV["LOG_LEVEL"]
  indent = ""
  logger.formatter = proc { |severity,time,progname,message|
    formatted = "#{indent} - #{message}\n"
    if message =~ /^Begin applying/
      indent = "   "
    elsif message =~ /^Finished applying/
      indent = ""
      formatted = "#{indent} - #{message}\n"
    end
    formatted
  }
  Brut.container.sequel_db_handle.logger = logger
  Brut.container.instrumentation.span("migrations.run") do
    Sequel::Migrator.run(Brut.container.sequel_db_handle,migrations_dir)
  end
  out.puts "Migrations applied"
end

#handle_bootstrap_exception(ex) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/brut/cli/apps/db.rb', line 150

def handle_bootstrap_exception(ex)
  case ex
  when Sequel::DatabaseConnectionError
    abort_execution("Database does not exist. Create it first")
  when Sequel::DatabaseError
    if ex.cause.kind_of?(PG::UndefinedTable)
      # ignoring - we are running migrations which will address this
      continue_execution
    else
      super
    end
  else
    super
  end
end