class Charrington::AlterRedshiftTable

Constants

AlterFailed
Error

Attributes

column_types[RW]
columns[R]
connection[R]
event[R]
schema[R]
table_name[R]

Public Class Methods

new(connection, event, schema, table_name, columns) click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 16
def initialize(connection, event, schema, table_name, columns)
  @connection = connection
  @event = event.to_hash
  @schema = schema
  @table_name = table_name
  @columns = columns
  @column_types = []
end

Public Instance Methods

call() click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 25
def call
  set_column_types
  alter_table
  true
rescue => e
  raise AlterFailed, e.message
ensure
  @column_types.clear if @column_types.is_a? Array
end

Private Instance Methods

alter_table() click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 37
def alter_table
  execute_list(get_list_of_alter_table_stmts)
end
current_table_columns() click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 70
def current_table_columns
  sql = "SELECT * FROM #{schema}#{table_name} LIMIT 1;"
  stmt, rs = executeQuery(prep_sql(sql))
  meta_data = rs.getMetaData()
  column_count = meta_data.getColumnCount()
  (1..column_count).map {|i| meta_data.getColumnName(i) }
ensure
  self.logger.info "Within ensure block of current_table_columns in alter_redshift_table.rb and value of stmt.nil?: #{stmt.nil?}"
  stmt.close unless stmt.nil?
end
execute(sql) click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 90
def execute(sql)
  stmt = connection.prepareStatement(prep_sql(sql))
  stmt.execute()
rescue Java::JavaSql::SQLException => e
  self.logger.error "Alter Redshift SQLException: #{e.message}, with SQL: #{sql}"
rescue => e
  self.logger.error "Alter Redshift Unknown exception: #{e.message}, with SQL: #{sql}"
ensure
  self.logger.error "Within ensure block of execute in alter_redshift_table.rb and value of stmt.nil?: #{stmt.nil?}"
  stmt.close unless stmt.nil?
end
executeQuery(sql) click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 102
def executeQuery(sql)
  stmt = connection.createStatement()
  # only close the statement if something goes wrong
  # otherwise, the caller is responsible for closing the
  # statement when they are doen with the result set
  return stmt, stmt.executeQuery(prep_sql(sql))
rescue Java::JavaSql::SQLException => e
  puts "execute query SQLException: #{e.message}"
  self.logger.info "execute query SQLException: #{e.message}, with SQL: #{sql}"
  stmt.close unless stmt.nil?
  # @logger.error("#{e.message}")
rescue => e
  puts "execute query Unknown exception: #{e.message}"
  self.logger.info "execute query Unknown exception: #{e.message}, with SQL: #{sql}"
  stmt.close unless stmt.nil?
end
execute_list(list_of_sql_stmts) click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 81
def execute_list(list_of_sql_stmts)
  self.logger.info "Received list of sql statments to execute: #{list_of_sql_stmts}"

  list_of_sql_stmts.each_with_index do |sql, idx|
    self.logger.info "Executing ALTER TABLE statement with index #{idx} and sql of: #{sql}"
    execute(sql)
  end
end
get_list_of_alter_table_stmts() click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 41
def get_list_of_alter_table_stmts
  column_types.map do |column|
    "ALTER TABLE #{schema}#{table_name} ADD COLUMN #{column}"
  end
end
prep_sql(sql) click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 119
def prep_sql(sql)
  sql.gsub(/\s+/, " ").strip
end
set_column_types() click to toggle source
# File lib/logstash/outputs/charrington/alter_redshift_table.rb, line 47
def set_column_types
  (columns - current_table_columns).each_with_index do |key, idx|
    self.logger.info "New column: #{key}, because of event: #{event}"

    case event[key]
    when Time, LogStash::Timestamp
      column_types << "#{key} TIMESTAMP"
    when Date
      column_types << "#{key} DATE"
    when Integer
      column_types << "#{key} BIGINT"
    when BigDecimal
      column_types << "#{key} DECIMAL"
    when Float
      column_types << "#{key} DOUBLE PRECISION"
    when true, false
      column_types << "#{key} BOOLEAN"
    else
      column_types << "#{key} VARCHAR(512)"
    end
  end
end