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
326
|
# File 'lib/brut/cli/apps/db.rb', line 326
def args_description = "migration_name"
|
#bootstrap? ⇒ Boolean
327
|
# File 'lib/brut/cli/apps/db.rb', line 327
def bootstrap? = false
|
#default_rack_env ⇒ Object
328
|
# File 'lib/brut/cli/apps/db.rb', line 328
def default_rack_env = "development"
|
#description ⇒ Object
322
|
# File 'lib/brut/cli/apps/db.rb', line 322
def description = "Create a new migration file"
|
#opts ⇒ Object
323
324
325
|
# File 'lib/brut/cli/apps/db.rb', line 323
def opts = [
[ "--dry-run", "If true, only show what would happen, don't make any files" ],
]
|
#run ⇒ Object
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
363
364
|
# File 'lib/brut/cli/apps/db.rb', line 330
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
|