@@ -277,6 +277,7 @@ def _create_table( # pylint: disable=too-many-locals,too-many-arguments
277
277
use_threads : Union [bool , int ] = True ,
278
278
boto3_session : Optional [boto3 .Session ] = None ,
279
279
s3_additional_kwargs : Optional [Dict [str , str ]] = None ,
280
+ lock : bool = False ,
280
281
) -> Tuple [str , Optional [str ]]:
281
282
if mode == "overwrite" :
282
283
if overwrite_method == "truncate" :
@@ -291,14 +292,21 @@ def _create_table( # pylint: disable=too-many-locals,too-many-arguments
291
292
_logger .debug (str (e ))
292
293
con .rollback ()
293
294
_begin_transaction (cursor = cursor )
295
+ if lock :
296
+ _lock (cursor , [table ], schema = schema )
294
297
elif overwrite_method == "delete" :
295
298
if _does_table_exist (cursor = cursor , schema = schema , table = table ):
299
+ if lock :
300
+ _lock (cursor , [table ], schema = schema )
296
301
# Atomic, but slow.
297
302
_delete_all (cursor = cursor , schema = schema , table = table )
298
303
else :
299
304
# Fast, atomic, but either fails if there are any dependent views or, in cascade mode, deletes them.
300
305
_drop_table (cursor = cursor , schema = schema , table = table , cascade = bool (overwrite_method == "cascade" ))
306
+ # No point in locking here, the oid will change.
301
307
elif _does_table_exist (cursor = cursor , schema = schema , table = table ) is True :
308
+ if lock :
309
+ _lock (cursor , [table ], schema = schema )
302
310
if mode == "upsert" :
303
311
guid : str = uuid .uuid4 ().hex
304
312
temp_table : str = f"temp_redshift_{ guid } "
@@ -363,6 +371,8 @@ def _create_table( # pylint: disable=too-many-locals,too-many-arguments
363
371
)
364
372
_logger .debug ("Create table query:\n %s" , sql )
365
373
cursor .execute (sql )
374
+ if lock :
375
+ _lock (cursor , [table ], schema = schema )
366
376
return table , schema
367
377
368
378
@@ -869,6 +879,7 @@ def to_sql( # pylint: disable=too-many-locals
869
879
primary_keys = primary_keys ,
870
880
varchar_lengths_default = varchar_lengths_default ,
871
881
varchar_lengths = varchar_lengths ,
882
+ lock = lock ,
872
883
)
873
884
if index :
874
885
df .reset_index (level = df .index .names , inplace = True )
@@ -885,8 +896,6 @@ def to_sql( # pylint: disable=too-many-locals
885
896
_logger .debug ("sql: %s" , sql )
886
897
cursor .executemany (sql , (parameters ,))
887
898
if table != created_table : # upsert
888
- if lock :
889
- _lock (cursor , [table ], schema = schema )
890
899
_upsert (cursor = cursor , schema = schema , table = table , temp_table = created_table , primary_keys = primary_keys )
891
900
if commit_transaction :
892
901
con .commit ()
@@ -1354,9 +1363,6 @@ def copy_from_files( # pylint: disable=too-many-locals,too-many-arguments
1354
1363
boto3_session = boto3_session ,
1355
1364
s3_additional_kwargs = s3_additional_kwargs ,
1356
1365
)
1357
- if lock and table == created_table :
1358
- # Lock before copy if copying into target (not temp) table
1359
- _lock (cursor , [table ], schema = schema )
1360
1366
_copy (
1361
1367
cursor = cursor ,
1362
1368
path = path ,
@@ -1372,8 +1378,6 @@ def copy_from_files( # pylint: disable=too-many-locals,too-many-arguments
1372
1378
manifest = manifest ,
1373
1379
)
1374
1380
if table != created_table : # upsert
1375
- if lock :
1376
- _lock (cursor , [table ], schema = schema )
1377
1381
_upsert (cursor = cursor , schema = schema , table = table , temp_table = created_table , primary_keys = primary_keys )
1378
1382
if commit_transaction :
1379
1383
con .commit ()
0 commit comments