Class: Jredshift::Redshift

Inherits:
JdbcDb
  • Object
show all
Defined in:
lib/jredshift.rb

Constant Summary collapse

ERROR_CONNECTION_REFUSED =

I’ve encountered a Redshift bug where the DB breaks all connections and can’t be connected to again for a short time.

'Connection refused. Check that the hostname and ' +
'port are correct and that the postmaster is accepting TCP/IP connections.'
ERROR_IO =

This is seen when the connection is broken.

'An I/O error occurred while sending to the backend'

Constants inherited from JdbcDb

JdbcDb::ERROR_UPDATE_COUNT_OVERFLOW_REGEX

Instance Attribute Summary collapse

Attributes inherited from JdbcDb

#conn

Instance Method Summary collapse

Methods inherited from JdbcDb

#big_query, #clear_error_state, #error_occurred?, #execute, #execute_script, #query

Constructor Details

#initialize(jdbc_url, user, password, options = {}) ⇒ Redshift

Returns a new instance of Redshift.



16
17
18
19
20
21
22
23
# File 'lib/jredshift.rb', line 16

def initialize(jdbc_url, user, password, options={})
  super
  @query_group = options.fetch(:query_group, nil)
  @query_slot_count = options.fetch(:query_slot_count, nil)

  set_query_group(@query_group) if @query_group
  set_query_slot_count(@query_slot_count) if @query_slot_count
end

Instance Attribute Details

#query_groupObject (readonly)

Returns the value of attribute query_group.



6
7
8
# File 'lib/jredshift.rb', line 6

def query_group
  @query_group
end

#query_slot_countObject (readonly)

Returns the value of attribute query_slot_count.



6
7
8
# File 'lib/jredshift.rb', line 6

def query_slot_count
  @query_slot_count
end

Instance Method Details

#drop_table_if_exists(table, options = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/jredshift.rb', line 33

def drop_table_if_exists(table, options={})
  cascade = options.fetch(:cascade, false) ? ' CASCADE' : ''
  err_msg = "ERROR: Table \"#{remove_schema(table)}\" does not exist"

  execute("DROP TABLE #{table}#{cascade};", :quiet => true, :errors_to_ignore => [err_msg])
end

#drop_view_if_exists(view, options = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/jredshift.rb', line 40

def drop_view_if_exists(view, options={})
  cascade = options.fetch(:cascade, false) ? ' CASCADE' : ''
  err_msg = "ERROR: View \"#{remove_schema(view)}\" does not exist"

  execute("DROP VIEW #{view}#{cascade};", :quiet => true, :errors_to_ignore => [err_msg])
end

#set_query_group(query_group) ⇒ Object



25
26
27
# File 'lib/jredshift.rb', line 25

def set_query_group(query_group)
  execute("SET query_group TO #{query_group};")
end

#set_query_slot_count(count) ⇒ Object



29
30
31
# File 'lib/jredshift.rb', line 29

def set_query_slot_count(count)
  execute("SET wlm_query_slot_count TO #{count};")
end

#table_exists?(schema, table) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/jredshift.rb', line 47

def table_exists?(schema, table)
  sql = <<-SQL
    SELECT count(*) FROM pg_tables
    WHERE schemaname = '#{schema}' AND tablename = '#{table}'
    ;
  SQL
  query(sql, :quiet => true).first['count'] == 1
end