Class: Bricolage::PSQLDataSource

Inherits:
DataSource show all
Includes:
CommandUtils, VacuumLock
Defined in:
lib/bricolage/psqldatasource.rb

Constant Summary

Constants included from VacuumLock

VacuumLock::DEFAULT_VACUUM_LOCK_FILE, VacuumLock::DEFAULT_VACUUM_LOCK_TIMEOUT

Constants inherited from DataSource

DataSource::CLASSES

Instance Attribute Summary collapse

Attributes inherited from DataSource

#context, #logger, #name

Instance Method Summary collapse

Methods included from VacuumLock

cleanup_vacuum_lock, create_lockfile_cmd, create_vacuum_lock_file, enable_vacuum_lock?, locking?, psql_serialize_vacuum_begin, psql_serialize_vacuum_end, serialize_vacuum, using, #using_vacuum_lock, vacuum_lock_parameters

Methods included from CommandUtils

#command, #make_tmpfile, #new_tmpfile_path, #retrieve_last_match_from_stderr

Methods inherited from DataSource

get_class, new_for_type, #redshift_loader_source?

Constructor Details

#initialize(host: 'localhost', port: 5439, database: 'dev', username: ENV['LOGNAME'], password: nil, pgpass: nil, encoding: nil, psql: 'psql', tmpdir: Dir.tmpdir) ⇒ PSQLDataSource

Returns a new instance of PSQLDataSource.

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bricolage/psqldatasource.rb', line 17

def initialize(
    host: 'localhost',
    port: 5439,
    database: 'dev',
    username: ENV['LOGNAME'],
    password: nil,
    pgpass: nil,
    encoding: nil,
    psql: 'psql',
    tmpdir: Dir.tmpdir)
  @host = host
  @port = port
  @database = database
  @user = username
  @password = password
  @pgpass = pgpass
  @encoding = encoding
  @psql = psql
  @tmpdir = tmpdir
  raise ParameterError, "missing psql host" unless @host
  raise ParameterError, "missing psql port" unless @port
  raise ParameterError, "missing psql database" unless @database
  raise ParameterError, "missing psql username" unless @user
  unless @pgpass or @password
    raise ParameterError, "missing psql password"
  end
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



47
48
49
# File 'lib/bricolage/psqldatasource.rb', line 47

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



45
46
47
# File 'lib/bricolage/psqldatasource.rb', line 45

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



46
47
48
# File 'lib/bricolage/psqldatasource.rb', line 46

def port
  @port
end

#userObject (readonly)

Returns the value of attribute user.



48
49
50
# File 'lib/bricolage/psqldatasource.rb', line 48

def user
  @user
end

Instance Method Details

#analyze(table) ⇒ Object



139
140
141
# File 'lib/bricolage/psqldatasource.rb', line 139

def analyze(table)
  open {|conn| conn.analyze(table) }
end

#drop_table(name) ⇒ Object



113
114
115
# File 'lib/bricolage/psqldatasource.rb', line 113

def drop_table(name)
  open {|conn| conn.drop_table(name) }
end

#drop_table_force(name) ⇒ Object



117
118
119
# File 'lib/bricolage/psqldatasource.rb', line 117

def drop_table_force(name)
  open {|conn| conn.drop_table_force(name) }
end

#execute(source, options = []) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bricolage/psqldatasource.rb', line 59

def execute(source, options = [])
  make_tmpfile(source, tmpdir: @tmpdir) {|path|
    st = command @psql, "--no-psqlrc", "--host=#{@host}", "--port=#{@port}",
        "--username=#{@user}", @database,
        '--echo-all',
        '-v', 'ON_ERROR_STOP=true',
        '-f', path,
        '--no-password',
        *options,
        env: get_psql_env
    msg = retrieve_last_match_from_stderr(/^psql:.*?:\d+: ERROR: (.*)/, 1) unless st.success?
    JobResult.for_process_status(st, msg)
  }
end

#execute_query(query, &block) ⇒ Object



109
110
111
# File 'lib/bricolage/psqldatasource.rb', line 109

def execute_query(query, &block)
  open {|conn| conn.execute_query(query, &block) }
end

#get_psql_envObject



74
75
76
77
78
79
80
81
82
# File 'lib/bricolage/psqldatasource.rb', line 74

def get_psql_env
  env = {}
  if @pgpass
    env["PGPASSFILE"] = @pgpass
  elsif @password
    env["PGPASSWORD"] = @password
  end
  env
end

#new_taskObject



50
51
52
# File 'lib/bricolage/psqldatasource.rb', line 50

def new_task
  PSQLTask.new(self)
end

#open(&block) ⇒ Object



98
99
100
101
102
103
# File 'lib/bricolage/psqldatasource.rb', line 98

def open(&block)
  conn = PG::Connection.open(host: @host, port: @port, dbname: @database, user: @user, password: password)
  yield PostgresConnection.new(conn, self, logger)
ensure
  conn.close if conn
end

#open_for_batchObject



54
55
56
57
# File 'lib/bricolage/psqldatasource.rb', line 54

def open_for_batch
  # do not call #open
  yield
end

#passwordObject

Ruby Library Interface



88
89
90
91
# File 'lib/bricolage/psqldatasource.rb', line 88

def password
  # FIXME: same user must not exist
  @password ||= read_password_from_pgpass(@pgpass, @user)
end

#query(query) ⇒ Object



105
106
107
# File 'lib/bricolage/psqldatasource.rb', line 105

def query(query)
  open {|conn| conn.execute(query) }
end

#read_password_from_pgpass(path, user) ⇒ Object



93
94
95
96
# File 'lib/bricolage/psqldatasource.rb', line 93

def read_password_from_pgpass(path, user)
  File.read(path).slice(/:#{user}:([^:\r\n]+)$/, 1) or
      raise ParameterError, "could not read password: #{path}, #{user}"
end

#select(table, &block) ⇒ Object



121
122
123
# File 'lib/bricolage/psqldatasource.rb', line 121

def select(table, &block)
  open {|conn| conn.select(table, &block) }
end

#vacuum(table) ⇒ Object



127
128
129
130
131
# File 'lib/bricolage/psqldatasource.rb', line 127

def vacuum(table)
  serialize_vacuum {
    open {|conn| conn.vacuum(table) }
  }
end

#vacuum_sort_only(table) ⇒ Object



133
134
135
136
137
# File 'lib/bricolage/psqldatasource.rb', line 133

def vacuum_sort_only(table)
  serialize_vacuum {
    open {|conn| conn.vacuum_sort_only(table) }
  }
end