Updated database upgrade code
authorThierry Florac <tflorac@ulthar.net>
Sat, 10 Mar 2018 01:58:45 +0100
changeset 145 1a58b78a8f99
parent 144 c9e3cfbbe4ad
child 146 46bc9bf127bf
Updated database upgrade code
src/pyams_utils/interfaces/site.py
src/pyams_utils/site.py
src/pyams_utils/timezone/utility.py
--- a/src/pyams_utils/interfaces/site.py	Thu Mar 08 09:13:06 2018 +0100
+++ b/src/pyams_utils/interfaces/site.py	Sat Mar 10 01:58:45 2018 +0100
@@ -41,10 +41,11 @@
 class ISiteGenerations(Interface):
     """Site generations interface"""
 
+    order = Attribute("Order in which generations should be upgraded")
     generation = Attribute("Current schema generation")
 
     def evolve(self, site, current=None):
-        """Evolve from current generation to last one"""
+        """Evolve database from current generation to last one"""
 
 
 class IStaticConfigurationManager(IAttributeAnnotatable):
--- a/src/pyams_utils/site.py	Thu Mar 08 09:13:06 2018 +0100
+++ b/src/pyams_utils/site.py	Sat Mar 10 01:58:45 2018 +0100
@@ -131,11 +131,14 @@
             generations = annotations.get(SITE_GENERATIONS_KEY)
             if generations is None:
                 generations = annotations[SITE_GENERATIONS_KEY] = PersistentDict()
-            for name, utility in get_utilities_for(ISiteGenerations):
+            for name, utility in sorted(get_utilities_for(ISiteGenerations),
+                                        key=lambda x: x[1].order):
                 if not name:
                     name = '.'.join((utility.__module__, utility.__class__.__name__))
                 current = generations.get(name)
-                if (not current) or (current < utility.generation):
+                if not current:
+                    print("Upgrading {0} to generation {1}...".format(name, utility.generation))
+                elif current < utility.generation:
                     print("Upgrading {0} from generation {1} to {2}...".format(name, current, utility.generation))
                 utility.evolve(application, current)
                 generations[name] = utility.generation
--- a/src/pyams_utils/timezone/utility.py	Thu Mar 08 09:13:06 2018 +0100
+++ b/src/pyams_utils/timezone/utility.py	Sat Mar 10 01:58:45 2018 +0100
@@ -18,14 +18,12 @@
 # import interfaces
 from pyams_utils.interfaces.site import ISiteGenerations
 from pyams_utils.interfaces.timezone import IServerTimezone
-from zope.site.interfaces import INewLocalSite
 
 # import packages
 from persistent import Persistent
 from pyams_utils.property import DocFieldProperty
 from pyams_utils.registry import utility_config
 from pyams_utils.site import check_required_utilities
-from pyramid.events import subscriber
 from zope.container.contained import Contained
 from zope.interface import implementer
 
@@ -39,17 +37,11 @@
 REQUIRED_UTILITIES = ((IServerTimezone, '', ServerTimezoneUtility, 'Server timezone'),)
 
 
-@subscriber(INewLocalSite)
-def handle_new_local_site(event):
-    """Create a new ServerTimezoneUtility when a site is created"""
-    site = event.manager.__parent__
-    check_required_utilities(site, REQUIRED_UTILITIES)
-
-
 @utility_config(name='PyAMS timezone', provides=ISiteGenerations)
 class TimezoneGenerationsChecker(object):
     """Timezone generations checker"""
 
+    order = 10
     generation = 1
 
     def evolve(self, site, current=None):