source: OpenWorkouts-current/ow/migrations/README.rst @ eb20dc8

currentfeature/docs
Last change on this file since eb20dc8 was eb20dc8, checked in by Borja Lopez <borja@…>, 5 years ago

(#45) - Added ZODB migrations support

  • Property mode set to 100644
File size: 1.9 KB
Line 
1ZODB migrations
2===============
3
4This directory contains the migrations applied to the database when the app
5is starting.
6
7.. contents::
8
9
10Adding a new migration
11----------------------
12
13Migrations are named numerically, before adding a new migration look at the
14biggest number already available (look at **.py** files), then create a new
15**.py** file containing your migration.
16
17For example, if the last migration is **11.py**, create a file called **12.py**
18with your migration code inside.
19
20The migration file needs a **migrate** function in it, that accepts one
21parameter, the **root** of the pyramid application. There is where your code
22should modify whatever needs to be modified in the database.
23
24For example, this migration adds a new attribute to all objects in the
25database::
26
27  from BTrees.OOBTree import OOTreeSet
28
29  def migrate(root):
30      """
31      Adds some_new_attribute to all objects that do not have such attribute
32
33      >>> from models.root import Root, Child
34      >>> root = Root()
35      >>> child = Child()
36      >>> delattr(child, 'some_new_attr')
37      >>> assert getattr(child, 'some_new_attr', None) is None
38      True
39      >>> root.add(child)
40      >>> c = root.values()[0]
41      >>> assert getattr(c, 'some_new_attr', None) is None
42      True
43      >>> migrate(root)
44      >>> c = root.values()[0]
45      >>> assert getattr(c, 'some_new_attr', None) is None
46      False
47      """
48      for child in root.values():
49          if getattr(child, 'some_new_attr', None) is None:
50              child.some_new_attr = OOTreeSet()
51
52
53It is important that you add a proper doctest for the migration, because:
54
551. The first paragraph of the migration will be shown on the app logs, letting
56   us known which kind of changes have been made to the database on start.
57
582. Doctests there allow us to keep code coverage high and test those migrations
59   separately
60
61**Please use previous migrations for reference when in doubt on how to write
62a new migration**
Note: See TracBrowser for help on using the repository browser.