zoom.migrations module

zoom.migrations

Work in progress - experimental

class zoom.migrations.Migration(db)

Bases: object

Provies the methods to apply or revert transformations to get system to a desired state.

apply()

apply changes to the database

name
revert()

Revert changes to the database

class zoom.migrations.Migrations(db, steps)

Bases: object

performs the migration steps needed to get to the target version

Systems are migrated from one state to another by providing a sequence of steps typically subclassed from the Migration class. Migrations should provide both an apply method to peform a transformation to take a system to a desired state and, where possible, a revert method to undo the transformation to transform the sytem back to its original state.

As they are applied and reverted, the migrations are tracked in a data store including the name of the object peforming the migration ( usually a subclass of Migration) the version of the system that was attained by applying or reverting the migration, the revision number of the migration (an integer representing the application or reversion of a transformation), the method used (apply or revert) and the timestamp of when the migration took place.

>>> class AddFaxColumnToUser(Migration):
...     def apply(self):
...         self.db('alter table users add column fax char(30)')
...     def revert(self):
...         self.db('alter table users drop column fax')
>>> steps = [
...     StartMigration,
...     AddFaxColumnToUser,
... ]
>>> zoom.system.site = site = zoom.sites.Site()
>>> site.db = zoom.database.setup_test()
>>> print(site.db('describe users'))
Field      Type             Null Key Default Extra
---------- ---------------- ---- --- ------- --------------
id         int(10) unsigned NO   PRI None    auto_increment
username   char(50)         NO   UNI None
password   varchar(125)     YES      None
first_name char(40)         YES      None
last_name  char(40)         YES      None
email      char(60)         YES  MUL None
phone      char(30)         YES      None
created    datetime         YES      None
updated    datetime         YES      None
last_seen  datetime         YES  MUL None
created_by int(10) unsigned YES      None
updated_by int(10) unsigned YES      None
status     char(1)          YES      None
>>> migrations = Migrations(site.db, steps)
>>> migrations.migrate()
>>> print(site.db('describe users'))
Field      Type             Null Key Default Extra
---------- ---------------- ---- --- ------- --------------
id         int(10) unsigned NO   PRI None    auto_increment
username   char(50)         NO   UNI None
password   varchar(125)     YES      None
first_name char(40)         YES      None
last_name  char(40)         YES      None
email      char(60)         YES  MUL None
phone      char(30)         YES      None
created    datetime         YES      None
updated    datetime         YES      None
last_seen  datetime         YES  MUL None
created_by int(10) unsigned YES      None
updated_by int(10) unsigned YES      None
status     char(1)          YES      None
fax        char(30)         YES      None
>>> migrations.migrate(0)
>>> print(site.db('describe users'))
Field      Type             Null Key Default Extra
---------- ---------------- ---- --- ------- --------------
id         int(10) unsigned NO   PRI None    auto_increment
username   char(50)         NO   UNI None
password   varchar(125)     YES      None
first_name char(40)         YES      None
last_name  char(40)         YES      None
email      char(60)         YES  MUL None
phone      char(30)         YES      None
created    datetime         YES      None
updated    datetime         YES      None
last_seen  datetime         YES  MUL None
created_by int(10) unsigned YES      None
updated_by int(10) unsigned YES      None
status     char(1)          YES      None
>>> migrations.revisions.zap()
migrate(target=None)

migrate to a target version

Target is the desired version. If no target is supplied the migrations required to get to the most up to date version are applied.

class zoom.migrations.StartMigration(db)

Bases: zoom.migrations.Migration

Start Migration

This is a starting state migration. It serves as a starting point for the system before any migrations were applied and does not itself apply any data transformations. This migration should be used as the first element of all migration sequences.

In theory, migrating back to this state should put the database back into its original state. By “in theory” we mean that assuming all of the applied migrations are completely revertable which may not be the case in all cases.

class zoom.migrations.SystemMigrationRecord

Bases: zoom.utils.Record

Migration Record