Class: Migr8::Actions::NewAction

Inherits:
Action
  • Object
show all
Defined in:
lib/migr8.rb

Constant Summary collapse

NAME =
"new"
DESC =
"create new migration file and open it by $MIGR8_EDITOR"
OPTS =
[
  "-m text  : description message (mandatory)",
  "-u user  : author name (default: current user)",
  "-v version : specify version number instead of random string",
  "-p       : plain skeleton",
  "-e editor: editr command (such as 'emacsclient', 'open', ...)",
  "--table=table       : skeleton to create table",
  "--column=tbl.column : skeleton to add column",
  "--index=tbl.column  : skeleton to create index",
  "--unique=tbl.column : skeleton to add unique constraint",
]
ARGS =
nil

Instance Method Summary collapse

Methods inherited from Action

#cmdopterr, find_by_name, #get_command, inherited, #parse, #parser, #repository, #short_usage, subclasses, #usage

Instance Method Details

#run(options, args) ⇒ Object



1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
# File 'lib/migr8.rb', line 1554

def run(options, args)
  editor = options['e'] || ENV['MIGR8_EDITOR']
  if ! editor || editor.empty?
    _recommend_to_set_MIGR8_EDITOR('create')
    raise cmdopterr("#{NAME}: failed to create migration file.")
  end
  author = options['u']
  version = options['v']
  opts = {}
  opts[:plain] = true if options['p']
  desc = nil
  tblcol_rexp = /\A(\w+)(?:\.(\w+)|\((\w+)\))\z/
  if (val = options['table'])
    val =~ /\A(\w+)\z/  or
      raise cmdopterr("#{NAME} --table=#{val}: unexpected format.")
    desc = "create '#{$1}' table"
    opts[:table] = val
  end
  if (val = options['column'])
    val =~ tblcol_rexp  or
      raise cmdopterr("#{NAME} --column=#{val}: unexpected format.")
    desc = "add '#{$2||$3}' column on '#{$1}' table"
    opts[:column] = val
  end
  if (val = options['index'])
    val =~ tblcol_rexp  or
      raise cmdopterr("#{NAME} --index=#{val}: unexpected format.")
    desc = "create index on '#{$1}.#{$2||$3}'"
    opts[:index] = val
  end
  if (val = options['unique'])
    val =~ tblcol_rexp  or
      raise cmdopterr("#{NAME} --unique=#{val}: unexpected format.")
    desc = "add unique constraint to '#{$1}.#{$2||$3}'"
    opts[:unique] = val
  end
  desc = options['m'] if options['m']
  desc  or
    raise cmdopterr("#{NAME}: '-m text' option required.")
  #
  op = RepositoryOperation.new(repository())
  mig = _wrap { op.new(version, author, desc, opts) }
  puts "## New migration file:"
  puts mig.filepath
  puts "$ #{editor} #{mig.filepath}"
  system("#{editor} #{mig.filepath}")
end