Skip to content

Commit

Permalink
Merge pull request #8 from Root-App/pedro-add-reload-from-sql
Browse files Browse the repository at this point in the history
Adds reload from sql method to redshift table
  • Loading branch information
pbcoronel authored Feb 28, 2018
2 parents 700450a + 6e722af commit 94e325e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/amazon/redshift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ def reload_from_bucket(bucket, iam_role, encoding: "UTF8", ignore_header: true)
end
end

def reload_from_sql(sql_string)
count_before = count

@connection.transaction do
@connection[Sequel.qualify(@schema_sym, @table_name_sym)].delete

@connection.run(_sql_insert_into_statement(sql_string))

raise Sequel::Rollback if count < count_before
end
end

def unload_to_bucket(bucket, iam_role)
output_file = _generate_s3_unload_filename(bucket.url)
query = _s3_unload_statement(iam_role, output_file)
Expand Down Expand Up @@ -125,6 +137,11 @@ def _meta_data_query
cols.ordinal_position::int;
QUERY
end

def _sql_insert_into_statement(sql_string)
"INSERT INTO #{@schema}.#{@table_name}
#{sql_string}"
end
end
end
end

0 comments on commit 94e325e

Please sign in to comment.