def save(self): """ Store the model (possibly updating values if changed) """ try: self._model.save(commit=True) except sqlalchemy.exc.IntegrityError as e: self._model.session.rollback() raise exceptions.IntegrityError(str(e))
def delete_all(self): """ Delete all Comment entries. :raises `~aiida.common.exceptions.IntegrityError`: if all Comments could not be deleted """ from django.db import transaction try: with transaction.atomic(): models.DbComment.objects.all().delete() except Exception as exc: raise exceptions.IntegrityError(f'Could not delete all Comments. Full exception: {exc}')
def save(self): """Store the model instance. :raises `aiida.common.IntegrityError`: if a database integrity error is raised during the save. """ # transactions are needed here for Postgresql: # https://docs.djangoproject.com/en/1.7/topics/db/transactions/#handling-exceptions-within-postgresql-transactions with transaction.atomic(): try: self._model.save() except IntegrityError as exception: raise exceptions.IntegrityError(str(exception))
def save(self): """Store the model instance. .. note:: If one is currently in a transaction, this method is a no-op. :raises `aiida.common.IntegrityError`: if a database integrity error is raised during the save. """ try: commit = not self._in_transaction() self._model.save(commit=commit) except IntegrityError as exception: self._model.session.rollback() raise exceptions.IntegrityError(str(exception))
def verify_uuid_uniqueness(table): """Check whether database table contains rows with duplicate UUIDS. :param table: Database table with uuid column, e.g. 'db_dbnode' :type str: :raises: IntegrityError if table contains rows with duplicate UUIDS. """ duplicates = get_duplicate_uuids(table=table) if duplicates: raise exceptions.IntegrityError( 'Table {table:} contains rows with duplicate UUIDS: run ' '`verdi database integrity detect-duplicate-uuid -t {table:}` to address the problem'.format(table=table) )
def delete_all(self): """ Delete all Log entries. :raises `~aiida.common.exceptions.IntegrityError`: if all Logs could not be deleted """ session = get_scoped_session() try: session.query(models.DbLog).delete() session.commit() except Exception as exc: session.rollback() raise exceptions.IntegrityError( 'Could not delete all Logs. Full exception: {}'.format(exc))
def delete_all(self): """ Delete all Comment entries. :raises `~aiida.common.exceptions.IntegrityError`: if all Comments could not be deleted """ session = get_scoped_session() try: session.query(models.DbComment).delete() session.commit() except Exception as exc: session.rollback() raise exceptions.IntegrityError( f'Could not delete all Comments. Full exception: {exc}')
def _flush(self, fields=None): """Flush the fields of the model to the database. .. note:: If the wrapped model is not actually saved in the database yet, this method is a no-op. :param fields: the model fields whose current value to flush to the database """ if self.is_saved(): try: # Manually append the `mtime` to fields to update, because when using the `update_fields` keyword of the # `save` method, the `auto_now` property of `mtime` column is not triggered. If `update_fields` is None # everything is updated, so we do not have to add anything if fields is not None and self._is_model_field('mtime'): fields.add('mtime') self._model.save(update_fields=fields) except IntegrityError as exception: raise exceptions.IntegrityError(str(exception))