comparison roundup/backends/back_sqlite.py @ 2137:c49495585c44

Fix for sqlite backend migration. Change Cookie -> SimpleCookie
author Richard Jones <richard@users.sourceforge.net>
date Fri, 26 Mar 2004 05:16:03 +0000
parents 18addf2a8596
children 6c1caf81a108
comparison
equal deleted inserted replaced
2136:ee3cf6a44f29 2137:c49495585c44
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 $
2 '''Implements a backend for SQLite. 2 '''Implements a backend for SQLite.
3 3
4 See https://pysqlite.sourceforge.net/ for pysqlite info 4 See https://pysqlite.sourceforge.net/ for pysqlite info
5 5
6 6
85 tables = self.database_schema['tables'] 85 tables = self.database_schema['tables']
86 for classname, spec in self.classes.items(): 86 for classname, spec in self.classes.items():
87 if tables.has_key(classname): 87 if tables.has_key(classname):
88 dbspec = tables[classname] 88 dbspec = tables[classname]
89 self.update_class(spec, dbspec, force=1, adding_actor=1) 89 self.update_class(spec, dbspec, force=1, adding_actor=1)
90 # we've updated - don't try again
91 tables[classname] = spec.schema()
90 92
91 def update_class(self, spec, old_spec, force=0, adding_actor=0): 93 def update_class(self, spec, old_spec, force=0, adding_actor=0):
92 ''' Determine the differences between the current spec and the 94 ''' Determine the differences between the current spec and the
93 database version of the spec, and update where necessary. 95 database version of the spec, and update where necessary.
94 96
104 if not force and new_spec == old_spec: 106 if not force and new_spec == old_spec:
105 # no changes 107 # no changes
106 return 0 108 return 0
107 109
108 if __debug__: 110 if __debug__:
109 print >>hyperdb.DEBUG, 'update_class FIRING' 111 print >>hyperdb.DEBUG, 'update_class FIRING for', spec.classname
110 112
111 # detect multilinks that have been removed, and drop their table 113 # detect multilinks that have been removed, and drop their table
112 old_has = {} 114 old_has = {}
113 for name,prop in old_spec[1]: 115 for name, prop in old_spec[1]:
116 print (name, prop)
114 old_has[name] = 1 117 old_has[name] = 1
115 if new_has(name) or not isinstance(prop, hyperdb.Multilink): 118 if new_has(name) or not isinstance(prop, hyperdb.Multilink):
116 continue 119 continue
117 # it's a multilink, and it's been removed - drop the old 120 # it's a multilink, and it's been removed - drop the old
118 # table. First drop indexes. 121 # table. First drop indexes.
130 fetch = ['_actor', '_activity', '_creation', '_creator'] 133 fetch = ['_actor', '_activity', '_creation', '_creator']
131 properties = spec.getprops() 134 properties = spec.getprops()
132 for propname,x in new_spec[1]: 135 for propname,x in new_spec[1]:
133 prop = properties[propname] 136 prop = properties[propname]
134 if isinstance(prop, hyperdb.Multilink): 137 if isinstance(prop, hyperdb.Multilink):
135 if force or not old_has(propname): 138 if not old_has(propname):
136 # we need to create the new table 139 # we need to create the new table
137 self.create_multilink_table(spec, propname) 140 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)))
138 elif old_has(propname): 163 elif old_has(propname):
139 # we copy this col over from the old table 164 # we copy this col over from the old table
140 fetch.append('_'+propname) 165 fetch.append('_'+propname)
141 166
142 # select the data out of the old table 167 # select the data out of the old table
152 177
153 # TODO: update all the other index dropping code 178 # TODO: update all the other index dropping code
154 self.drop_class_table_indexes(cn, old_spec[0]) 179 self.drop_class_table_indexes(cn, old_spec[0])
155 180
156 # drop the old table 181 # drop the old table
182 if __debug__:
183 print >>hyperdb.DEBUG, 'update_class "drop table _%s"'%cn
157 self.cursor.execute('drop table _%s'%cn) 184 self.cursor.execute('drop table _%s'%cn)
158 185
159 # create the new table 186 # create the new table
160 self.create_class_table(spec) 187 self.create_class_table(spec)
161 188

Roundup Issue Tracker: http://roundup-tracker.org/