Python django.db.OperationalError() Examples
The following are 30
code examples of django.db.OperationalError().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db
, or try the search function
.
Example #1
Source File: point.py From opentaps_seas with GNU Lesser General Public License v3.0 | 6 votes |
def get_context_data(self, **kwargs): context = super(PointDetailView, self).get_context_data(**kwargs) context['grafana_url'] = settings.GRAFANA_BASE_URL + "/d/" # add the parent Site (optional) try: context['site'] = SiteView.objects.get(object_id=context['object'].site_id) except SiteView.DoesNotExist: pass # add the parent Equipment (optional) try: context['equipment'] = EquipmentView.objects.get(object_id=context['object'].equipment_id) except EquipmentView.DoesNotExist: pass if context['object']: charts = [] try: charts = utils.charts_for_points([context['object']]) except OperationalError: logging.warning('Crate database unavailable') context['charts'] = charts return context
Example #2
Source File: database.py From yawn with MIT License | 6 votes |
def close_on_exception(func): """ A wrapper to close the database connection if a DB error occurs, so that it will get re-opened on the next use. Squashes the exception and logs it. """ @functools.wraps(func) def wrapper(*args, **kwargs): try: func(*args, **kwargs) except OperationalError: logger.error('Database error, closing connection', exc_info=True) db.connection.close() assert db.connection.closed_in_transaction is False, \ 'Could not close connection, probably because this wrapper ' \ 'was used inside an transaction.atomic() block.' return wrapper
Example #3
Source File: test_consumer.py From cadasta-platform with GNU Affero General Public License v3.0 | 6 votes |
def test_process_task_handles_failed_db( self, close_old_connections, handle_task, logger): """ Ensure that process_task() gracefully handles closed DB connections """ for err in (InterfaceError, OperationalError): handle_task.side_effect = err('Broken DB') body = MagicMock() msg = MagicMock() # First time will fail, will log error, and not ack message self.mock_worker.process_task(body, msg) assert not msg.ack.called assert logger.exception.calls[0][0] assert close_old_connections.called close_old_connections.reset_mock()
Example #4
Source File: consumer.py From cadasta-platform with GNU Affero General Public License v3.0 | 6 votes |
def process_task(self, body, message): logger.info('Processing message: %r', message) try: self._handle_task(body, message) except (OperationalError, InterfaceError): # Lost DB connection, close DB and don't ack() msg. # A new DB connection will be re-opened next time we # try to access the DB. Msg will be re-processed # after SQS visibility timeout passes. logger.exception("DB connection lost. Cleaning up connections") return close_old_connections() except: # NOQA logger.exception("Failed to process message: %r", message) logger.info("ACKing message %r", message) if self.connection.as_uri().lower().startswith('sqs://'): # HACK: Can't seem to get message.ack() to work for SQS # backend. Without this hack, messages will keep # re-appearing after the visibility_timeout expires. # See https://github.com/celery/kombu/issues/758 return self._sqs_ack(message) return message.ack()
Example #5
Source File: checks.py From zing with GNU General Public License v3.0 | 6 votes |
def check_users(app_configs=None, **kwargs): from django.contrib.auth import get_user_model errors = [] User = get_user_model() try: admin_user = User.objects.get(username="admin") except (User.DoesNotExist, OperationalError, ProgrammingError): pass else: if admin_user.check_password("admin"): errors.append( checks.Warning( _("The default 'admin' user still has a password set to 'admin'."), hint=_("Remove the 'admin' user or change its password."), id="pootle.W016", ) ) return errors
Example #6
Source File: report_processor.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def process(self): """ Process the current cost usage report. Args: None Returns: (List) List of filenames downloaded. """ try: return self._processor.process() except (InterfaceError, DjangoInterfaceError, OperationalError) as err: raise ReportProcessorDBError(str(err)) except Exception as err: raise ReportProcessorError(str(err))
Example #7
Source File: test_kafka_listener.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def test_process_synchronize_sources_msg_db_error(self, mock_process_message): """Test processing synchronize messages with database errors.""" provider = Sources.objects.create(**self.aws_source) provider.save() future_mock = asyncio.Future() future_mock.set_result("test result") test_queue = queue.PriorityQueue() test_matrix = [ {"test_value": {"operation": "update", "provider": provider}, "side_effect": InterfaceError}, {"test_value": {"operation": "update", "provider": provider}, "side_effect": OperationalError}, ] for i, test in enumerate(test_matrix): mock_process_message.side_effect = test.get("side_effect") with patch("sources.kafka_listener.connection.close") as close_mock: with patch.object(Config, "RETRY_SECONDS", 0): process_synchronize_sources_msg((i, test["test_value"]), test_queue) close_mock.assert_called() for i in range(2): priority, _ = test_queue.get_nowait() self.assertEqual(priority, i)
Example #8
Source File: checks.py From zing with GNU General Public License v3.0 | 6 votes |
def check_revision(app_configs=None, **kwargs): from pootle.core.models import Revision from pootle_store.models import Unit errors = [] revision = Revision.get() try: max_revision = Unit.max_revision() except (OperationalError, ProgrammingError): return errors if revision is None or revision < max_revision: errors.append( checks.Critical( _("Revision is missing or has an incorrect value."), hint=_("Run `revision --restore` to reset the revision counter."), id="pootle.C016", ) ) return errors
Example #9
Source File: storage.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def is_known_source(source_id): """ Check if source exists in database. Args: source_id (Integer) - Platform-Sources identifier Returns: source_exists (Boolean) - True if source is known """ try: Sources.objects.get(source_id=source_id) source_exists = True except Sources.DoesNotExist: source_exists = False except (InterfaceError, OperationalError) as error: LOG.error(f"Accessing Sources resulting in {type(error).__name__}: {error}") raise error return source_exists
Example #10
Source File: storage.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def destroy_source_event(source_id): """ Destroy a Sources database object. Args: source_id (Integer) - Platform-Sources identifier Returns: None """ koku_uuid = None try: source = Sources.objects.get(source_id=source_id) koku_uuid = source.koku_uuid source.delete() LOG.info(f"source.storage.destroy_source_event destroyed Source ID: {source_id}") except Sources.DoesNotExist: LOG.debug("Source ID: %s already removed.", str(source_id)) except (InterfaceError, OperationalError) as error: LOG.error(f"source.storage.destroy_provider_event {type(error).__name__}: {error}") raise error return koku_uuid
Example #11
Source File: models.py From opentaps_seas with GNU Lesser General Public License v3.0 | 6 votes |
def delete_tags_from_crate_entity(row): # we only sync for entity linked to a topic if not row.topic: return try: with connections['crate'].cursor() as c: # make sure the topic is in CrateDB sql = """DELETE {0} WHERE topic = %s;""".format("topic") try: c.execute(sql, [row.topic]) except Exception: # ignore if the entity did not exist pass except OperationalError: logging.warning('Crate database unavailable')
Example #12
Source File: database.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def check_migrations(): """ Check the status of database migrations. The koku API server is responsible for running all database migrations. This method will return the state of the database and whether or not all migrations have been completed. Hat tip to the Stack Overflow contributor: https://stackoverflow.com/a/31847406 Returns: Boolean - True if database is available and migrations have completed. False otherwise. """ try: connection = connections[DEFAULT_DB_ALIAS] connection.prepare_database() executor = MigrationExecutor(connection) targets = executor.loader.graph.leaf_nodes() return not executor.migration_plan(targets) except OperationalError: return False
Example #13
Source File: problems.py From edx-analytics-data-api with GNU Affero General Public License v3.0 | 6 votes |
def get_queryset(self): """Select all the answer distribution response having to do with this usage of the problem.""" problem_id = self.kwargs.get('problem_id') try: queryset = list(ProblemResponseAnswerDistribution.objects.filter(module_id=problem_id).order_by('part_id')) except OperationalError: self.serializer_class = ConsolidatedFirstLastAnswerDistributionSerializer queryset = list(ProblemFirstLastResponseAnswerDistribution.objects.filter( module_id=problem_id).order_by('part_id')) consolidated_rows = [] for _, part in groupby(queryset, lambda x: x.part_id): consolidated_rows += self.consolidate_answers(list(part)) return consolidated_rows
Example #14
Source File: reference_processor.py From oldp with MIT License | 6 votes |
def process_content(self): for content in self.pre_processed_content: # type: Reference try: # First save (some processing steps require ids) content.full_clean() # Validate model content.save() self.call_processing_steps(content) # Save again content.save() logger.debug('Completed: %s' % content) self.doc_counter += 1 self.processed_content.append(content) except (ValidationError, DataError, OperationalError, IntegrityError, ProcessingError) as e: logger.error('Cannot process: %s; %s' % (content, e)) self.processing_errors.append(e) self.doc_failed_counter += 1
Example #15
Source File: case_processor.py From oldp with MIT License | 6 votes |
def process_content_item(self, content: Case) -> Case: try: # First save (some processing steps require ids) # content.full_clean() # Validate model content.save() self.call_processing_steps(content) # Save again content.save() logger.debug('Completed: %s' % content) self.doc_counter += 1 self.processed_content.append(content) except (ValidationError, DataError, OperationalError, IntegrityError, ProcessingError) as e: logger.error('Cannot process case: %s; %s' % (content, e)) self.processing_errors.append(e) self.doc_failed_counter += 1 return content
Example #16
Source File: court_processor.py From oldp with MIT License | 6 votes |
def process_content(self): for content in self.pre_processed_content: # type: Court try: # First save (some processing steps require ids) content.full_clean() # Validate model content.save() self.call_processing_steps(content) # Save again content.save() logger.debug('Completed: %s' % content) self.doc_counter += 1 self.processed_content.append(content) except (ValidationError, DataError, OperationalError, IntegrityError, ProcessingError) as e: logger.error('Cannot process court: %s; %s' % (content, e)) self.processing_errors.append(e) self.doc_failed_counter += 1
Example #17
Source File: test_locks.py From django-mysql with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_write_fails_touching_other_table(self): with pytest.raises(OperationalError) as excinfo: with TableLock(write=[Alphabet]): Customer.objects.create(name="Lizzy") assert excinfo.value.args[0] == 1100 # ER_TABLE_NOT_LOCKED
Example #18
Source File: topic.py From opentaps_seas with GNU Lesser General Public License v3.0 | 5 votes |
def setup(self, request, *args, **kwargs): super().setup(request, *args, **kwargs) try: connections['crate'].ensure_connection() self.crate_conn = True except OperationalError: logging.warning('Crate database unavailable') self.crate_conn = False
Example #19
Source File: admin.py From dissemin with GNU Affero General Public License v3.0 | 5 votes |
def count(self): # We set the timeout in a db transaction to prevent it from # affecting other transactions. with transaction.atomic(), connection.cursor() as cursor: cursor.execute('SET LOCAL statement_timeout TO 400;') try: return super().count except OperationalError: return 9999999999
Example #20
Source File: 0045_crate_topic_migration.py From opentaps_seas with GNU Lesser General Public License v3.0 | 5 votes |
def check_schema(apps, schema_editor): try: with connections['crate'].cursor() as c: try: c.execute("""ALTER TABLE "volttron"."topic" ADD COLUMN "kv_tags" OBJECT (DYNAMIC) AS ( "dis" STRING, "id" STRING );""") except Exception as e: print(e) try: c.execute("""ALTER TABLE "volttron"."topic" ADD COLUMN "m_tags" ARRAY(STRING);""") except Exception as e: print(e) c.close() except OperationalError: print('Crate database unavailable !!!') else: print("!!!!!!!!!!!!") print("This migration only updates the database schema.") print("To migrate the data use the following script:") print("") print("python manage.py runscript copy_crate_entity_tags_to_topics") print("") print("!!!!!!!!!!!!")
Example #21
Source File: error_explorer.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 5 votes |
def retry_for_operational_error( function=None, retries_count: int = 2, cooldown_interval: float = 1.0): """ Allows N retries for specific error - django.db.OperationalError and writes detailed log """ def _dec(run_func): def _caller(*args, **kwargs): ret_value = None for try_num in range(1, retries_count + 1): try: ret_value = run_func(*args, **kwargs) break except Exception as e: known_error = False if isinstance(e, OperationalError) or type(e).__name__ == 'OperationalError': known_error = True if known_error: msg = f'{run_func.__name__}: {try_num} ' + \ f'attempt of {retries_count} failed ({type(e).__name__})' logger.error(msg) print(msg) OperationalErrorExplorer.log_operational_error(e) if try_num < retries_count: sleep(cooldown_interval) continue raise else: raise return ret_value return _caller return _dec(function) if function is not None else _dec
Example #22
Source File: test_locks.py From django-mysql with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_writes_fail_under_read(self): with TableLock(read=[Alphabet]): with pytest.raises(OperationalError) as excinfo: Alphabet.objects.update(a=2) assert "was locked with a READ lock and can't be updated" in str(excinfo.value)
Example #23
Source File: model_bulk_delete.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 5 votes |
def delete_objects(self, where_suffix: str) -> Dict[str, int]: count_deleted = {} # type: Dict[str, int] queries = self.build_delete_all_queries() with connection.cursor() as cursor: cursor.db.autocommit = True for i in range(len(queries)): for iteration in range(self.tries_per_query + 1): try: query = queries[i].format(where_suffix=where_suffix) table = self.deps[i].deps[0].own_table if not self.safe_mode and table in self.unsafe_tables: query = f'ALTER TABLE "{table}" DISABLE TRIGGER ALL;\n' + query query += f'\nALTER TABLE "{table}" ENABLE TRIGGER ALL;' cursor.execute(query) count = cursor.rowcount table_name = self.deps[i].deps[0].own_table count_deleted[table_name] = count except OperationalError as oe: if iteration == self.tries_per_query: raise RuntimeError(f'Error in bulk_delete.delete_objects(). Query:\n{query}') from oe # if it's a deadlock - sleep a bit and the retry time.sleep(0.5) continue except Exception as e: raise RuntimeError(f'Error in bulk_delete.delete_objects(). Query:\n{query}') from e return count_deleted
Example #24
Source File: test_locks.py From django-mysql with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_inherited_model_top_parent_fails(self): AgedCustomer.objects.create(age=99999, name="Methuselah") with pytest.raises(OperationalError) as excinfo: with TableLock(write=[Customer]): # Django automatically follows down to children which aren't # locked Customer.objects.all().delete() assert "was not locked with LOCK TABLES" in str(excinfo.value)
Example #25
Source File: sqlite.py From django-dbbackup with BSD 3-Clause "New" or "Revised" License | 5 votes |
def restore_dump(self, dump): if not self.connection.is_usable(): self.connection.connect() cursor = self.connection.cursor() for line in dump.readlines(): try: cursor.execute(line.decode('UTF-8')) except OperationalError as err: warnings.warn("Error in db restore: {}".format(err)) except IntegrityError as err: warnings.warn("Error in db restore: {}".format(err))
Example #26
Source File: databases.py From django-heartbeat with MIT License | 5 votes |
def execute_sql(connection, stmt): try: cursor = connection.cursor() except OperationalError as e: return {'error': str(e)} cursor.execute(stmt) result = cursor.fetchone()[0] return result
Example #27
Source File: views.py From elmer with MIT License | 5 votes |
def get_home_subjects(): try: home_subjects = Subject.get_subjects() except OperationalError: home_subjects = None return home_subjects
Example #28
Source File: util.py From donation-tracker with Apache License 2.0 | 5 votes |
def LatestEvent(): from tracker.models import Event if Event.objects.exists(): try: return Event.objects.latest() except (Event.DoesNotExist, OperationalError): return None return None
Example #29
Source File: __init__.py From django-dbconn-retry with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self) -> None: _log.debug("[FullErrorTests] patching for setup") self.s_connect = BaseDatabaseWrapper.connect BaseDatabaseWrapper.connect = Mock(side_effect=OperationalError('fail testing')) BaseDatabaseWrapper.connection = property(lambda x: None, lambda x, y: None) # type: ignore
Example #30
Source File: __init__.py From django-dbconn-retry with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_prehook(self) -> None: cb = Mock(name='pre_reconnect_hook') ddr.pre_reconnect.connect(cb) self.assertRaises(OperationalError, connection.ensure_connection) self.assertTrue(cb.called) del connection._connection_retries