|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Sample application that shows how to perform a "schema migration" using |
| 16 | +Google Cloud Datastore. |
| 17 | +
|
| 18 | +This application uses one model named "Pictures" but two different versions |
| 19 | +of it. v2 contains two extra fields. The application shows how to |
| 20 | +populate these new fields onto entities that existed prior to adding the |
| 21 | +new fields to the model class. |
| 22 | +""" |
| 23 | + |
| 24 | +import logging |
| 25 | +import os |
| 26 | + |
| 27 | +from google.appengine.ext import deferred |
| 28 | +from google.appengine.ext import ndb |
| 29 | +import jinja2 |
| 30 | +import webapp2 |
| 31 | + |
| 32 | +import models_v1 |
| 33 | +import models_v2 |
| 34 | + |
| 35 | + |
| 36 | +JINJA_ENVIRONMENT = jinja2.Environment( |
| 37 | + loader=jinja2.FileSystemLoader( |
| 38 | + os.path.join(os.path.dirname(__file__), 'templates')), |
| 39 | + extensions=['jinja2.ext.autoescape'], |
| 40 | + autoescape=True) |
| 41 | + |
| 42 | + |
| 43 | +class DisplayEntitiesHandler(webapp2.RequestHandler): |
| 44 | + """Displays the current set of entities and options to add entities |
| 45 | + or update the schema.""" |
| 46 | + def get(self): |
| 47 | + # Force ndb to use v2 of the model by re-loading it. |
| 48 | + reload(models_v2) |
| 49 | + |
| 50 | + entities = models_v2.Picture.query().fetch() |
| 51 | + template_values = { |
| 52 | + 'entities': entities, |
| 53 | + } |
| 54 | + |
| 55 | + template = JINJA_ENVIRONMENT.get_template('index.html') |
| 56 | + self.response.write(template.render(template_values)) |
| 57 | + |
| 58 | + |
| 59 | +class AddEntitiesHandler(webapp2.RequestHandler): |
| 60 | + """Adds new entities using the v1 schema.""" |
| 61 | + def post(self): |
| 62 | + # Force ndb to use v1 of the model by re-loading it. |
| 63 | + reload(models_v1) |
| 64 | + |
| 65 | + # Save some example data. |
| 66 | + ndb.put_multi([ |
| 67 | + models_v1.Picture(author='Alice', name='Sunset'), |
| 68 | + models_v1.Picture(author='Bob', name='Sunrise') |
| 69 | + ]) |
| 70 | + |
| 71 | + self.response.write(""" |
| 72 | + Entities created. <a href="/">View entities</a>. |
| 73 | + """) |
| 74 | + |
| 75 | + |
| 76 | +class UpdateSchemaHandler(webapp2.RequestHandler): |
| 77 | + """Queues a task to start updating the model schema.""" |
| 78 | + def post(self): |
| 79 | + deferred.defer(update_schema_task) |
| 80 | + self.response.write(""" |
| 81 | + Schema update started. Check the console for task progress. |
| 82 | + <a href="/">View entities</a>. |
| 83 | + """) |
| 84 | + |
| 85 | + |
| 86 | +def update_schema_task(cursor=None, num_updated=0, batch_size=100): |
| 87 | + """Task that handles updating the models' schema. |
| 88 | +
|
| 89 | + This is started by |
| 90 | + UpdateSchemaHandler. It scans every entity in the datastore for the |
| 91 | + Picture model and re-saves it so that it has the new schema fields. |
| 92 | + """ |
| 93 | + |
| 94 | + # Force ndb to use v2 of the model by re-loading it. |
| 95 | + reload(models_v2) |
| 96 | + |
| 97 | + # Get all of the entities for this Model. |
| 98 | + query = models_v2.Picture.query() |
| 99 | + pictures, cursor, more = query.fetch_page(batch_size, start_cursor=cursor) |
| 100 | + |
| 101 | + to_put = [] |
| 102 | + for picture in pictures: |
| 103 | + # Give the new fields default values. |
| 104 | + # If you added new fields and were okay with the default values, you |
| 105 | + # would not need to do this. |
| 106 | + picture.num_votes = 1 |
| 107 | + picture.avg_rating = 5 |
| 108 | + to_put.append(picture) |
| 109 | + |
| 110 | + # Save the updated entities. |
| 111 | + if to_put: |
| 112 | + ndb.put_multi(to_put) |
| 113 | + num_updated += len(to_put) |
| 114 | + logging.info( |
| 115 | + 'Put {} entities to Datastore for a total of {}'.format( |
| 116 | + len(to_put), num_updated)) |
| 117 | + |
| 118 | + # If there are more entities, re-queue this task for the next page. |
| 119 | + if more: |
| 120 | + deferred.defer( |
| 121 | + update_schema_task, cursor=query.cursor(), num_updated=num_updated) |
| 122 | + else: |
| 123 | + logging.debug( |
| 124 | + 'update_schema_task complete with {0} updates!'.format( |
| 125 | + num_updated)) |
| 126 | + |
| 127 | + |
| 128 | +app = webapp2.WSGIApplication([ |
| 129 | + ('/', DisplayEntitiesHandler), |
| 130 | + ('/add_entities', AddEntitiesHandler), |
| 131 | + ('/update_schema', UpdateSchemaHandler)]) |
0 commit comments