class TableCopy::PG::Source

Attributes

conn_method[R]
infer_pk_proc[R]
table_name[R]

Public Class Methods

new(args) click to toggle source
# File lib/table_copy/pg/source.rb, line 9
def initialize(args)
  @table_name    = args[:table_name]
  @conn_method   = args[:conn_method]
  @infer_pk_proc = args[:infer_pk_proc]
end

Public Instance Methods

copy_from(fields_list_arg, where=nil) { |conn| ... } click to toggle source
# File lib/table_copy/pg/source.rb, line 31
def copy_from(fields_list_arg, where=nil)
  with_conn do |conn|
    conn.copy_data("copy (select #{fields_list_arg} from #{table_name} #{where}) to stdout csv")  do
      yield conn
    end
  end
end
fields() click to toggle source
# File lib/table_copy/pg/source.rb, line 39
def fields
  fields_objects.map(&:name)
end
fields_ddl() click to toggle source
# File lib/table_copy/pg/source.rb, line 23
def fields_ddl
  fields_objects.map(&:ddl).join(",\n  ")
end
indexes() click to toggle source
# File lib/table_copy/pg/source.rb, line 27
def indexes
  viable_index_columns.map { |name, columns| TableCopy::PG::Index.new(table_name, name, columns) }
end
primary_key() click to toggle source
# File lib/table_copy/pg/source.rb, line 19
def primary_key
  @primary_key ||= get_primary_key
end
to_s() click to toggle source
# File lib/table_copy/pg/source.rb, line 15
def to_s
  table_name
end

Private Instance Methods

fields_objects() click to toggle source
# File lib/table_copy/pg/source.rb, line 49
def fields_objects
  with_conn do |conn|
    conn.exec(fields_sql).map { |r| TableCopy::PG::Field.new(r) }
  end
end
fields_sql() click to toggle source
# File lib/table_copy/pg/source.rb, line 99
      def fields_sql
        <<-SQL
          SELECT *
          FROM information_schema.columns
          WHERE table_schema='public' AND table_name='#{table_name}'
        SQL
      end
get_primary_key() click to toggle source
# File lib/table_copy/pg/source.rb, line 107
def get_primary_key
  with_conn do |conn|
    rows = conn.exec(primary_key_sql)
    if (row = rows.first) && row['attname']
      row['attname']
    elsif infer_pk_proc
      inferred_pk = infer_pk_proc.call(table_name)
      TableCopy.logger.warn "No explicit PK found for #{table_name}. Falling back to #{inferred_pk}."
      inferred_pk
    else
      TableCopy.logger.warn "No explicit PK found for #{table_name}. Falling back to \"id\"."
      'id'
    end
  end
end
index_columns() click to toggle source
# File lib/table_copy/pg/source.rb, line 61
def index_columns
  raw_indexes.inject({}) do |indexes, ri|
    index_name = ri['index_name']
    indexes[index_name] ||= []
    indexes[index_name] << ri['column_name']
    indexes
  end
end
indexes_sql() click to toggle source
# File lib/table_copy/pg/source.rb, line 76
      def indexes_sql
        <<-SQL
          select
              i.relname as index_name,
              a.attname as column_name
          from
              pg_class t,
              pg_class i,
              pg_index ix,
              pg_attribute a
          where
              t.oid = ix.indrelid
              and i.oid = ix.indexrelid
              and a.attrelid = t.oid
              and a.attnum = ANY(ix.indkey)
              and t.relkind = 'r'
              and t.relname = '#{table_name}'
          order by
              t.relname,
              i.relname;
        SQL
      end
primary_key_sql() click to toggle source
# File lib/table_copy/pg/source.rb, line 123
      def primary_key_sql
        <<-SQL
          SELECT
          pg_attribute.attname,
          format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
        FROM pg_index, pg_class, pg_attribute
        WHERE
          pg_class.oid = '#{table_name}'::regclass AND
          indrelid = pg_class.oid AND
          pg_attribute.attrelid = pg_class.oid AND
          pg_attribute.attnum = any(pg_index.indkey)
          AND indisprimary
        SQL
      end
raw_indexes() click to toggle source
# File lib/table_copy/pg/source.rb, line 70
def raw_indexes
  with_conn do |conn|
    conn.exec(indexes_sql)
  end
end
viable_index_columns() click to toggle source
# File lib/table_copy/pg/source.rb, line 55
def viable_index_columns
  index_columns.select do |name, columns|
    (columns - fields).empty?
  end
end
with_conn(&block) click to toggle source
# File lib/table_copy/pg/source.rb, line 45
def with_conn(&block)
  conn_method.call(&block)
end