def execute(self, stmt): results = [] changes = 0 errors = 0 try: # If we're about to ignore errors we need to # use a separate transaction for each # statement - otherwise previously successful # executions would get lost trans = self.connection.begin() result = self.connection.execute(stmt) if result.cursor: results = map(lambda row: Item(self.connection, row), result) self.write(results) else: # increase changes based on the returned result # info changes = result.rowcount if self.opts.dry_run: trans.rollback() else: trans.commit() except BaseException as e: trans.rollback() if not self.opts.mute_errors: log_error(e) errors += 1 return (results, changes, errors)
def run(self): try: if self.options is not None and self.options.daemon: log.logger.debug('Executing remotely') return self.executer(*sys.argv) log.logger.debug('Executing locally') return self.execute() except BaseException as e: log.logger.exception(e) if log.logger.getEffectiveLevel() <= logging.DEBUG: # Start post mortem debugging only when debugging is enabled if os.getenv('UNITTEST', 'False') == 'True': raise if self.options.trace: pdb.post_mortem(sys.exc_info()[2]) # pragma: no cover else: # Show the error message if log level is INFO or higher log.log_error(e) # pragma: no cover