1- # $Id: back_sqlite.py,v 1.20 2004-03-22 07:45:39 richard Exp $
1+ # $Id: back_sqlite.py,v 1.21 2004-03-26 05:16:03 richard Exp $
22'''Implements a backend for SQLite.
33
44See https://pysqlite.sourceforge.net/ for pysqlite info
@@ -87,6 +87,8 @@ def add_actor_column(self):
8787 if tables .has_key (classname ):
8888 dbspec = tables [classname ]
8989 self .update_class (spec , dbspec , force = 1 , adding_actor = 1 )
90+ # we've updated - don't try again
91+ tables [classname ] = spec .schema ()
9092
9193 def update_class (self , spec , old_spec , force = 0 , adding_actor = 0 ):
9294 ''' Determine the differences between the current spec and the
@@ -106,11 +108,12 @@ def update_class(self, spec, old_spec, force=0, adding_actor=0):
106108 return 0
107109
108110 if __debug__ :
109- print >> hyperdb .DEBUG , 'update_class FIRING'
111+ print >> hyperdb .DEBUG , 'update_class FIRING for' , spec . classname
110112
111113 # detect multilinks that have been removed, and drop their table
112114 old_has = {}
113- for name ,prop in old_spec [1 ]:
115+ for name , prop in old_spec [1 ]:
116+ print (name , prop )
114117 old_has [name ] = 1
115118 if new_has (name ) or not isinstance (prop , hyperdb .Multilink ):
116119 continue
@@ -132,9 +135,31 @@ def update_class(self, spec, old_spec, force=0, adding_actor=0):
132135 for propname ,x in new_spec [1 ]:
133136 prop = properties [propname ]
134137 if isinstance (prop , hyperdb .Multilink ):
135- if force or not old_has (propname ):
138+ if not old_has (propname ):
136139 # we need to create the new table
137140 self .create_multilink_table (spec , propname )
141+ elif force :
142+ tn = '%s_%s' % (spec .classname , propname )
143+ # grabe the current values
144+ sql = 'select linkid, nodeid from %s' % tn
145+ if __debug__ :
146+ print >> hyperdb .DEBUG , 'update_class' , (self , sql )
147+ self .cursor .execute (sql )
148+ rows = self .cursor .fetchall ()
149+
150+ # drop the old table
151+ self .drop_multilink_table_indexes (spec .classname , propname )
152+ sql = 'drop table %s' % tn
153+ if __debug__ :
154+ print >> hyperdb .DEBUG , 'migration' , (self , sql )
155+ self .cursor .execute (sql )
156+
157+ # re-create and populate the new table
158+ self .create_multilink_table (spec , propname )
159+ sql = '''insert into %s (linkid, nodeid) values
160+ (%s, %s)''' % (tn , self .arg , self .arg )
161+ for linkid , nodeid in rows :
162+ self .cursor .execute (sql , (int (linkid ), int (nodeid )))
138163 elif old_has (propname ):
139164 # we copy this col over from the old table
140165 fetch .append ('_' + propname )
@@ -154,6 +179,8 @@ def update_class(self, spec, old_spec, force=0, adding_actor=0):
154179 self .drop_class_table_indexes (cn , old_spec [0 ])
155180
156181 # drop the old table
182+ if __debug__ :
183+ print >> hyperdb .DEBUG , 'update_class "drop table _%s"' % cn
157184 self .cursor .execute ('drop table _%s' % cn )
158185
159186 # create the new table
0 commit comments