Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiaaro/6256f414da95be5e7ef6 to your computer and use it in GitHub Desktop.
Save jiaaro/6256f414da95be5e7ef6 to your computer and use it in GitHub Desktop.
create hstore col
from django.db import connection
def hs_str(s):
s = unicode(s)
return s.replace('"', r'\"')
def dict_to_hstore(d):
hs_data = [u'"{0}" => "{1}"'.format(hs_str(k), hs_str(v)) for (k, v) in data.items()]
hs_data = u",".join(hs_data)
return "u'{0}'::hstore".format(hs_data)
cursor = connection.cursor()
i = 0
while True:
pids = list(Participant.objects.values_list('id', flat=True)[i:i+1000])
if not pids:
break
query = u""
for pid, data in Participant.objects.get_answer_data(pids):
if not data:
continue
query += u"UPDATE stitcher_participant SET hs_data = %s;" % dict_to_hstore(data)
cursor.execute(query)
i += 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment