changeset 144:495fb7bcf45a

Allow to force both current and destination schema version in upgrade operation.
author Daniele Nicolodi <daniele@grinta.net>
date Thu, 20 Oct 2011 09:30:33 +0200
parents 5e847f92a010
children 0ea8cef630e2
files src/ltpdarepo/admin.py src/ltpdarepo/upgrade.py
diffstat 2 files changed, 19 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/ltpdarepo/admin.py	Thu Oct 20 09:29:43 2011 +0200
+++ b/src/ltpdarepo/admin.py	Thu Oct 20 09:30:33 2011 +0200
@@ -88,7 +88,8 @@
 
 from .upgrade import upgrade
 cmd = commands.add(upgrade)
-cmd.add_argument('--from', type=float, dest='force', metavar='REV')
+cmd.add_argument('--from', type=float, dest='_from', metavar='REV')
+cmd.add_argument('--to', type=float, dest='_to', metavar='REV')
 
 
 def useradd(username, password=None, **kwargs):
--- a/src/ltpdarepo/upgrade.py	Thu Oct 20 09:29:43 2011 +0200
+++ b/src/ltpdarepo/upgrade.py	Thu Oct 20 09:30:33 2011 +0200
@@ -18,7 +18,7 @@
     return func
 
 
-def upgrade(force=None):
+def upgrade(_from=None, _to=None):
     """run database schema upgrade steps"""
     logger = logging.getLogger(__name__)
 
@@ -27,19 +27,25 @@
 
     # current schema version
     curs.execute("""SELECT value+0 FROM options WHERE name='version'""")
-    current = curs.fetchone()[0]
-    logger.info("current database shema v%s", current)
+    curr = curs.fetchone()[0]
+    logger.info("current database shema v%s", curr)
 
     # allow to force upgrade from specific version
-    if force is not None:
-        current = force
-        logger.info("force upgrade from schema v%s", current)
+    if _from is not None:
+        curr = _from
+        logger.info("force upgrade from schema v%s", curr)
 
-    while current != SCHEMA:
+    # allow to force upgrade to specific version
+    dest = SCHEMA
+    if _to is not None:
+        dest = _to
+        logger.info("force upgrade to schema v%s", dest)
+
+    while curr < dest:
         # next upgrade step
-        step = steps.get(current, None)
+        step = steps.get(curr, None)
         if step is None:
-            raise ValueError("no upgrade path from database shema v%s" % current)
+            raise ValueError("no upgrade path from database shema v%s" % curr)
         fromr, tor, func = step
 
         # run upgrade step
@@ -47,9 +53,9 @@
         func(conn)
 
         # update current schema version
-        current = tor
+        curr = tor
         curs.execute("""UPDATE options SET value=%s
-                        WHERE name='version'""", str(current))
+                        WHERE name='version'""", str(curr))
 
     conn.commit()
     conn.close()