class Turboquery::Connection

Public Class Methods

after_fork() click to toggle source
# File lib/turboquery/connection.rb, line 45
def self.after_fork
  AR.establish_connection
end

Public Instance Methods

after_fork() click to toggle source
# File lib/turboquery/connection.rb, line 41
def after_fork
  self.class.after_fork
end
config() click to toggle source
# File lib/turboquery/connection.rb, line 33
def config
  connection.instance_variable_get(:@config)
end
copy_s3_to_table(_key, _table) click to toggle source
# File lib/turboquery/connection.rb, line 25
def copy_s3_to_table(_key, _table)
  fail 'Not implemented'
end
copy_table_to_s3(_table) click to toggle source
# File lib/turboquery/connection.rb, line 21
def copy_table_to_s3(_table)
  fail 'Not implemented'
end
dump_table_ddl(table) click to toggle source
# File lib/turboquery/connection.rb, line 15
def dump_table_ddl(table)
  set_env
  schema = `pg_dump -i -s -x -o -O --no-tablespaces --no-security-labels -t #{Shellwords.escape(table)} #{Shellwords.escape(config[:database])}`
  schema.empty? ? '' : schema.match(/CREATE TABLE[^;]+;/)[0]
end
execute(*args, &block) click to toggle source
# File lib/turboquery/connection.rb, line 29
def execute(*args, &block)
  connection.execute(*args, &block)
end
set_env() click to toggle source
# File lib/turboquery/connection.rb, line 37
def set_env
  PsqlEnv.set(config)
end
table_exists?(table = nil) click to toggle source
# File lib/turboquery/connection.rb, line 5
def table_exists?(table = nil)
  return false if table.nil?
  connection.execute("
     SELECT 1 AS exists
     FROM   information_schema.tables
     WHERE  table_schema = 'public'
     AND    table_name = '#{table}';
  ").count == 1
end

Protected Instance Methods

build_manifest(objects) click to toggle source
# File lib/turboquery/connection.rb, line 65
def build_manifest(objects)
  {
    entries: objects.map do |obj|
      {
        url: "s3://#{obj.bucket_name}/#{obj.key}",
        mandatory: true
      }
    end
  }.to_json
end
connection() click to toggle source
# File lib/turboquery/connection.rb, line 51
def connection
  AR.connection
end
copy_file_to_s3(filename, key) click to toggle source
# File lib/turboquery/connection.rb, line 96
def copy_file_to_s3(filename, key)
  object = Turboquery.s3_bucket.object(key)
  object.upload_file(filename)
end
copy_s3_to_tmp(key) click to toggle source
# File lib/turboquery/connection.rb, line 85
def copy_s3_to_tmp(key)
  objects = valid_objects(key)
  file = Tempfile.open('turboquery', Turboquery.tmp_path) do |f|
    objects.each do |object|
      f.write object.get.body.read
    end
    f
  end
  file.path
end
random_key() click to toggle source
# File lib/turboquery/connection.rb, line 55
def random_key
  SecureRandom.hex(10)
end
upload_manifest(key) click to toggle source
# File lib/turboquery/connection.rb, line 76
def upload_manifest(key)
  objects = valid_objects(key)
  file = Tempfile.open('turboquery', Turboquery.tmp_path) do |f|
    f.write(build_manifest(objects))
    f
  end
  copy_file_to_s3(file.path, "#{key}manifest")
end
valid_objects(key) click to toggle source
# File lib/turboquery/connection.rb, line 59
def valid_objects(key)
  Turboquery.s3_bucket.objects.to_a.select do |obj|
    obj.key =~ /^#{key}/ && !(obj.key =~ /manifest$/)
  end
end