Skip to content

Commit

Permalink
Prevent race conditions by adding a random suffix to snowflake stage (#…
Browse files Browse the repository at this point in the history
…166)

* Prevent race conditions by adding a random suffix to snowflake stage

* Bump version

* Use UUID instead
  • Loading branch information
ganesh-krishnan authored May 5, 2020
1 parent 575b5ce commit f4789b1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
__copyright__ = 'Copyright © 2018, Instacart'
__credits__ = ['Montana Low', 'Jeremy Stanley', 'Emmanuel Turlay', 'Shrikar Archak', 'Ganesh Krishnan']
__license__ = 'MIT'
__version__ = '0.8.2'
__version__ = '0.8.3'
__maintainer__ = 'Montana Low'
__email__ = '[email protected]'
__status__ = 'Development Status :: 4 - Beta'
Expand Down
12 changes: 9 additions & 3 deletions lore/io/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
import tempfile
import threading
import uuid

from datetime import datetime

Expand Down Expand Up @@ -188,15 +189,20 @@ def insert(self, table, dataframe, batch_size=10 ** 5):
tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.csv.gz')
tmp.close()
batch.to_csv(tmp.name, index=False, header=False, sep='|', na_rep='\\N', quoting=csv.QUOTE_NONE, compression='gzip')
self._connection.connection.cursor().execute('PUT file://%(path)s @~/staged;' % {'path': tmp.name})
suffix = str(uuid.uuid4())
stage_name = 'staged_' + suffix
self.execute('REMOVE @~/%(stage_name)s' % {'stage_name': stage_name})
self._connection.connection.cursor().execute('PUT file://%(path)s @~/%(stage_name)s;' % {'path': tmp.name, 'stage_name': stage_name})
self._connection.connection.cursor().execute(
'COPY INTO %(table)s '
'FROM @~/staged '
'FROM @~/%(stage_name)s '
'FILE_FORMAT = (TYPE = CSV FIELD_DELIMITER = \'|\' SKIP_HEADER = 0 COMPRESSION = GZIP) '
'PURGE = TRUE' % {
'table': table
'table': table,
'stage_name': stage_name
}
)
self.execute('REMOVE @~/%(stage_name)s' % {'stage_name': stage_name})
finally:
os.remove(tmp.name)

Expand Down

0 comments on commit f4789b1

Please sign in to comment.