-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
print Redshift loading error #23
Open
jhuangtw
wants to merge
4
commits into
toothrot:master
Choose a base branch
from
jhuangtw:stl-load-error
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
require "postgres_to_redshift/version" | ||
require 'postgres_to_redshift/version' | ||
require 'pg' | ||
require 'uri' | ||
require 'aws-sdk-v1' | ||
require 'zlib' | ||
require 'tempfile' | ||
require "postgres_to_redshift/table" | ||
require "postgres_to_redshift/column" | ||
require 'postgres_to_redshift/table' | ||
require 'postgres_to_redshift/column' | ||
|
||
class PostgresToRedshift | ||
class << self | ||
|
@@ -41,7 +41,7 @@ def self.target_uri | |
def self.source_connection | ||
unless instance_variable_defined?(:"@source_connection") | ||
@source_connection = PG::Connection.new(host: source_uri.host, port: source_uri.port, user: source_uri.user || ENV['USER'], password: source_uri.password, dbname: source_uri.path[1..-1]) | ||
@source_connection.exec("SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;") | ||
@source_connection.exec('SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;') | ||
end | ||
|
||
@source_connection | ||
|
@@ -85,7 +85,7 @@ def bucket | |
end | ||
|
||
def copy_table(table) | ||
tmpfile = Tempfile.new("psql2rs") | ||
tmpfile = Tempfile.new('psql2rs') | ||
zip = Zlib::GzipWriter.new(tmpfile) | ||
chunksize = 5 * GIGABYTE # uncompressed | ||
chunk = 1 | ||
|
@@ -97,14 +97,14 @@ def copy_table(table) | |
source_connection.copy_data(copy_command) do | ||
while row = source_connection.get_copy_data | ||
zip.write(row) | ||
if (zip.pos > chunksize) | ||
if zip.pos > chunksize | ||
zip.finish | ||
tmpfile.rewind | ||
upload_table(table, tmpfile, chunk) | ||
chunk += 1 | ||
zip.close unless zip.closed? | ||
tmpfile.unlink | ||
tmpfile = Tempfile.new("psql2rs") | ||
tmpfile = Tempfile.new('psql2rs') | ||
zip = Zlib::GzipWriter.new(tmpfile) | ||
end | ||
end | ||
|
@@ -128,14 +128,36 @@ def import_table(table) | |
puts "Importing #{table.target_table_name}" | ||
target_connection.exec("DROP TABLE IF EXISTS public.#{table.target_table_name}_updating") | ||
|
||
target_connection.exec("BEGIN;") | ||
begin | ||
target_connection.exec('BEGIN;') | ||
|
||
target_connection.exec("ALTER TABLE public.#{target_connection.quote_ident(table.target_table_name)} RENAME TO #{table.target_table_name}_updating") | ||
|
||
target_connection.exec("CREATE TABLE public.#{target_connection.quote_ident(table.target_table_name)} (#{table.columns_for_create})") | ||
|
||
target_connection.exec("COPY public.#{target_connection.quote_ident(table.target_table_name)} FROM 's3://#{ENV['S3_DATABASE_EXPORT_BUCKET']}/export/#{table.target_table_name}.psv.gz' CREDENTIALS 'aws_access_key_id=#{ENV['S3_DATABASE_EXPORT_ID']};aws_secret_access_key=#{ENV['S3_DATABASE_EXPORT_KEY']}' GZIP TRUNCATECOLUMNS ESCAPE DELIMITER as '|';") | ||
|
||
target_connection.exec('COMMIT;') | ||
|
||
target_connection.exec("ALTER TABLE public.#{target_connection.quote_ident(table.target_table_name)} RENAME TO #{table.target_table_name}_updating") | ||
rescue PG::InternalError => e | ||
target_connection.exec('ROLLBACK;') | ||
|
||
target_connection.exec("CREATE TABLE public.#{target_connection.quote_ident(table.target_table_name)} (#{table.columns_for_create})") | ||
print_last_redshift_loading_error if e.message.include?('stl_load_errors') | ||
|
||
target_connection.exec("COPY public.#{target_connection.quote_ident(table.target_table_name)} FROM 's3://#{ENV['S3_DATABASE_EXPORT_BUCKET']}/export/#{table.target_table_name}.psv.gz' CREDENTIALS 'aws_access_key_id=#{ENV['S3_DATABASE_EXPORT_ID']};aws_secret_access_key=#{ENV['S3_DATABASE_EXPORT_KEY']}' GZIP TRUNCATECOLUMNS ESCAPE DELIMITER as '|';") | ||
continue_after_error = | ||
!ENV['IGNORE_LOADING_ERRORS_AND_CONTINUE'].nil? && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe move this logic to a helper method, and change the name to something like |
||
ENV['IGNORE_LOADING_ERRORS_AND_CONTINUE'].downcase == 'true' | ||
|
||
target_connection.exec("COMMIT;") | ||
raise unless continue_after_error | ||
end | ||
end | ||
|
||
def print_last_redshift_loading_error | ||
puts 'ERROR: Last Redshift loading error:' | ||
error_row = target_connection.exec('SELECT * FROM pg_catalog.stl_load_errors ORDER BY starttime DESC LIMIT 1').first | ||
error_row.each do |k, v| | ||
puts "\t#{k}: #{v}" | ||
end | ||
puts | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this will not print any errors that do not match. Will other kinds of errors fail silently?