Class: Brut::CLI::Apps::DB::NewMigration

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

Instance Method Details

#args_descriptionObject



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

def args_description = "migration_name"

#before_executeObject



214
215
216
# File 'lib/brut/cli/apps/db.rb', line 214

def before_execute
  ENV["RACK_ENV"] = "development"
end

#bootstrap?Boolean

Returns:

  • (Boolean)


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

def bootstrap? = false

#descriptionObject



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

def description = "Create a new migration file"

#runObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/brut/cli/apps/db.rb', line 218

def run
  if argv.length == 0
    stderr.puts "You must provide a name for the migration"
    return 1
  end
  if env["RACK_ENV"] != "development"
    stderr.puts "This only works in the development environment, not #{env["RACK_ENV"]}"
    return 1
  end
  migrations_dir = Brut.container.migrations_dir
  name = argv.join(" ").gsub(/[^\w\d\-]/,"-")
  date = DateTime.now.strftime("%Y%m%d%H%M%S")
  file_name = migrations_dir / "#{date}_#{name}.rb"
  File.open(file_name,"w") do |file|
    file.puts "Sequel.migration do"
    file.puts "  up do"
    file.puts "    # See https://brutrb.com/recipes/migrations.html"
    file.puts "    # for a recipe on writing migrations"
    file.puts "  end"
    file.puts "end"
  end
  relative_path = file_name.relative_path_from(Brut.container.project_root)
  stdout.puts "Migration created:\n    #{relative_path}"
  0
end