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, #puts, #stdin, #system!, #theme
Instance Method Details
#args_description ⇒ Object
324
|
# File 'lib/brut/cli/apps/db.rb', line 324
def args_description = "migration_name"
|
#bootstrap? ⇒ Boolean
325
|
# File 'lib/brut/cli/apps/db.rb', line 325
def bootstrap? = false
|
#default_rack_env ⇒ Object
326
|
# File 'lib/brut/cli/apps/db.rb', line 326
def default_rack_env = "development"
|
#description ⇒ Object
320
|
# File 'lib/brut/cli/apps/db.rb', line 320
def description = "Create a new migration file"
|
#opts ⇒ Object
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" ],
]
|
#run ⇒ Object
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
|