|
1693.
|
|
|
You can manually cause the application to be saved by calling its <code>.save</code> method (on the <code class="API">twisted.persisted.sob.IPersistable</code> adapted object).
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:48
|
|
1694.
|
|
|
Versioned: New Code Meets Old Data
|
|
|
type: Content of: <html><body><h2>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:53
|
|
1695.
|
|
|
So suppose you're running version 1 of some application, and you want to upgrade to version 2. You shut down the program, giving you a .tap file that you could restore with twistd to get back to the same state that you had before. The upgrade process is to then install the new version of the application, and then use twistd to launch the saved .tap file. The old data will be loaded into classes created with the new code, and now you'll have a program running with the new behavior but the old data.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:55
|
|
1696.
|
|
|
But what about the data structures that have changed? Since these structures are really just pickled class instances, the real question is what about the class definitions that have changed? Changes to class methods are easy: nothing about them is saved in the .tap file. The issue is when the data attributes of a instance are added, removed, or their format is changed.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:63
|
|
1697.
|
|
|
Twisted provides a mechanism called <code class="API" base="twisted.persisted.styles">Versioned</code> to ease these upgrades. Each version of the data structure (i.e. each version of the class) gets a version number. This number must change every time you add or remove a data attribute to the class. It must also change every time you modify one of those data attributes: for example, if you use a string in one version and an integer in another, those versions must have different version numbers.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:70
|
|
1698.
|
|
|
The version number is defined in a class attribute named <code>persistenceVersion</code>. This is an integer which will be stored in the .tap file along with the rest of the instance state. When the object is unserialized, the saved persistenceVersion is compared against the current class's value, and if they differ, special upgrade methods are called. These methods are named <code>upgradeToVersionNN</code>, and there must be one for each intermediate version. These methods are expected to manipulate the instance's state from the previous version's format into that of the new version.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:79
|
|
1699.
|
|
|
To use this, simply have your class inherit from <code class="API" base="twisted.persisted.styles">Versioned</code>. You don't have to do this from the very beginning of time: all objects have an implicit version number of <q>0</q> when they don't inherit from Versioned. So when you first make an incompatible data-format change to your class, add Versioned to the inheritance list, and add an <code>upgradeToVersion1</code> method.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:89
|
|
1700.
|
|
|
For example, suppose the first version of our class saves an integer which measures the size of a line. We release this as version 1.0 of our neat application:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:96
|
|
1701.
|
|
|
class Thing:
def __init__(self, length):
self.length = length
|
|
|
type: Content of: <html><body><pre>
|
|
|
represents a line break.
Start a new line in the equivalent position in the translation.
|
|
|
represents a space character.
Enter a space in the equivalent position in the translation.
|
|
|
|
class Thing:
def __init__(self, length):
self.length = length
|
|
Translated and reviewed by
Patrick Fiquet
|
|
|
|
Located in
howto/upgrading.xhtml:101
|
|
1702.
|
|
|
Then we fix some bugs elsewhere, and release versions 1.1 and 1.2 of the application. Later, we decide that we should add some units to the length, so that people can refer to it in inches or meters. Version 1.3 is shipped with the following code:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/upgrading.xhtml:106
|