Skip to content

Instantly share code, notes, and snippets.

@jdunne-kaplan
Last active August 29, 2015 14:06
Show Gist options
  • Save jdunne-kaplan/1e2a28a4b8b20d5b9664 to your computer and use it in GitHub Desktop.
Save jdunne-kaplan/1e2a28a4b8b20d5b9664 to your computer and use it in GitHub Desktop.
timeline_version migration
import models
import datetime
import time
from google.appengine.ext import ndb
# Upgrade sessions and set timeline_version to 1
#sessions = models.DBSession.all().run()
#for s in sessions:
# if s.timeline_version:
# continue
# s.timeline_version = 1
# s.put()
# Upgrade TimelineEvents and set timeline_version to 1 and sequence_number to utc_timestamp(start):
tles = models.TimelineEvent.query()
futs = []
for tle in tles:
modded = False
if not tle.timeline_version:
tle.timeline_version = 1
modded = True
if not tle.sequence_number:
# UTC timestamp (# of milliseconds since 1970)
tle.sequence_number = int(time.mktime(tle.start.timetuple())*1e3 + tle.start.microsecond/1e3)
modded = True
if not modded:
continue
# Need to special-case handle any TextProperty()s, but we'll hack for the drawing_args case specifically:
drawing_args = None
if hasattr(tle, 'drawing_args'):
drawing_args = getattr(tle, 'drawing_args')
delattr(tle, 'drawing_args')
# Add back the drawing_args through the proper channels:
if drawing_args:
drawing_args_dict = drawing_args.to_dict()
safe_drawing_args = models.SlideAnnotationEventArgs(**drawing_args_dict)
tle.drawing_args = safe_drawing_args
futs.append(tle.put_async())
if len(futs) >= 50:
ndb.Future.wait_all(futs)
for fut in futs:
fut.get_result()
futs = []
if futs is not None and len(futs) > 0:
ndb.Future.wait_all(futs)
for fut in futs:
fut.get_result()
import models
import datetime
import time
from google.appengine.ext import ndb
# Upgrade TimelineEvents and set sequence_number to utc_timestamp(start):
tles = models.TimelineEvent.query()
for tle in tles:
modded = False
if not tle.sequence_number:
# UTC timestamp (# of milliseconds since 1970)
tle.sequence_number = int(time.mktime(tle.start.timetuple())*1e3 + tle.start.microsecond/1e3)
modded = True
if not modded:
continue
# Need to special-case handle any TextProperty()s, but we'll hack for the drawing_args case specifically:
drawing_args = None
if hasattr(tle, 'drawing_args'):
drawing_args = getattr(tle, 'drawing_args')
delattr(tle, 'drawing_args')
# Add back the drawing_args through the proper channels:
if drawing_args:
drawing_args_dict = drawing_args.to_dict()
safe_drawing_args = models.SlideAnnotationEventArgs(**drawing_args_dict)
tle.drawing_args = safe_drawing_args
tle.put()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment