Skip to content

Commit a29e2ad

Browse files
authored
Merge pull request #2 from radondb/master-import-override-param
import add override param
2 parents a391a4b + 358d376 commit a29e2ad

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ aws_s3.table_import_from_s3 (
8181
credentials aws_commons._aws_credentials_1,
8282
endpoint_url text default null,
8383
read_timeout integer default 60,
84+
override boolean default false,
8485
tempfile_dir text default '/var/lib/postgresql/data/'
8586
)
8687
```
@@ -96,6 +97,7 @@ s3_info | An aws_commons._s3_uri_1 composite type containing the bucket, file pa
9697
credentials | An aws_commons._aws_credentials_1 composite type containing the access key, secret key, session token credentials. To omit a value, set it `null`.
9798
endpoint_url | optional endpoint to use (e.g., `http://localhost:4566`)
9899
read_timeout | The time in seconds till a timeout exception is thrown when attempting to read from a connection.The default is 60 seconds.
100+
override | Whether to overwrite data. If true, will truncate table.
99101
tempfile_dir | Specify temporary file location
100102

101103
##### Example
@@ -175,6 +177,7 @@ aws_s3.table_import_from_s3 (
175177
session_token text,
176178
endpoint_url text default null,
177179
read_timeout integer default 60,
180+
override boolean default false,
178181
tempfile_dir text default '/var/lib/postgresql/data/'
179182
)
180183
```
@@ -192,6 +195,7 @@ secret_key | aws secret key
192195
session_token | optional session token
193196
endpoint_url | optional endpoint to use (e.g., `http://localhost:4566`)
194197
read_timeout | The time in seconds till a timeout exception is thrown when attempting to read from a connection.The default is 60 seconds.
198+
override | Whether to overwrite data. If true, will truncate table.
195199
tempfile_dir | Specify temporary file location
196200

197201
##### Example

aws_s3--1.0.0.sql

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CREATE OR REPLACE FUNCTION aws_s3.table_import_from_s3 (
5050
session_token text default null,
5151
endpoint_url text default null,
5252
read_timeout integer default 60,
53+
override boolean default false,
5354
tempfile_dir text default '/var/lib/postgresql/data/'
5455
) RETURNS int
5556
LANGUAGE plpython3u
@@ -91,6 +92,9 @@ AS $$
9192
**aws_settings
9293
)
9394

95+
if override:
96+
plpy.execute("TRUNCATE TABLE {table_name} RESTRICT;".format(table_name=table_name))
97+
9498
formatted_column_list = "({column_list})".format(column_list=column_list) if column_list else ''
9599
num_rows = 0
96100

@@ -146,14 +150,15 @@ CREATE OR REPLACE FUNCTION aws_s3.table_import_from_s3(
146150
credentials aws_commons._aws_credentials_1,
147151
endpoint_url text default null,
148152
read_timeout integer default 60,
153+
override boolean default false,
149154
tempfile_dir text default '/var/lib/postgresql/data/'
150155
) RETURNS INT
151156
LANGUAGE plpython3u
152157
AS $$
153158

154159
plan = plpy.prepare(
155-
'SELECT aws_s3.table_import_from_s3($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) AS num_rows',
156-
['TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'INTEGER', 'TEXT']
160+
'SELECT aws_s3.table_import_from_s3($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) AS num_rows',
161+
['TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'TEXT', 'INTEGER', 'BOOLEAN', 'TEXT']
157162
)
158163
return plan.execute(
159164
[
@@ -167,7 +172,9 @@ AS $$
167172
credentials['secret_key'],
168173
credentials['session_token'],
169174
endpoint_url,
170-
read_timeout
175+
read_timeout,
176+
override,
177+
tempfile_dir
171178
]
172179
)[0]['num_rows']
173180
$$;
@@ -306,7 +313,9 @@ AS $$
306313
credentials.get('session_token') if credentials else None,
307314
options,
308315
endpoint_url,
309-
read_timeout
316+
read_timeout,
317+
override,
318+
tempfile_dir
310319
]
311320
)
312321
$$;

0 commit comments

Comments
 (0)