comparison roundup/backends/back_mysql.py @ 6433:c1d3fbcdbfbd

issue2551142 - Import of retired node ... unique constraint failure. Title: Import of retired node with username after active node fails with unique constraint failure. More fixes needed for mysql and postgresql. mysql: add unique constraint for (keyvalue, __retired__) when creating class in the database. On schema change if class is changed, remove the unique constraint too. upgrade version of rdbms database from 5 to 6 to add constraint to all version 5 databases that were created as version 5 and didn't get the unique constraint. Make no changes on version 5 databases upgraded from version 4, the upgrade process to 5 added the constraint. Make no changes to other databases (sqlite, postgres) during upgrade from version 5 to 6. postgres: Handle the exception raised on unique constraint violation. The exception invalidates the database connection so it can't be used to recover from the exception. Added two new database methods: checkpoint_data - performs a db.commit under postgres does nothing on other backends restore_connection_on_error - does a db.rollback on postgres, does nothing on other backends with the rollback() done on the connection I can use the database connection to fixup the import that failed on the unique constraint. This makes postgres slower but without the commit after every imported object, the rollback will delete all the entries done up to this point. Trying to figure out how to make the caller do_import batch and recover from this failure is beyond me. Also dismissed having to process the export csv file before importing. Pushing that onto a user just seems wrong. Also since import/export isn't frequently done the lack of surprise on having a failing import and reduced load/frustration for the user seems worth it. Also the import can be run in verbose mode where it prints out a row as it is processed, so it may take a while, ut the user can get feedback. db_test-base.py: add test for upgrade from 5 to 6.
author John Rouillard <rouilj@ieee.org>
date Thu, 10 Jun 2021 12:52:05 -0400
parents 75a53956cf13
children e70e2789bc2c
comparison
equal deleted inserted replaced
6432:97a45bfa62a8 6433:c1d3fbcdbfbd
393 self._convert_journal_tables() 393 self._convert_journal_tables()
394 394
395 # Convert all String properties to TEXT 395 # Convert all String properties to TEXT
396 self._convert_string_properties() 396 self._convert_string_properties()
397 397
398 def fix_version_5_tables(self):
399 # A bug caused the _<class>_key_retired_idx to be missing
400 # unless the database was upgraded from version 4 to 5.
401 # If it was created at version 5, the index is missing.
402 # The user class is always present and has a key.
403 # Check it for the index. If missing, add index to all
404 # classes by rerunning self.fix_version_4_tables().
405
406 # if this fails abort. Probably means no user class
407 # so we should't be doing anything.
408 if not self.sql_index_exists("_user", "_user_key_retired_idx"):
409 self.fix_version_4_tables()
410 else:
411 self.log_info('No changes needed.')
412
398 def __repr__(self): 413 def __repr__(self):
399 return '<myroundsql 0x%x>'%id(self) 414 return '<myroundsql 0x%x>'%id(self)
400 415
401 def sql_fetchone(self): 416 def sql_fetchone(self):
402 return self.cursor.fetchone() 417 return self.cursor.fetchone()
443 idx = spec.key 458 idx = spec.key
444 index_sql3 = 'create index _%s_%s_idx on _%s(_%s)'%( 459 index_sql3 = 'create index _%s_%s_idx on _%s(_%s)'%(
445 spec.classname, spec.key, 460 spec.classname, spec.key,
446 spec.classname, idx) 461 spec.classname, idx)
447 self.sql(index_sql3) 462 self.sql(index_sql3)
463
464 # and the unique index for key / retired(id)
465 self.add_class_key_required_unique_constraint(spec.classname,
466 spec.key)
448 467
449 # TODO: create indexes on (selected?) Link property columns, as 468 # TODO: create indexes on (selected?) Link property columns, as
450 # they're more likely to be used for lookup 469 # they're more likely to be used for lookup
451 470
452 def add_class_key_required_unique_constraint(self, cn, key): 471 def add_class_key_required_unique_constraint(self, cn, key):
527 index_name = '_%s_%s_idx'%(cn, key) 546 index_name = '_%s_%s_idx'%(cn, key)
528 if not self.sql_index_exists(table_name, index_name): 547 if not self.sql_index_exists(table_name, index_name):
529 return 548 return
530 sql = 'drop index %s on %s'%(index_name, table_name) 549 sql = 'drop index %s on %s'%(index_name, table_name)
531 self.sql(sql) 550 self.sql(sql)
551
552 # and now the retired unique index too
553 index_name = '_%s_key_retired_idx' % cn
554 if self.sql_index_exists(table_name, index_name):
555 sql = 'drop index %s on _%s'%(index_name, cn)
556 self.sql(sql)
532 557
533 # old-skool id generation 558 # old-skool id generation
534 def newid(self, classname): 559 def newid(self, classname):
535 ''' Generate a new id for the given class 560 ''' Generate a new id for the given class
536 ''' 561 '''

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