summary refs log tree commit diff
path: root/synapse/app
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <paul@matrix.org>2014-09-03 14:14:04 +0100
committerPaul "LeoNerd" Evans <paul@matrix.org>2014-09-03 14:14:39 +0100
commit756e171ad0715daef6b0bdaccb61b0278a786ef3 (patch)
tree1b08aa37c2bf428dc96be8542faa6a0d0b2f6c75 /synapse/app
parentBF: Do not be polluted by ops events that come when paginating back (diff)
downloadsynapse-756e171ad0715daef6b0bdaccb61b0278a786ef3.tar.xz
Store SQL DDL deltas as well; attempt to upgrade the database on startup if it's too old
Diffstat (limited to 'synapse/app')
-rwxr-xr-xsynapse/app/homeserver.py22
1 files changed, 15 insertions, 7 deletions
diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py
index 0fa4b40c94..db2e9ebb0c 100755
--- a/synapse/app/homeserver.py
+++ b/synapse/app/homeserver.py
@@ -93,20 +93,28 @@ class SynapseHomeServer(HomeServer):
             if row and row[0]:
                 user_version = row[0]
 
-                if user_version < SCHEMA_VERSION:
-                    # TODO(paul): add some kind of intelligent fixup here
-                    raise ValueError("Cannot use this database as the " +
-                        "schema version (%d) does not match (%d)" %
-                        (user_version, SCHEMA_VERSION)
+                if user_version > SCHEMA_VERSION:
+                    raise ValueError("Cannot use this database as it is too " +
+                        "new for the server to understand"
                     )
+                elif user_version < SCHEMA_VERSION:
+                    logging.info("Upgrading database from version %d",
+                        user_version
+                    )
+
+                    # Run every version since after the current version.
+                    for v in range(user_version + 1, SCHEMA_VERSION + 1):
+                        sql_script = read_schema("delta/v%d" % (v))
+                        c.executescript(sql_script)
+
+                    db_conn.commit()
 
             else:
                 for sql_loc in SCHEMAS:
                     sql_script = read_schema(sql_loc)
 
                     c.executescript(sql_script)
-                    db_conn.commit()
-
+                db_conn.commit()
                 c.execute("PRAGMA user_version = %d" % SCHEMA_VERSION)
 
             c.close()