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
44Basics:
@@ -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 ):
0 commit comments