Class: BigShift::RedshiftTableSchema
- Inherits:
-
Object
- Object
- BigShift::RedshiftTableSchema
- Defined in:
- lib/bigshift/redshift_table_schema.rb
Defined Under Namespace
Classes: Column
Instance Method Summary collapse
- #columns ⇒ Object
-
#initialize(table_name, redshift_connection) ⇒ RedshiftTableSchema
constructor
A new instance of RedshiftTableSchema.
- #to_big_query ⇒ Object
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
#columns ⇒ Object
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_query ⇒ Object
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 |