Skip to content

Commit a639d97

Browse files
committed
Add schema consistency checks.
1 parent 3d5f689 commit a639d97

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

roundup/instance.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)