Created
October 9, 2012 19:11
-
-
Save andylolz/3860781 to your computer and use it in GitHub Desktop.
Block out a load of IDs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from google.appengine.ext import db | |
| import sys | |
| import inspect | |
| def fetch_all(model_or_query): | |
| '''Fetch all the entities of the given model, or satisfying the given query. | |
| ''' | |
| if isinstance(model_or_query, type) and issubclass(model_or_query, db.Model): | |
| query = model_or_query.all(keys_only=False) | |
| elif isinstance(model_or_query, db.Query): | |
| query = model_or_query | |
| else: | |
| raise Exception("model_or_query is neither a model nor a query") | |
| results = [] | |
| while True: | |
| to_fetch = query.fetch(500) | |
| results += to_fetch | |
| if len(to_fetch) < 500: | |
| return results | |
| query.with_cursor(query.cursor()) | |
| def do_the_business(): | |
| model_classes = inspect.getmembers(sys.modules["core.models"], inspect.isclass) | |
| for item in model_classes: | |
| cls = item[1] | |
| cls_name = item[0] | |
| if issubclass(cls, db.Model) and cls.__name__ != "PolyModel": | |
| print("getting data for %s" % cls_name) | |
| highest_id = 0 | |
| all_instances = fetch_all(cls.all(keys_only=True)) | |
| for e in all_instances: | |
| id = e.id() | |
| highest_id = id if id > highest_id else highest_id | |
| print("highest_id: %d" % highest_id) | |
| if highest_id: | |
| print("allocating ids until: %d" % highest_id) | |
| handmade_key = db.Key.from_path(cls_name, 1) | |
| db.allocate_id_range(handmade_key, 1, highest_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment