Migrating data in Charmworld

Charmworld uses MongoDB for its data storage. In order to facilitate changes to the design of the data in Mongo a migration tool is provided in migrations/migrate.py.

The migration tool stores the current version in the collection version.

Commands

The migrate command is created when charmworld setup.py is processed.

In the order of typical usage:

Check the current version of the data store

$ bin/migrations current
0

Add a new migration script to be run.

$ bin/migrations new -d "add new data to mongo"
Created new migration: 001_add_new_data_to_mongo.py

Check the latest migration available.

$ bin/migrations latest
1

Run any migrations not in Mongo

# Perform any long-running migrations in temp storage
$ bin/migrations prepare-upgrade
# Swap temp storage into place and run short-running migrations
$ bin/migrations upgrade
Updated the datastore to version: 1

Initial Upgrade requires –init

Initially the datastore is not tracked. The first upgrade done needs to include –init to version the datastore and then process upgrades.

$ bin/migrations prepare-upgrade --init
Updated the datastore to version: 1

Deleting migrations

The most recent migration must not be deleted, because it also serves to identify the current migration version for up-to-date instances. All other migrations can safely be deleted if they are no longer needed. So if all production instances have had the migration applied, it is safe to simply delete it.

Avoid performing an es-update in migrations

The charm and Makefile ensure that es-update is run before running the code, so it is unnecessary to run es-update in a migration.