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, #print, #puts, #stdin, #system!, #theme

Instance Method Details

#args_descriptionObject



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

def args_description = "migration_name"

#bootstrap?Boolean

Returns:

  • (Boolean)


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

def bootstrap? = false

#default_rack_envObject



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

def default_rack_env = "development"

#descriptionObject



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

def description = "Create a new migration file"

#optsObject



333
334
335
# File 'lib/brut/cli/apps/db.rb', line 333

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

#runObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/brut/cli/apps/db.rb', line 340

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