changeset 4266:2b75274936bc

Add schema consistency checks.
author Stefan Seefeld <stefan@seefeld.name>
date Fri, 09 Oct 2009 13:54:54 +0000
parents e24a6ca34448
children c5f52c2c9e36
files roundup/instance.py
diffstat 1 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/instance.py	Fri Oct 09 13:51:35 2009 +0000
+++ b/roundup/instance.py	Fri Oct 09 13:54:54 2009 +0000
@@ -136,6 +136,27 @@
         # or this is the first time the database is opened,
         # do database upgrade checks
         if not (self.optimize and self.db_open):
+            # As a consistency check, ensure that every link property is
+            # pointing at a defined class.  Otherwise, the schema is
+            # internally inconsistent.  This is an important safety
+            # measure as it protects against an accidental schema change
+            # dropping a table while there are still links to the table;
+            # once the table has been dropped, there is no way to get it
+            # back, so it is important to drop it only if we are as sure
+            # as possible that it is no longer needed.
+            classes = db.getclasses()
+            for classname in classes:
+                cl = db.getclass(classname)
+                for propname, prop in cl.getprops().iteritems():
+                    if not isinstance(prop, (hyperdb.Link,
+                                             hyperdb.Multilink)):
+                        continue
+                    linkto = prop.classname
+                    if linkto not in classes:
+                        raise ValueError, \
+                            ("property %s.%s links to non-existent class %s"
+                             % (classname, propname, linkto))
+
             db.post_init()
             self.db_open = 1
         return db

Roundup Issue Tracker: http://roundup-tracker.org/