Class: Masamune::Commands::Postgres

Inherits:
SimpleDelegator
  • Object
show all
Includes:
PostgresCommon, StringFormat
Defined in:
lib/masamune/commands/postgres.rb

Constant Summary collapse

DEFAULT_ATTRIBUTES =
{
  path: 'psql',
  options: [],
  hostname: 'localhost',
  database: 'postgres',
  username: 'postgres',
  pgpass_file: nil,
  file: nil,
  exec: nil,
  input: nil,
  output: nil,
  print: false,
  block: nil,
  csv: false,
  variables: {},
  tuple_output: false,
  debug: false
}.with_indifferent_access.freeze

Instance Method Summary collapse

Methods included from PostgresCommon

#command_env

Methods included from StringFormat

#strip_sql

Constructor Details

#initialize(delegate, attrs = {}) ⇒ Postgres

Returns a new instance of Postgres.

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
# File 'lib/masamune/commands/postgres.rb', line 53

def initialize(delegate, attrs = {})
  super delegate
  DEFAULT_ATTRIBUTES.merge(configuration.commands.postgres).merge(attrs).each do |name, value|
    instance_variable_set("@#{name}", value)
  end
  raise ArgumentError, 'Cannot specify both file and exec' if @file && @exec
  @error = nil
end

Instance Method Details

#before_executeObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/masamune/commands/postgres.rb', line 90

def before_execute
  console("psql with file #{@file}") if @file
  if @debug && (output = rendered_template || @file)
    logger.debug("#{output}:\n" + File.read(output))
  end

  return unless @exec
  console("postgres exec '#{strip_sql(@exec)}' #{'into ' + @output if @output}")
  @file = exec_file
end

#command_argsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/masamune/commands/postgres.rb', line 74

def command_args
  args = []
  args << @path
  args << "--host=#{@hostname}" if @hostname
  args << "--dbname=#{@database}"
  args << "--username=#{@username}" if @username
  args << '--no-password'
  args << '--set=ON_ERROR_STOP=1'
  args << @options.map(&:to_a)
  args << command_args_for_file if @file
  args << "--output=#{@output}" if @output
  args << '--no-align' << '--field-separator=,' << '--pset=footer' if @csv
  args << '--pset=tuples_only' if @tuple_output
  args.flatten.compact
end

#failure_message(_status) ⇒ Object



115
116
117
# File 'lib/masamune/commands/postgres.rb', line 115

def failure_message(_status)
  @error || 'psql failed without error'
end

#handle_stderr(line, _line_no) ⇒ Object



110
111
112
113
# File 'lib/masamune/commands/postgres.rb', line 110

def handle_stderr(line, _line_no)
  @error = line.split(/ERROR:\s*/).last if line =~ /ERROR:/
  logger.debug(line)
end

#handle_stdout(line, _line_no) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/masamune/commands/postgres.rb', line 101

def handle_stdout(line, _line_no)
  if line =~ /\A#{prompt}/
    logger.debug(line)
  else
    @block.call(line) if @block
    console(line) if print?
  end
end

#interactive?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/masamune/commands/postgres.rb', line 66

def interactive?
  !(@exec || @file)
end

#print?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/masamune/commands/postgres.rb', line 70

def print?
  @print
end

#promptObject



119
120
121
# File 'lib/masamune/commands/postgres.rb', line 119

def prompt
  @database + '=>'
end

#stdinObject



62
63
64
# File 'lib/masamune/commands/postgres.rb', line 62

def stdin
  @stdin ||= StringIO.new(strip_sql(@input)) if @input
end