1. Adrian Dries
  2. fenton

Commits

Adrian Dries  committed 6ffcf92

Support always-next database upgrade "next"
merge upgrade-pending and upgrade-offline into upgrade-status

  • Participants
  • Parent commits f55ec79
  • Branches default

Comments (0)

Files changed (1)

File fenton/db.py

View file
 
 
 def initialize(app):
+    from fenton import logging
     os.putenv('PGTZ', 'UTC')
     name = app.config['fenton.db'] + '.db.'
     engine = sql.engine_from_config(app.config, name)
+    app.upgrade_path = app.config['upgrade.path']
     if not upgrading():
-        c = engine.connect()
-        version = get_version(c)
-        c.close()
-        if find_upgrade(app, version):
-            from fenton import logging
+        con = engine.connect()
+        if find_upgrade(con, app.upgrade_path):
             logging.log.error('upgrade pending')
+        con.close()
     DB = orm.sessionmaker(bind=engine, autoflush=False)
     DB.bind = engine
     return DB
 
 
 # command
-def upgrade_pending(cx):
-    version = get_version(cx.db)
-    return find_upgrade(cx.app, version)
-
-
-
-# command
-def upgrade_offline(cx):
-    version = get_version(cx.db)
-    up = find_upgrade(cx.app, version)
-    return up and not up.online or False
-
+def upgrade_status(cx):
+    '0 => no upgrade; 99 => online; 100 => offline'
+    up = find_upgrade(cx.db, cx.app.upgrade_path)
+    return up and (up.online and 99 or 100) or 0
 
 
 # command
 def upgrade(cx, commit=False):
-    cx = UpgradeContext(cx)
-    version = get_version(cx.db)
-    upgrade = find_upgrade(cx.app, version)
-    if not upgrade:
+    up = find_upgrade(cx.db, cx.app.upgrade_path)
+    if not up:
         print 'Nothing to do'
         return 1
+    cx = UpgradeContext(cx)
     try:
         with cx:
-            do_upgrade(cx, upgrade)
-            print 'Upgraded to version', upgrade.version
+            do_upgrade(cx, up)
+            print 'Upgraded to version', up.version
             if not commit:
                 raise NoCommit
     except NoCommit:
         return bool(os.environ.get(ENV_KEY))
 
 
-def get_version(conn):
+def get_version(con):
     v = SCHEMA_HISTORY.c.version
     q = sql.select([v]).order_by(v.desc()).limit(1)
-    return conn.execute(q).scalar() or 0
+    return con.execute(q).scalar() or 0
 
 
 def get_script(v, p):
         run('GRANT %s ON "%s" TO "%s" ' % (privs, obj, user))
 
 
-def find_upgrade(app, version, _memo=[]):
+def find_upgrade(con, path, _memo=[]):
 
     if _memo:
         return _memo[1]
 
     _memo.append(None)
     OP = os.path
-    version += 1
     upgrades = {}
     pending = None
-    path = app.config['upgrade.path']
+    version = get_version(con) + 1
 
     if not OP.exists(path):
         print 'Directory %s does not exist' % path
     elif not OP.isdir(path):
         print 'Not a directory:', path
 
+    v = None
     for d, _, files in os.walk(path, followlinks=True):
         for n in files:
             p = OP.join(d, n)
             if not OP.isfile(p):
                 continue
+            v = OP.splitext(n)[0]
             try:
-                v = int(OP.splitext(n)[0])
+                v = int(v)
             except ValueError:
-                continue
+                if v == 'next':
+                    v = version
+                else:
+                    continue
             if v < version:
                 continue
             if v in upgrades: