-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_entities.py
53 lines (46 loc) · 1.42 KB
/
update_entities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Re-puts entities of a given type, to set newly added properties.
#
# To run this script:
# - Make sure App Engine library (incl. yaml) is in PYTHONPATH.
# - Make sure that the remote API is included in app.yaml.
# - Run "tools/appengine_console.py APP_ID".
# - Import this module.
# - Import models from codereview.
# - update_entities.run(models.Issue) updates issues.
import logging
from google.appengine.ext import db
from codereview import models
import urllib2
def run(model_class, batch_size=100, last_key=None):
while True:
q = model_class.all()
if last_key:
q.filter('__key__ >', last_key)
q.order('__key__')
this_batch_size = batch_size
while True:
try:
try:
batch = q.fetch(this_batch_size)
except urllib2.URLError, err:
if 'timed out' in str(err):
raise db.Timeout
else:
raise
break
except db.Timeout:
logging.warn("Query timed out, retrying")
if this_batch_size == 1:
logging.critical("Unable to update entities, aborting")
return
this_batch_size //= 2
if not batch:
break
keys = None
while not keys:
try:
keys = db.put(batch)
except db.Timeout:
logging.warn("Put timed out, retrying")
last_key = keys[-1]
print "Updated %d records" % (len(keys),)