Skip to content

Commit

Permalink
🐛 Fix transform file producer code to check before insert
Browse files Browse the repository at this point in the history
The cnx-publishing tests pass with this change in place.
  • Loading branch information
mmulich committed Mar 14, 2016
1 parent dc0de21 commit 93e5eaf
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cnxarchive/transforms/producers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# See LICENCE.txt for details.
# ###
"""Producer functions that wrap basic transforms to create complete docs."""

import hashlib
from io import BytesIO

from lxml import etree
Expand Down Expand Up @@ -295,11 +295,17 @@ def produce_transformed_file(cursor, ident, transform_type,
warning_messages = 'Invalid References: {}' \
.format('; '.join(bad_refs))

# Insert the cnxml into the database.
payload = (memoryview(new_content), media_type,)
cursor.execute("INSERT INTO files (file, media_type) VALUES (%s, %s) "
"RETURNING fileid;", payload)
destination_file_id = cursor.fetchone()[0]
# Find existing file in database before attempting to insert a new one.
sha1 = hashlib.new('sha1', new_content).hexdigest()
cursor.execute("SELECT fileid FROM files WHERE sha1 = %s", (sha1,))
try:
destination_file_id = cursor.fetchone()[0]
except TypeError:
# Insert the cnxml into the database.
payload = (memoryview(new_content), media_type,)
cursor.execute("INSERT INTO files (file, media_type) VALUES (%s, %s) "
"RETURNING fileid;", payload)
destination_file_id = cursor.fetchone()[0]
for filename in destination_filenames:
cursor.execute("INSERT INTO module_files "
" (module_ident, fileid, filename) "
Expand Down

0 comments on commit 93e5eaf

Please sign in to comment.