Class: BigShift::RedshiftTableSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/bigshift/redshift_table_schema.rb

Defined Under Namespace

Classes: Column

Instance Method Summary collapse

Constructor Details

#initialize(table_name, redshift_connection) ⇒ RedshiftTableSchema

Returns a new instance of RedshiftTableSchema.



3
4
5
6
# File 'lib/bigshift/redshift_table_schema.rb', line 3

def initialize(table_name, redshift_connection)
  @table_name = table_name
  @redshift_connection = redshift_connection
end

Instance Method Details

#columnsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bigshift/redshift_table_schema.rb', line 8

def columns
  @columns ||= begin
    rows = @redshift_connection.exec_params(%|SELECT "column", "type", "notnull" FROM "pg_table_def" WHERE "schemaname" = 'public' AND "tablename" = $1|, [@table_name])
    if rows.count == 0
      raise sprintf('Table not found: %s', @table_name.inspect)
    else
      columns = rows.map do |row|
        name = row['column']
        type = row['type']
        nullable = row['notnull'] == 'f'
        Column.new(name, type, nullable)
      end
      columns.sort_by!(&:name)
      columns
    end
  end
end

#to_big_queryObject



26
27
28
# File 'lib/bigshift/redshift_table_schema.rb', line 26

def to_big_query
  Google::Apis::BigqueryV2::TableSchema.new(fields: columns.map(&:to_big_query))
end