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

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, #bootstrap?, #commands, #default_command, #default_command_class, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#default_rack_envObject



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

def default_rack_env = "development"

#descriptionObject



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

def description = "Apply any outstanding migrations to the database"

#runObject



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
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/brut/cli/apps/db.rb', line 121

def run
  migrations_dir = Brut.container.migrations_dir
  if !migrations_dir.exist?
    stdout.puts "No migrations to run from #{migrations_dir}"
    return 0
  elsif Dir[migrations_dir / "*.rb"].empty?
    stdout.puts "No migrations to run from #{migrations_dir}"
    return 0
  end

  Sequel.extension :migration
  Brut.container.sequel_db_handle.extension :brut_migrations
  Brut.container.sequel_db_handle.extension :pg_array

  logger = Logger.new(STDOUT)
  logger.level = self.options.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
  Sequel::Migrator.run(Brut.container.sequel_db_handle,migrations_dir)
  stdout.puts "All migrations have been applied"
  0
rescue Sequel::DatabaseConnectionError => ex
  stderr.puts "Database #{Brut.container.database_url} does not exist. Create it first with `brut db create`"
  1
rescue Sequel::DatabaseError => ex
  #if ex.cause.kind_of?(PG::UndefinedTable)
  #  # ignoring - we are running migrations which will address this
  #  0
  #else
    raise ex
  #end
end