Skip to content

Commit cc2c12b

Browse files
author
Anthony Baxter
committed
hyperdb grows a refresh_database() method.
There will be a future roundup-admin command to invoke this. XXXX Unit tests do _not_ cover large slabs of the hyperdb backend code, it seems. :-(
1 parent 8493c93 commit cc2c12b

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

roundup/backends/back_anydbm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
#$Id: back_anydbm.py,v 1.128 2003-09-14 18:55:37 jlgijsbers Exp $
18+
#$Id: back_anydbm.py,v 1.129 2003-10-07 11:58:57 anthonybaxter Exp $
1919
'''
2020
This module defines a backend that saves the hyperdatabase in a database
2121
chosen by anydbm. It is guaranteed to always be available in python
@@ -98,6 +98,10 @@ def post_init(self):
9898
if self.indexer.should_reindex():
9999
self.reindex()
100100

101+
def refresh_database(self):
102+
"Rebuild the database"
103+
self.reindex()
104+
101105
def reindex(self):
102106
for klass in self.classes.values():
103107
for nodeid in klass.list():

roundup/backends/back_metakit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: back_metakit.py,v 1.50 2003-09-08 20:39:18 jlgijsbers Exp $
1+
# $Id: back_metakit.py,v 1.51 2003-10-07 11:58:57 anthonybaxter Exp $
22
'''
33
Metakit backend for Roundup, originally by Gordon McMillan.
44
@@ -69,6 +69,10 @@ def post_init(self):
6969
if self.indexer.should_reindex():
7070
self.reindex()
7171

72+
def refresh_database(self):
73+
# XXX handle refresh
74+
self.reindex()
75+
7276
def reindex(self):
7377
for klass in self.classes.values():
7478
for nodeid in klass.list():

roundup/backends/rdbms_common.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: rdbms_common.py,v 1.64 2003-10-07 08:34:58 anthonybaxter Exp $
1+
# $Id: rdbms_common.py,v 1.65 2003-10-07 11:58:58 anthonybaxter Exp $
22
''' Relational database (SQL) backend common code.
33
44
Basics:
@@ -145,6 +145,21 @@ def post_init(self):
145145
# commit
146146
self.conn.commit()
147147

148+
def refresh_database(self):
149+
# now detect changes in the schema
150+
for classname, spec in self.classes.items():
151+
dbspec = self.database_schema[classname]
152+
self.update_class(spec, dbspec, force=1)
153+
self.database_schema[classname] = spec.schema()
154+
# update the database version of the schema
155+
self.sql('delete from schema')
156+
self.save_dbschema(self.database_schema)
157+
# reindex the db
158+
self.reindex()
159+
# commit
160+
self.conn.commit()
161+
162+
148163
def reindex(self):
149164
for klass in self.classes.values():
150165
for nodeid in klass.list():
@@ -170,34 +185,52 @@ def determine_columns(self, properties):
170185
cols.sort()
171186
return cols, mls
172187

173-
def update_class(self, spec, old_spec):
188+
def update_class(self, spec, old_spec, force=0):
174189
''' Determine the differences between the current spec and the
175-
database version of the spec, and update where necessary
190+
database version of the spec, and update where necessary.
191+
If 'force' is true, update the database anyway.
176192
'''
177193
new_spec = spec
178194
new_has = new_spec.properties.has_key
179195

180196
new_spec = new_spec.schema()
181197
new_spec[1].sort()
182198
old_spec[1].sort()
183-
if new_spec == old_spec:
199+
if not force and new_spec == old_spec:
184200
# no changes
185201
return 0
186202

187203
if __debug__:
188204
print >>hyperdb.DEBUG, 'update_class FIRING'
189205

190206
# key property changed?
191-
if old_spec[0] != new_spec[0]:
207+
if force or old_spec[0] != new_spec[0]:
192208
if __debug__:
193209
print >>hyperdb.DEBUG, 'update_class setting keyprop', `spec[0]`
194210
# XXX turn on indexing for the key property.
211+
index_sql = 'drop index _%s_%s_idx'%(
212+
spec.classname, old_spec[0])
213+
if __debug__:
214+
print >>hyperdb.DEBUG, 'drop_index', (self, index_sql)
215+
try:
216+
self.cursor.execute(index_sql1)
217+
except:
218+
# Hackage. Until we update the schema to include some
219+
# backend-specific knowledge, assume that this might fail.
220+
pass
221+
222+
index_sql = 'create index _%s_%s_idx on _%s(%s)'%(
223+
spec.classname, new_spec[0],
224+
spec.classname, new_spec[0])
225+
if __debug__:
226+
print >>hyperdb.DEBUG, 'create_index', (self, index_sql)
227+
self.cursor.execute(index_sql1)
195228

196229
# detect multilinks that have been removed, and drop their table
197230
old_has = {}
198231
for name,prop in old_spec[1]:
199232
old_has[name] = 1
200-
if not new_has(name) and isinstance(prop, Multilink):
233+
if (force or not new_has(name)) and isinstance(prop, Multilink):
201234
# it's a multilink, and it's been removed - drop the old
202235
# table. First drop indexes.
203236
index_sqls = [ 'drop index %s_%s_l_idx'%(spec.classname, ml),
@@ -224,7 +257,7 @@ def update_class(self, spec, old_spec):
224257
for propname,x in new_spec[1]:
225258
prop = properties[propname]
226259
if isinstance(prop, Multilink):
227-
if not old_has(propname):
260+
if force or not old_has(propname):
228261
# we need to create the new table
229262
self.create_multilink_table(spec, propname)
230263
elif old_has(propname):

roundup/hyperdb.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: hyperdb.py,v 1.88 2003-09-04 00:47:01 richard Exp $
18+
# $Id: hyperdb.py,v 1.89 2003-10-07 11:58:57 anthonybaxter Exp $
1919

2020
"""
2121
Hyperdatabase implementation, especially field types.
@@ -188,7 +188,15 @@ def __init__(self, config, journaltag=None):
188188
raise NotImplementedError
189189

190190
def post_init(self):
191-
"""Called once the schema initialisation has finished."""
191+
"""Called once the schema initialisation has finished.
192+
If 'refresh' is true, we want to rebuild the backend
193+
structures.
194+
"""
195+
raise NotImplementedError
196+
197+
def refresh_database(self):
198+
"""Called to indicate that the backend should rebuild all tables
199+
and structures. Not called in normal usage."""
192200
raise NotImplementedError
193201

194202
def __getattr__(self, classname):

0 commit comments

Comments
 (0)