Class: Brut::CLI::Apps::DB::NewMigration
Instance Attribute Summary
#parent_command
Instance Method Summary
collapse
#accepts, #argv, #commands, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #print, #puts, #stdin, #system!, #theme
Instance Method Details
#args_description ⇒ Object
336
|
# File 'lib/brut/cli/apps/db.rb', line 336
def args_description = "migration_name"
|
#bootstrap? ⇒ Boolean
337
|
# File 'lib/brut/cli/apps/db.rb', line 337
def bootstrap? = false
|
#default_rack_env ⇒ Object
338
|
# File 'lib/brut/cli/apps/db.rb', line 338
def default_rack_env = "development"
|
#description ⇒ Object
332
|
# File 'lib/brut/cli/apps/db.rb', line 332
def description = "Create a new migration file"
|
#opts ⇒ Object
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" ],
]
|
#run ⇒ Object
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
|