Python django.db.DatabaseError() Examples

The following are 30 code examples of django.db.DatabaseError(). 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: managers.py    From pasportaservo with GNU Affero General Public License v3.0 7 votes vote down vote up
def get_queryset(self):
        try:
            validity_period = SiteConfiguration.get_solo().confirmation_validity_period
        except DatabaseError:
            from datetime import timedelta
            validity_period = timedelta(weeks=42)
        validity_start = timezone.now() - validity_period
        return super().get_queryset().annotate(deleted=Case(
            When(deleted_on__isnull=True, then=False),
            default=True,
            output_field=BooleanField()
        )).annotate(confirmed=Case(
            When(confirmed_on__isnull=True, then=False),
            When(confirmed_on__lt=validity_start, then=False),
            default=True,
            output_field=BooleanField()
        )).annotate(checked=Case(
            When(checked_on__isnull=True, then=False),
            # When(checked_on__lt=validity_start, then=False),  # Temporarily disabled.
            default=True,
            output_field=BooleanField()
        )).select_related() 
Example #2
Source File: views.py    From mangaki with GNU Affero General Public License v3.0 7 votes vote down vote up
def generic_error_view(error, error_code):
    def error_view(request, exception=None):
        try:
            trope = Trope.objects.order_by('?').first()
        except DatabaseError:
            return server_error(request)

        parameters = {
            'error_code': error_code,
            'error': error,
        }
        if trope:
            parameters['trope'] = trope
            parameters['origin'] = trope.origin
        return render(request, 'error.html', parameters, status=error_code)

    return error_view 
Example #3
Source File: auth.py    From django-cas-server with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, username):
        if "cas_server" not in connections:
            raise RuntimeError("Please configure the 'cas_server' database in settings.DATABASES")
        for retry_nb in range(3):
            try:
                with connections["cas_server"].cursor() as curs:
                    curs.execute(settings.CAS_SQL_USER_QUERY, (username,))
                    results = dictfetchall(curs)
                    if len(results) == 1:
                        self.user = results[0]
                        super(SqlAuthUser, self).__init__(self.user['username'])
                    else:
                        super(SqlAuthUser, self).__init__(username)
                break
            except DatabaseError:
                connections["cas_server"].close()
                if retry_nb == 2:
                    raise 
Example #4
Source File: db_before_090.py    From govready-q with GNU General Public License v3.0 6 votes vote down vote up
def handle(self, *args, **options):
    site_app_migration_exists = MigrationRecorder.Migration.objects.filter(app='siteapp', name='0025_auto_20190515_1455')
    guardian_migration_exists = MigrationRecorder.Migration.objects.filter(app='guardian', name='0001_initial')
    system_settings_exists = MigrationRecorder.Migration.objects.filter(app='system_settings', name='0002_auto_20190808_1947')

    # Assume case of existing Database initialized and in state prior to 0.9.0
    DB_BEFORE_090 = "True"

    try:
      if site_app_migration_exists:
        DB_BEFORE_090 = "False"
      if guardian_migration_exists:
        DB_BEFORE_090 = "False"
      if system_settings_exists:
        DB_BEFORE_090 = "False"
    except DatabaseError:
        # Treat case of database not initialized as OK to run 0.9.0 migrations
        DB_BEFORE_090 = "False"

    print(DB_BEFORE_090) 
Example #5
Source File: db.py    From python2017 with MIT License 6 votes vote down vote up
def save(self, must_create=False):
        """
        Saves the current session data to the database. If 'must_create' is
        True, a database error will be raised if the saving operation doesn't
        create a *new* entry (as opposed to possibly updating an existing
        entry).
        """
        if self.session_key is None:
            return self.create()
        data = self._get_session(no_load=must_create)
        obj = self.create_model_instance(data)
        using = router.db_for_write(self.model, instance=obj)
        try:
            with transaction.atomic(using=using):
                obj.save(force_insert=must_create, force_update=not must_create, using=using)
        except IntegrityError:
            if must_create:
                raise CreateError
            raise
        except DatabaseError:
            if not must_create:
                raise UpdateError
            raise 
Example #6
Source File: views.py    From edx-analytics-dashboard with GNU Affero General Public License v3.0 6 votes vote down vote up
def health(_request):
    if newrelic:  # pragma: no cover
        newrelic.agent.ignore_transaction()
    overall_status = database_status = UNAVAILABLE

    try:
        cursor = connection.cursor()
        cursor.execute("SELECT 1")
        cursor.fetchone()
        cursor.close()
        database_status = OK
    except DatabaseError as e:
        logger.exception('Insights database is not reachable: %s', e)
        database_status = UNAVAILABLE

    overall_status = OK if (database_status == OK) else UNAVAILABLE

    data = {
        'overall_status': overall_status,
        'detailed_status': {
            'database_connection': database_status,
        }
    }

    return HttpResponse(json.dumps(data), content_type='application/json', status=200 if overall_status == OK else 503) 
Example #7
Source File: clearmapcache.py    From c3nav with Apache License 2.0 6 votes vote down vote up
def handle(self, *args, **options):
        from c3nav.mapdata.models import MapUpdate

        logger = logging.getLogger('c3nav')

        MapUpdate.objects.create(type='management', geometries_changed=options['include_geometries'])
        logger.info('New management update created.')

        if options['include_history']:
            logger.info('Deleting base history...')
            for filename in os.listdir(settings.CACHE_ROOT):
                if filename.startswith('history_base_'):
                    logger.info('Deleting %s...' % filename)
                    os.remove(os.path.join(settings.CACHE_ROOT, filename))
            logger.info('Base history deleted.')

        if not settings.HAS_CELERY and not options['no_process']:
            print(_('You don\'t have celery installed, so we will run processupdates now...'))
            try:
                process_map_updates()
            except DatabaseError:
                logger.error('Didn\'t work, there is already map update processing in progress.')

        if not settings.HAS_REAL_CACHE:
            print(_('You have no external cache configured, so don\'t forget to restart your c3nav instance!')) 
Example #8
Source File: create_roles.py    From doccano with MIT License 6 votes vote down vote up
def handle(self, *args, **options):
        try:
            role_names = [settings.ROLE_PROJECT_ADMIN, settings.ROLE_ANNOTATOR, settings.ROLE_ANNOTATION_APPROVER]
        except KeyError as key_error:
            self.stderr.write(self.style.ERROR(f'Missing Key: "{key_error}"'))
        for role_name in role_names:
            if Role.objects.filter(name=role_name).exists():
                continue
            role = Role()
            role.name = role_name
            try:
                role.save()
            except DatabaseError as db_error:
                self.stderr.write(self.style.ERROR(f'Database Error: "{db_error}"'))
            else:
                self.stdout.write(self.style.SUCCESS(f'Role created successfully "{role_name}"')) 
Example #9
Source File: django_tests.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_user_info_raises_database_error(django_elasticapm_client, client):
    user = User(username="admin", email="admin@example.com")
    user.set_password("admin")
    user.save()

    assert client.login(username="admin", password="admin")

    with mock.patch("django.contrib.auth.models.User.is_authenticated") as is_authenticated:
        is_authenticated.side_effect = DatabaseError("Test Exception")
        with pytest.raises(Exception):
            client.get(reverse("elasticapm-raise-exc"))

    assert len(django_elasticapm_client.events[ERROR]) == 1
    event = django_elasticapm_client.events[ERROR][0]
    assert "user" in event["context"]
    user_info = event["context"]["user"]
    assert user_info == {} 
Example #10
Source File: views.py    From developer-portal with Mozilla Public License 2.0 6 votes vote down vote up
def readiness(request):
    """
    A successful response from this endpoint goes a step further
    and means not only that Django is up and running, but also that
    the database can be successfully used from within this service.
    """
    try:
        # Confirm that we can use the database by making a fast query
        # against the Article table. It's not important that the requested
        # primary key exists or not, just that the query completes without
        # error.
        Article.objects.filter(pk=1).exists()
    except DatabaseError as e:
        reason_tmpl = "service unavailable due to database issue ({!s})"
        status, reason = 503, reason_tmpl.format(e)
    else:
        status, reason = 204, None
    return HttpResponse(status=status, reason=reason) 
Example #11
Source File: server.py    From Inboxen with GNU Affero General Public License v3.0 6 votes vote down vote up
def process_message(message, inbox=None, domain=None):
    try:
        with transaction.atomic():
            inbox = Inbox.objects.filter(inbox=inbox, domain__domain=domain)
            inbox = inbox.select_related("user", "user__inboxenprofile").receiving()
            inbox = inbox.get()

            make_email(message, inbox)

            inbox.new = True
            inbox.save(update_fields=["new"])

            if not inbox.exclude_from_unified:
                profile = inbox.user.inboxenprofile
                profile.unified_has_new_messages = True
                profile.save(update_fields=["unified_has_new_messages"])
    except DatabaseError as e:
        log.exception("DB error: %s", e)
        raise SMTPError(451, "Error processing message, try again later.")
    except Inbox.DoesNotExist:
        raise SMTPError(550, "No such address") 
Example #12
Source File: db.py    From Hands-On-Application-Development-with-PyCharm with MIT License 6 votes vote down vote up
def save(self, must_create=False):
        """
        Save the current session data to the database. If 'must_create' is
        True, raise a database error if the saving operation doesn't create a
        new entry (as opposed to possibly updating an existing entry).
        """
        if self.session_key is None:
            return self.create()
        data = self._get_session(no_load=must_create)
        obj = self.create_model_instance(data)
        using = router.db_for_write(self.model, instance=obj)
        try:
            with transaction.atomic(using=using):
                obj.save(force_insert=must_create, force_update=not must_create, using=using)
        except IntegrityError:
            if must_create:
                raise CreateError
            raise
        except DatabaseError:
            if not must_create:
                raise UpdateError
            raise 
Example #13
Source File: models.py    From ahmia-site with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def add_or_increment(self, **kwargs):
        """
        Handles Metric table updates:
        If object does not exist create it, else update the
        counter (occurrences) of same instances in the table

        :param kwargs: A Dict containing the attributes that identify the obj
        :return the object that was created or updated
        """

        try:
            obj, created = self.get_or_create(**kwargs)
            if not created:
                obj.occurrences += 1
            obj.save()

        except DatabaseError as e:
            logger.exception(e)
            obj = None
            # stats shouldn't disrupt website functionality

        return obj 
Example #14
Source File: layer_publishing.py    From urbanfootprint with GNU General Public License v3.0 6 votes vote down vote up
def delete_layer_selections(layers):
    for config_entity in unique(map(lambda layer: layer.config_entity, layers)):
        FeatureClassCreator(config_entity).ensure_dynamic_models()
    for selection_layer in layers:
        try:
            # Drop the table
            layer_selection_class = get_or_create_layer_selection_class_for_layer(selection_layer, no_table_creation=True)

            if layer_selection_class:
                if hasattr(layer_selection_class.features, 'through'):
                    layer_selection_features_class = layer_selection_class.features.through
                    drop_layer_selection_table(layer_selection_features_class)
                drop_layer_selection_table(layer_selection_class)

        except DatabaseError, e:
            logger.warning(
                "Couldn't destroy LayerSelection tables. Maybe the public.layer table no longer exists: %s" % e.message) 
Example #15
Source File: exceptions.py    From CTF_AWD_Platform with MIT License 6 votes vote down vote up
def exception_handler(exc, context):
    """
    自定义异常处理
    :param exc: 别的地方抛的异常就会传给exc
    :param context: 字典形式。抛出异常的上下文(即抛出异常的出处;即抛出异常的视图)
    :return: Response响应对象
    """
    # 调用drf框架原生的异常处理方法,把异常和异常出处交给他处理,如果是序列化器异常就直接处理,处理之后就直接返回
    response = drf_exception_handler(exc, context)
	#如果响应为空表示不是序列化器异常,补充数据库异常
    if response is None:
        view = context['view']
        if isinstance(exc, DatabaseError) or isinstance(exc, RedisError):
            # 数据库异常
            logger.error('[%s] %s' % (view, exc))
            response = Response({'message': '服务器内部错误'}, status=status.HTTP_507_INSUFFICIENT_STORAGE)

    return response 
Example #16
Source File: apps.py    From online-judge with GNU Affero General Public License v3.0 6 votes vote down vote up
def ready(self):
        # WARNING: AS THIS IS NOT A FUNCTIONAL PROGRAMMING LANGUAGE,
        #          OPERATIONS MAY HAVE SIDE EFFECTS.
        #          DO NOT REMOVE THINKING THE IMPORT IS UNUSED.
        # noinspection PyUnresolvedReferences
        from . import signals, jinja2  # noqa: F401, imported for side effects

        from judge.models import Language, Profile
        from django.contrib.auth.models import User

        try:
            lang = Language.get_default_language()
            for user in User.objects.filter(profile=None):
                # These poor profileless users
                profile = Profile(user=user, language=lang)
                profile.save()
        except DatabaseError:
            pass 
Example #17
Source File: models.py    From django-sitemessage with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_dispatches_for_update(filter_kwargs: dict) -> Optional[List['Dispatch']]:
    """Distributed friendly version using ``select for update``."""

    dispatches = Dispatch.objects.prefetch_related('message').filter(
        **filter_kwargs

    ).select_for_update(
        **GET_DISPATCHES_ARGS[1]

    ).order_by('-message__time_created')

    try:
        dispatches = list(dispatches)

    except NotSupportedError:
        return None

    except DatabaseError:  # Probably locked. That's fine.
        return []

    return dispatches 
Example #18
Source File: db.py    From bioforum with MIT License 6 votes vote down vote up
def save(self, must_create=False):
        """
        Save the current session data to the database. If 'must_create' is
        True, raise a database error if the saving operation doesn't create a
        new entry (as opposed to possibly updating an existing entry).
        """
        if self.session_key is None:
            return self.create()
        data = self._get_session(no_load=must_create)
        obj = self.create_model_instance(data)
        using = router.db_for_write(self.model, instance=obj)
        try:
            with transaction.atomic(using=using):
                obj.save(force_insert=must_create, force_update=not must_create, using=using)
        except IntegrityError:
            if must_create:
                raise CreateError
            raise
        except DatabaseError:
            if not must_create:
                raise UpdateError
            raise 
Example #19
Source File: test_debug.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_handle_db_exception(self):
        """
        Ensure the debug view works when a database exception is raised by
        performing an invalid query and passing the exception to the debug view.
        """
        with connection.cursor() as cursor:
            try:
                cursor.execute('INVALID SQL')
            except DatabaseError:
                exc_info = sys.exc_info()

        rf = RequestFactory()
        response = technical_500_response(rf.get('/'), *exc_info)
        self.assertContains(response, 'OperationalError at /', status_code=500) 
Example #20
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_force_update(self):
        c = Counter.objects.create(name="one", value=1)

        # The normal case
        c.value = 2
        c.save()
        # Same thing, via an update
        c.value = 3
        c.save(force_update=True)

        # Won't work because force_update and force_insert are mutually
        # exclusive
        c.value = 4
        with self.assertRaises(ValueError):
            c.save(force_insert=True, force_update=True)

        # Try to update something that doesn't have a primary key in the first
        # place.
        c1 = Counter(name="two", value=2)
        with self.assertRaises(ValueError):
            with transaction.atomic():
                c1.save(force_update=True)
        c1.save(force_insert=True)

        # Won't work because we can't insert a pk of the same value.
        c.value = 5
        with self.assertRaises(IntegrityError):
            with transaction.atomic():
                c.save(force_insert=True)

        # Trying to update should still fail, even with manual primary keys, if
        # the data isn't in the database already.
        obj = WithCustomPK(name=1, value=1)
        with self.assertRaises(DatabaseError):
            with transaction.atomic():
                obj.save(force_update=True) 
Example #21
Source File: views.py    From credentials with GNU Affero General Public License v3.0 5 votes vote down vote up
def health(_):
    """Allows a load balancer to verify this service is up.

    Checks the status of the database connection on which this service relies.

    Returns:
        HttpResponse: 200 if the service is available, with JSON data indicating the health of each required service
        HttpResponse: 503 if the service is unavailable, with JSON data indicating the health of each required service

    Example:
        >>> response = requests.get('https://credentials.edx.org/health')
        >>> response.status_code
        200
        >>> response.content
        '{"overall_status": "OK", "detailed_status": {"database_status": "OK", "lms_status": "OK"}}'
    """
    if newrelic:  # pragma: no cover
        newrelic.agent.ignore_transaction()
    try:
        cursor = connection.cursor()
        cursor.execute("SELECT 1")
        cursor.fetchone()
        cursor.close()
        database_status = Status.OK
    except DatabaseError:
        database_status = Status.UNAVAILABLE

    overall_status = Status.OK if (database_status == Status.OK) else Status.UNAVAILABLE

    data = {
        'overall_status': overall_status,
        'detailed_status': {
            'database_status': database_status,
        },
    }

    if overall_status == Status.OK:
        return JsonResponse(data)
    return JsonResponse(data, status=503) 
Example #22
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_select_on_save_lying_update(self):
        """
        select_on_save works correctly if the database doesn't return correct
        information about matched rows from UPDATE.
        """
        # Change the manager to not return "row matched" for update().
        # We are going to change the Article's _base_manager class
        # dynamically. This is a bit of a hack, but it seems hard to
        # test this properly otherwise. Article's manager, because
        # proxy models use their parent model's _base_manager.

        orig_class = Article._base_manager._queryset_class

        class FakeQuerySet(QuerySet):
            # Make sure the _update method below is in fact called.
            called = False

            def _update(self, *args, **kwargs):
                FakeQuerySet.called = True
                super()._update(*args, **kwargs)
                return 0

        try:
            Article._base_manager._queryset_class = FakeQuerySet
            asos = ArticleSelectOnSave.objects.create(pub_date=datetime.now())
            with self.assertNumQueries(3):
                asos.save()
                self.assertTrue(FakeQuerySet.called)
            # This is not wanted behavior, but this is how Django has always
            # behaved for databases that do not return correct information
            # about matched rows for UPDATE.
            with self.assertRaisesMessage(DatabaseError, 'Forced update did not affect any rows.'):
                asos.save(force_update=True)
            msg = (
                "An error occurred in the current transaction. You can't "
                "execute queries until the end of the 'atomic' block."
            )
            with self.assertRaisesMessage(DatabaseError, msg):
                asos.save(update_fields=['pub_date'])
        finally:
            Article._base_manager._queryset_class = orig_class 
Example #23
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_nodb_connection(self):
        """
        The _nodb_connection property fallbacks to the default connection
        database when access to the 'postgres' database is not granted.
        """
        def mocked_connect(self):
            if self.settings_dict['NAME'] is None:
                raise DatabaseError()
            return ''

        nodb_conn = connection._nodb_connection
        self.assertIsNone(nodb_conn.settings_dict['NAME'])

        # Now assume the 'postgres' db isn't available
        msg = (
            "Normally Django will use a connection to the 'postgres' database "
            "to avoid running initialization queries against the production "
            "database when it's not needed (for example, when running tests). "
            "Django was unable to create a connection to the 'postgres' "
            "database and will use the first PostgreSQL database instead."
        )
        with self.assertWarnsMessage(RuntimeWarning, msg):
            with mock.patch('django.db.backends.base.base.BaseDatabaseWrapper.connect',
                            side_effect=mocked_connect, autospec=True):
                with mock.patch.object(
                    connection,
                    'settings_dict',
                    {**connection.settings_dict, 'NAME': 'postgres'},
                ):
                    nodb_conn = connection._nodb_connection
        self.assertIsNotNone(nodb_conn.settings_dict['NAME'])
        self.assertEqual(nodb_conn.settings_dict['NAME'], connections['other'].settings_dict['NAME']) 
Example #24
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_invalid_operator(self):
        self.skipTest("TODO fix django.db.utils.ProgrammingError: Incorrect syntax near '1000000'.")
        # throws django.db.utils.ProgrammingError instead of DatabaseError
        with self.assertRaises(DatabaseError):
            list(Experiment.objects.filter(start=F('start') * datetime.timedelta(0))) 
Example #25
Source File: tests.py    From django-sqlserver with MIT License 5 votes vote down vote up
def test_savepoint_rollback(self):
        """
        Regression test for #20463: the database connection should still be
        usable after a DataError or ProgrammingError in .get_or_create().
        """
        try:
            Person.objects.get_or_create(
                birthday=date(1970, 1, 1),
                defaults={'first_name': b"\xff", 'last_name': b"\xff"})
        except (DatabaseError, DjangoUnicodeDecodeError):
            Person.objects.create(
                first_name="Bob", last_name="Ross", birthday=date(1950, 1, 1))
        else:
            self.skipTest("This backend accepts broken utf-8.") 
Example #26
Source File: middleware.py    From django-silk with MIT License 5 votes vote down vote up
def process_response(self, request, response):
        if getattr(request, 'silk_is_intercepted', False):
            while True:
                try:
                    self._process_response(request, response)
                except (AttributeError, DatabaseError):
                    Logger.debug('Retrying _process_response')
                    self._process_response(request, response)
                finally:
                    break
        return response 
Example #27
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_invalid_operator(self):
        with self.assertRaises(DatabaseError):
            list(Experiment.objects.filter(start=F('start') * datetime.timedelta(0))) 
Example #28
Source File: middleware.py    From django-silk with MIT License 5 votes vote down vote up
def process_response(self, request, response):
        if getattr(request, 'silk_is_intercepted', False):
            while True:
                try:
                    self._process_response(request, response)
                except (AttributeError, DatabaseError):
                    Logger.debug('Retrying _process_response')
                    self._process_response(request, response)
                finally:
                    break
        return response 
Example #29
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_select_on_save(self):
        a1 = Article.objects.create(pub_date=datetime.now())
        with self.assertNumQueries(1):
            a1.save()
        asos = ArticleSelectOnSave.objects.create(pub_date=datetime.now())
        with self.assertNumQueries(2):
            asos.save()
        with self.assertNumQueries(1):
            asos.save(force_update=True)
        Article.objects.all().delete()
        with self.assertRaisesMessage(DatabaseError, 'Forced update did not affect any rows.'):
            with self.assertNumQueries(1):
                asos.save(force_update=True) 
Example #30
Source File: tests.py    From Inboxen with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_exceptions(self):
        with self.assertRaises(SMTPError) as error:
            process_message(None, None, None)
        self.assertEqual(error.exception.code, 550)

        with self.assertRaises(SMTPError) as error, \
                mock.patch.object(models.Inbox.objects, "filter", side_effect=DatabaseError):
            process_message(None, None, None)
        self.assertEqual(error.exception.code, 451)