Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a basic migration for adding _week_start_at

This loads all documents that do not have a _week_start_at field and
adds it. This is required for the periodic grouping.
  • Loading branch information...
commit a01827b81c876a93efe9f5f765438c70affd15b3 1 parent 27cb458
Rob Young robyoung authored
Showing with 65 additions and 0 deletions.
  1. +23 −0 migrations/001_add_week_start.py
  2. +42 −0 run_migrations.py
23 migrations/001_add_week_start.py
View
@@ -0,0 +1,23 @@
+"""
+Add _week_start_at field to all documents in all collections
+"""
+from backdrop.core.storage import utc
+from backdrop.core.records import Record
+import logging
+
+log = logging.getLogger(__name__)
+
+
+def up(db):
+ for name in db.collection_names():
+ log.info("Migrating collection: {0}".format(name))
+ collection = db[name]
+ query = {
+ "_timestamp": {"$exists": True},
+ "_week_start_at": {"$exists": False}
+ }
+ for document in collection.find(query):
+ document['_timestamp'] = utc(document['_timestamp'])
+ record = Record(document)
+
+ collection.save(record.to_mongo())
42 run_migrations.py
View
@@ -0,0 +1,42 @@
+"""
+Run all migrations
+"""
+import imp
+import os
+import pymongo
+from os.path import join
+import logging
+
+logging.basicConfig(level=logging.DEBUG)
+
+ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
+
+
+def load_config(env):
+ config_path = os.path.join(ROOT_PATH, 'backdrop', 'write', 'config',
+ '{0}.py'.format(env))
+ return imp.load_source('config', config_path)
+
+
+def get_database(config):
+ client = pymongo.MongoClient(config.MONGO_HOST, config.MONGO_PORT)
+
+ return client[config.DATABASE_NAME]
+
+
+def get_migrations():
+ migrations_path = join(ROOT_PATH, 'migrations')
+ for migration_file in os.listdir(migrations_path):
+ if migration_file.endswith('.py'):
+ migration_path = join(migrations_path, migration_file)
+
+ yield imp.load_source('migration', migration_path)
+
+
+if __name__ == '__main__':
+
+ config = load_config(os.getenv('GOVUK_ENV', 'development'))
+ database = get_database(config)
+
+ for migration in get_migrations():
+ migration.up(database)
Please sign in to comment.
Something went wrong with that request. Please try again.