comparison roundup/backends/rdbms_common.py @ 6431:ada1edcc9132

issue2551142 - Import ... unique constraint failure. Full title: Import of retired node with username after active node fails with unique constraint failure. Fix this in two ways: 1) sort export on keyname, retired status so that retired nodes for a given keyname are before the acive node in the export file. This stops generating a broken export. 2) handle importing a broken export by deactivating/fixing up/clearing the active record's unique index entry temporarily. Redo the import of the retired node and resetting the active record to active. The fixup changes the unique index (keyvalue, __retired__) from (keyvalue, 0) to (keyvalue, -1). Then it retries the failed import of a retired record with keyvalue. I use -1 in case something goes wrong, It makes the record stand out in the database allowing hand recovery if needed. Rather than using -1 I could just use the id of the record like a normal retirement does. If the retry of the import fails (raises exception), reset the active record from -1 back to 0 and raise the exception. If it succeeds, reset the active record from -1 back to 0 and continue the import process. Reset __retired__ from -1 to 0 on every import. I don't think the performance loss from resetting on every exception matters as there should be very few exceptions. Also this makes the code more understandable. There is no reason to leave the -1 value in place and do a bulk rest of -1 to 0 after the class csv file is loaded. Also if a fixup is needed it is logged at level info with the rest of the database logging. Also success of the fixup is logged. Fixup failure generates a propagated exception.
author John Rouillard <rouilj@ieee.org>
date Mon, 07 Jun 2021 09:58:39 -0400
parents dbacf6bf2a2f
children c1d3fbcdbfbd
comparison
equal deleted inserted replaced
6430:ff4ab763f47c 6431:ada1edcc9132
3111 journal should be initialised using the "creator" and "created" 3111 journal should be initialised using the "creator" and "created"
3112 information. 3112 information.
3113 3113
3114 Return the nodeid of the node imported. 3114 Return the nodeid of the node imported.
3115 """ 3115 """
3116
3117 logger = logging.getLogger('roundup.hyperdb.backend')
3118
3116 if self.db.journaltag is None: 3119 if self.db.journaltag is None:
3117 raise DatabaseError(_('Database open read-only')) 3120 raise DatabaseError(_('Database open read-only'))
3118 properties = self.getprops() 3121 properties = self.getprops()
3119 3122
3120 # make the new node's property map 3123 # make the new node's property map
3166 3169
3167 # get a new id if necessary 3170 # get a new id if necessary
3168 if newid is None: 3171 if newid is None:
3169 newid = self.db.newid(self.classname) 3172 newid = self.db.newid(self.classname)
3170 3173
3174 activeid = None
3175 has_node = False
3176
3177 # use the arg for __retired__ to cope with any odd database type
3178 # conversion (hello, sqlite)
3179 retired_sql = 'update _%s set __retired__=%s where id=%s'%(
3180 self.classname, self.db.arg, self.db.arg)
3181
3171 # insert new node or update existing? 3182 # insert new node or update existing?
3172 if not self.hasnode(newid): 3183 # if integrity error raised try to recover
3173 self.db.addnode(self.classname, newid, d) # insert 3184 try:
3174 else: 3185 has_node = self.hasnode(newid)
3175 self.db.setnode(self.classname, newid, d) # update 3186 if not has_node:
3187 self.db.addnode(self.classname, newid, d) # insert
3188 else:
3189 self.db.setnode(self.classname, newid, d) # update
3190 # Blech, different db's return different exceptions
3191 # so I can't list them here as some might not be defined
3192 # on a given system. So capture all exceptions from the
3193 # code above and try to correct it. If it's correctable its
3194 # some form of Uniqueness Failure/Integrity Error otherwise
3195 # undo the fixup and pass on the error.
3196 except Exception as e:
3197 logger.info('Attempting to handle import exception '
3198 'for id %s: %s' % (newid,e))
3199
3200 keyname = self.db.user.getkey()
3201 if has_node or not keyname: # Not an integrity error
3202 raise
3203 activeid = self.db.user.lookup(d[keyname])
3204 self.db.sql(retired_sql, (-1, activeid)) # clear the active node
3205 # this can only happen on an addnode, so retry
3206 try:
3207 # if this raises an error, let it propagate upward
3208 self.db.addnode(self.classname, newid, d) # insert
3209 except Exception:
3210 # undo the database change
3211 self.db.sql(retired_sql, (0, activeid)) # clear the active node
3212 raise # propagate
3213 logger.info('Successfully handled import exception '
3214 'for id %s which conflicted with %s' % (
3215 newid, activeid))
3176 3216
3177 # retire? 3217 # retire?
3178 if retire: 3218 if retire:
3179 # use the arg for __retired__ to cope with any odd database type 3219 self.db.sql(retired_sql, (newid, newid))
3180 # conversion (hello, sqlite) 3220
3181 sql = 'update _%s set __retired__=%s where id=%s'%(self.classname, 3221 if activeid:
3182 self.db.arg, self.db.arg) 3222 # unretire the active node
3183 self.db.sql(sql, (newid, newid)) 3223 self.db.sql(retired_sql, ('0', activeid))
3224
3184 return newid 3225 return newid
3185 3226
3186 def export_journals(self): 3227 def export_journals(self):
3187 """Export a class's journal - generate a list of lists of 3228 """Export a class's journal - generate a list of lists of
3188 CSV-able data: 3229 CSV-able data:

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