Skip to content

Instantly share code, notes, and snippets.

@igorkramaric
Created June 27, 2014 08:11
Show Gist options
  • Save igorkramaric/9273224839fb8c176b24 to your computer and use it in GitHub Desktop.
Save igorkramaric/9273224839fb8c176b24 to your computer and use it in GitHub Desktop.
def migrate_to_hstore(request):
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 = ",".join(hs_data)
return u"$${0}$$::hstore".format(hs_data)
cursor = connection.cursor()
i = 26896
step=1
while True and i<26900:
pids = list(Participant.objects.values_list('id', flat=True)[i:i+step])
if not pids:
break
query = ""
for pid, data in Participant.objects.get_answer_data(pids).items():
if not data:
continue
query += u"UPDATE stitcher_participant SET hs_data = %s WHERE ID = %s;" % (dict_to_hstore(data), pid)
try:
cursor.execute(query)
except:
print 'Exception cursor.execute.\nQuery [size:%d] is:[%s] [%s,%s], %s' % ( len(query), query, str(pids), query, fe() )
print u"%06d cursor.execute(query) OK %s%s" %(i, pids[:20], "...")
print "*"*20
i += step
return HttpResponse("<h1>hey</h1>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment