@@ -136,6 +136,27 @@ def open(self, name=None):
136136 # or this is the first time the database is opened,
137137 # do database upgrade checks
138138 if not (self .optimize and self .db_open ):
139+ # As a consistency check, ensure that every link property is
140+ # pointing at a defined class. Otherwise, the schema is
141+ # internally inconsistent. This is an important safety
142+ # measure as it protects against an accidental schema change
143+ # dropping a table while there are still links to the table;
144+ # once the table has been dropped, there is no way to get it
145+ # back, so it is important to drop it only if we are as sure
146+ # as possible that it is no longer needed.
147+ classes = db .getclasses ()
148+ for classname in classes :
149+ cl = db .getclass (classname )
150+ for propname , prop in cl .getprops ().iteritems ():
151+ if not isinstance (prop , (hyperdb .Link ,
152+ hyperdb .Multilink )):
153+ continue
154+ linkto = prop .classname
155+ if linkto not in classes :
156+ raise ValueError , \
157+ ("property %s.%s links to non-existent class %s"
158+ % (classname , propname , linkto ))
159+
139160 db .post_init ()
140161 self .db_open = 1
141162 return db
0 commit comments