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

Instance Method Details

#args_descriptionObject



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

def args_description = "migration_name"

#bootstrap?Boolean

Returns:

  • (Boolean)


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

def bootstrap? = false

#default_rack_envObject



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

def default_rack_env = "development"

#descriptionObject



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

def description = "Create a new migration file"

#optsObject



321
322
323
# File 'lib/brut/cli/apps/db.rb', line 321

def opts = [
  [ "--dry-run", "If true, only show what would happen, don't make any files" ],
]

#runObject



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/brut/cli/apps/db.rb', line 328

def run
  if argv.length == 0
    puts theme.error.render("You must provide a name for the migration")
    return 1
  end
  if env["RACK_ENV"] != "development"
    puts theme.error.render("This only works in the development environment, not #{theme.code.render(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"
  relative_path = file_name.relative_path_from(Brut.container.project_root)
  puts "Creating new migration file at #{theme.code.render(relative_path.to_s)}"
  info "Creating new migration file at #{file_name}"
  code = %{
Sequel.migration do
  up do
# See https://brutrb.com/recipes/migrations.html
# for a recipe on writing migrations
  end
end
}.strip
  if options.dry_run?
    puts theme.warning.render("Dry run - migration would contain this code:")
    puts theme.code.render(code)
  else
    File.open(file_name,"w") do |file|
      file.puts code
    end
    puts theme.success.render("✅ Migration created")
  end
  0
end