Skip to content

Commit 1cf00b4

Browse files
authored
Merge pull request #24 from co-github/master
Support JSONB datatypes and support flexible schema
2 parents 62d68e6 + fea38c5 commit 1cf00b4

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Set your source and target databases, as well as your s3 intermediary.
2727
```bash
2828
export POSTGRES_TO_REDSHIFT_SOURCE_URI='postgres://username:password@host:port/database-name'
2929
export POSTGRES_TO_REDSHIFT_TARGET_URI='postgres://username:password@host:port/database-name'
30+
export POSTGRES_TO_REDSHIFT_TARGET_SCHEMA='testing-data'
3031
export S3_DATABASE_EXPORT_ID='yourid'
3132
export S3_DATABASE_EXPORT_KEY='yourkey'
3233
export S3_DATABASE_EXPORT_BUCKET='some-bucket-to-use'

Diff for: lib/postgres_to_redshift.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def self.update_tables
2222
update_tables = PostgresToRedshift.new
2323

2424
update_tables.tables.each do |table|
25-
target_connection.exec("CREATE TABLE IF NOT EXISTS public.#{target_connection.quote_ident(table.target_table_name)} (#{table.columns_for_create})")
25+
target_connection.exec("CREATE TABLE IF NOT EXISTS #{schema}.#{target_connection.quote_ident(table.target_table_name)} (#{table.columns_for_create})")
2626

2727
update_tables.copy_table(table)
2828

@@ -55,6 +55,10 @@ def self.target_connection
5555
@target_connection
5656
end
5757

58+
def self.schema
59+
ENV.fetch('POSTGRES_TO_REDSHIFT_TARGET_SCHEMA')
60+
end
61+
5862
def source_connection
5963
self.class.source_connection
6064
end
@@ -126,15 +130,17 @@ def upload_table(table, buffer, chunk)
126130

127131
def import_table(table)
128132
puts "Importing #{table.target_table_name}"
129-
target_connection.exec("DROP TABLE IF EXISTS public.#{table.target_table_name}_updating")
133+
schema = self.class.schema
134+
135+
target_connection.exec("DROP TABLE IF EXISTS #{schema}.#{table.target_table_name}_updating")
130136

131137
target_connection.exec("BEGIN;")
132138

133-
target_connection.exec("ALTER TABLE public.#{target_connection.quote_ident(table.target_table_name)} RENAME TO #{table.target_table_name}_updating")
139+
target_connection.exec("ALTER TABLE #{schema}.#{target_connection.quote_ident(table.target_table_name)} RENAME TO #{table.target_table_name}_updating")
134140

135-
target_connection.exec("CREATE TABLE public.#{target_connection.quote_ident(table.target_table_name)} (#{table.columns_for_create})")
141+
target_connection.exec("CREATE TABLE #{schema}.#{target_connection.quote_ident(table.target_table_name)} (#{table.columns_for_create})")
136142

137-
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 '|';")
143+
target_connection.exec("COPY #{schema}.#{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 '|';")
138144

139145
target_connection.exec("COMMIT;")
140146
end

Diff for: lib/postgres_to_redshift/column.rb

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class PostgresToRedshift::Column
4949
CAST_TYPES_FOR_COPY = {
5050
"text" => "CHARACTER VARYING(65535)",
5151
"json" => "CHARACTER VARYING(65535)",
52+
"jsonb" => "CHARACTER VARYING(65535)",
5253
"bytea" => "CHARACTER VARYING(65535)",
5354
"money" => "DECIMAL(19,2)",
5455
"oid" => "CHARACTER VARYING(65535)",

0 commit comments

Comments
 (0)