Python django.db.utils.OperationalError() Examples

The following are 30 code examples of django.db.utils.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.utils , or try the search function .
Example #1
Source File: apps.py    From django-river with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def ready(self):

        for field_name in self._get_all_workflow_fields():
            try:
                workflows = self.get_model('Workflow').objects.filter(field_name=field_name)
                if workflows.count() == 0:
                    LOGGER.warning("%s field doesn't seem have any workflow defined in database. You should create its workflow" % field_name)
            except (OperationalError, ProgrammingError):
                pass

        from river.config import app_config

        if app_config.INJECT_MODEL_ADMIN:
            for model_class in self._get_all_workflow_classes():
                self._register_hook_inlines(model_class)

        LOGGER.debug('RiverApp is loaded.') 
Example #2
Source File: urls.py    From polls-api with MIT License 6 votes vote down vote up
def healthcheck_view(request):
    content_type = 'application/health+json'
    database_accessible = True

    try:
        connections['default'].cursor()
    except ImproperlyConfigured:
        # Database is not configured (DATABASE_URL may not be set)
        database_accessible = False
    except OperationalError:
        # Database is not accessible
        database_accessible = False

    if database_accessible:
        return JsonResponse({ 'status': 'ok' }, content_type=content_type)

    return JsonResponse({ 'status': 'fail' }, status=503, content_type=content_type) 
Example #3
Source File: schema.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def remove_field(self, model, field):
        if isinstance(field, GeometryField) and field.spatial_index:
            qn = self.connection.ops.quote_name
            sql = self.sql_drop_spatial_index % {
                'index': qn(self._create_spatial_index_name(model, field)),
                'table': qn(model._meta.db_table),
            }
            try:
                self.execute(sql)
            except OperationalError:
                logger.error(
                    "Couldn't remove spatial index: %s (may be expected "
                    "if your storage engine doesn't support them)." % sql
                )

        super(MySQLGISSchemaEditor, self).remove_field(model, field) 
Example #4
Source File: checks.py    From normandy with Mozilla Public License 2.0 6 votes vote down vote up
def actions_have_consistent_hashes(app_configs, **kwargs):
    errors = []
    try:
        Action = apps.get_model("recipes", "Action")
        actions = list(Action.objects.filter(implementation__isnull=False))
    except (ProgrammingError, OperationalError, ImproperlyConfigured) as e:
        errors.append(Info(f"Could not retrieve actions: {e}", id=INFO_COULD_NOT_RETRIEVE_ACTIONS))
    else:
        for action in actions:
            if action.compute_implementation_hash() != action.implementation_hash:
                msg = "Action '{action}' (id={action.id}) has a mismatched hash".format(
                    action=action
                )
                errors.append(Error(msg, id=ERROR_MISMATCHED_ACTION_HASH))

    return errors 
Example #5
Source File: checks.py    From python-dockerflow with Mozilla Public License 2.0 6 votes vote down vote up
def check_database_connected(app_configs, **kwargs):
    """
    A Django check to see if connecting to the configured default
    database backend succeeds.
    """
    errors = []

    try:
        connection.ensure_connection()
    except OperationalError as e:
        msg = "Could not connect to database: {!s}".format(e)
        errors.append(checks.Error(msg, id=health.ERROR_CANNOT_CONNECT_DATABASE))
    except ImproperlyConfigured as e:
        msg = 'Datbase misconfigured: "{!s}"'.format(e)
        errors.append(checks.Error(msg, id=health.ERROR_MISCONFIGURED_DATABASE))
    else:
        if not connection.is_usable():
            errors.append(
                checks.Error(
                    "Database connection is not usable",
                    id=health.ERROR_UNUSABLE_DATABASE,
                )
            )

    return errors 
Example #6
Source File: middleware.py    From koku with GNU Affero General Public License v3.0 6 votes vote down vote up
def process_request(self, request):
        """Check before super."""
        connection.set_schema_to_public()

        if not is_no_auth(request):
            if hasattr(request, "user") and hasattr(request.user, "username"):
                username = request.user.username
                try:
                    if username not in USER_CACHE:
                        USER_CACHE[username] = User.objects.get(username=username)
                        LOG.debug(f"User added to cache: {username}")
                except User.DoesNotExist:
                    return HttpResponseUnauthorizedRequest()
                if not request.user.admin and request.user.access is None:
                    LOG.warning("User %s is does not have permissions for Cost Management.", username)
                    raise PermissionDenied()
            else:
                return HttpResponseUnauthorizedRequest()
        try:
            super().process_request(request)
        except OperationalError as err:
            LOG.error("Request resulted in OperationalError: %s", err)
            DB_CONNECTION_ERRORS_COUNTER.inc()
            return HttpResponseFailedDependency({"source": "Database", "exception": err}) 
Example #7
Source File: tests_middleware.py    From koku with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_process_operational_error_return_424(self):
        """Test OperationalError causes 424 Reponse."""
        user_data = self._create_user_data()
        customer = self._create_customer_data()
        request_context = self._create_request_context(
            customer, user_data, create_customer=True, create_tenant=True, is_admin=True, is_cost_management=True
        )
        mock_request = request_context["request"]
        mock_request.path = "/api/v1/tags/aws/"
        mock_request.META["QUERY_STRING"] = ""

        with patch("koku.middleware.Customer.objects") as mock_customer:
            mock_customer.filter.side_effect = OperationalError

            middleware = IdentityHeaderMiddleware()
            response = middleware.process_request(mock_request)
            self.assertEqual(response.status_code, status.HTTP_424_FAILED_DEPENDENCY) 
Example #8
Source File: ExceptionHandlerMiddleware.py    From texta with GNU General Public License v3.0 6 votes vote down vote up
def process_exception(self, request, exception):
        if isinstance(exception, requests.exceptions.ConnectionError):
            logging.getLogger(ERROR_LOGGER).exception(exception)
            messages.error(request, "Could not connect to resource: {}. Please check if all the resources (Elasticsearch) are available!".format(exception.request.url))
            template_data = {'STATIC_URL': STATIC_URL, 'allowed_datasets': None, 'language_models': None}
            return redirect("/", context=template_data)

        if isinstance(exception, OperationalError):
            logging.getLogger(ERROR_LOGGER).exception(exception)
            messages.error(request, "Error, please refresh the page!".format(exception))
            template_data = {'STATIC_URL': STATIC_URL, 'allowed_datasets': None, 'language_models': None}
            return redirect("/", context=template_data)

        else:
            logging.getLogger(ERROR_LOGGER).exception(exception)
            messages.error(request, "Error, please try again or contact the developers: {}!".format(exception))
            template_data = {'STATIC_URL': STATIC_URL, 'allowed_datasets': None, 'language_models': None}
            return redirect("/", context=template_data) 
Example #9
Source File: wait_for_db.py    From doccano with MIT License 6 votes vote down vote up
def handle(self, *args, **options):
        max_retries = options['max_retries']
        poll_seconds = options['poll_seconds']

        for retry in range(max_retries):
            try:
                connection.ensure_connection()
            except OperationalError as ex:
                self.stdout.write(
                    'Database unavailable on attempt {attempt}/{max_retries}:'
                    ' {error}'.format(
                        attempt=retry + 1,
                        max_retries=max_retries,
                        error=ex))
                time.sleep(poll_seconds)
            else:
                break
        else:
            self.stdout.write(self.style.ERROR('Database unavailable'))
            sys.exit(1) 
Example #10
Source File: retry.py    From scale with Apache License 2.0 6 votes vote down vote up
def retry_database_query(no_arg_func=None, max_tries=5, base_ms_delay=1000, max_ms_delay=30000):
    """Wraps the decorated function so that it is retried with exponential backoff if any operational database errors
    (disconnect, too many connections, etc) occurs. On the first retry, the delay is a random number of milliseconds
    between 0 and base_ms_delay. The upper bound is then doubled for each successive retry. A retry delay will not
    exceed max_ms_delay milliseconds.

    :param no_arg_func: The function to retry (only populated if decorator used without args)
    :type no_arg_func: function
    :param max_tries: The maximum number of times to call the function
    :type max_tries: int
    :param base_ms_delay: The base time to delay in milliseconds before retrying the function
    :type base_ms_delay: int
    :param max_ms_delay: The maximum time to delay in milliseconds
    :type max_ms_delay: int
    """

    return retry(no_arg_func=no_arg_func, ex_class=OperationalError, max_tries=max_tries, base_ms_delay=base_ms_delay,
                 max_ms_delay=max_ms_delay) 
Example #11
Source File: permissions.py    From django-collaborative with MIT License 6 votes vote down vote up
def hydrate_models_and_permissions(app_config):
    """
    Setup content types, base permissions and data source
    specific permission groups for use in the admin.
    """
    try:
        create_contenttypes(app_config, interactive=False, verbosity=4)
    except OperationalError as e:
        logger.error("Error creating content-types: %s" % e)

    try:
        create_permissions(app_config, interactive=False, verbosity=4)
    except OperationalError as e:
        logger.error("Error creating permissions: %s" % e)

    try:
        build_permission_groups(app_config.name)
    except (OperationalError, AppRegistryNotReady) as e:
        logger.error("Error creating permission groups: %s" % e)

    try:
        build_tag_permission_group()
    except (OperationalError, AppRegistryNotReady) as e:
        logger.error("Error creating tagging perm group: %s" % e) 
Example #12
Source File: has_missing_migrations.py    From django-react-boilerplate with MIT License 6 votes vote down vote up
def handle(self, *args, **options):
        changed = set()

        self.stdout.write("Checking...")
        for db in settings.DATABASES.keys():
            try:
                executor = MigrationExecutor(connections[db])
            except OperationalError:
                sys.exit("Unable to check migrations: cannot connect to database\n")

            autodetector = MigrationAutodetector(
                executor.loader.project_state(), ProjectState.from_apps(apps),
            )
            changed.update(autodetector.changes(graph=executor.loader.graph).keys())

        changed -= set(options["ignore"])

        if changed:
            sys.exit(
                "Apps with model changes but no corresponding migration file: %(changed)s\n"
                % {"changed": list(changed)}
            )
        else:
            sys.stdout.write("All migration files present\n") 
Example #13
Source File: apps.py    From InvenTree with MIT License 6 votes vote down vote up
def generate_part_thumbnails(self):
        from .models import Part

        print("InvenTree: Checking Part image thumbnails")

        try:
            for part in Part.objects.all():
                if part.image:
                    url = part.image.thumbnail.name
                    loc = os.path.join(settings.MEDIA_ROOT, url)
                    
                    if not os.path.exists(loc):
                        print("InvenTree: Generating thumbnail for Part '{p}'".format(p=part.name))
                        try:
                            part.image.render_variations(replace=False)
                        except FileNotFoundError:
                            print("Image file missing")
                            part.image = None
                            part.save()
        except (OperationalError, ProgrammingError):
            print("Could not generate Part thumbnails") 
Example #14
Source File: apps.py    From InvenTree with MIT License 6 votes vote down vote up
def generate_company_thumbs(self):

        from .models import Company

        print("InvenTree: Checking Company image thumbnails")

        try:
            for company in Company.objects.all():
                if company.image:
                    url = company.image.thumbnail.name
                    loc = os.path.join(settings.MEDIA_ROOT, url)

                    if not os.path.exists(loc):
                        print("InvenTree: Generating thumbnail for Company '{c}'".format(c=company.name))
                        try:
                            company.image.render_variations(replace=False)
                        except FileNotFoundError:
                            print("Image file missing")
                            company.image = None
                            company.save()
        except (OperationalError, ProgrammingError):
            print("Could not generate Company thumbnails") 
Example #15
Source File: schema.py    From openhgsenti with Apache License 2.0 6 votes vote down vote up
def remove_field(self, model, field):
        if isinstance(field, GeometryField) and field.spatial_index:
            qn = self.connection.ops.quote_name
            sql = self.sql_drop_spatial_index % {
                'index': qn(self._create_spatial_index_name(model, field)),
                'table': qn(model._meta.db_table),
            }
            try:
                self.execute(sql)
            except OperationalError:
                logger.error(
                    "Couldn't remove spatial index: %s (may be expected "
                    "if your storage engine doesn't support them)." % sql
                )

        super(MySQLGISSchemaEditor, self).remove_field(model, field) 
Example #16
Source File: models.py    From trunk-player with MIT License 6 votes vote down vote up
def create_profile(sender, **kwargs):
    user = kwargs["instance"]
    if kwargs["created"]:
        default_plan = Plan.objects.get(pk=Plan.DEFAULT_PK)
        up = Profile(user=user, plan=default_plan)
        up.save()
        try:
            for tg in TalkGroupAccess.objects.filter(default_group=True):
                up.talkgroup_access.add(tg)
        except OperationalError:
            pass
        try:
            new_user_email = SiteOption.objects.get(name='SEND_ADMIN_EMAIL_ON_NEW_USER')
            if new_user_email.value_boolean_or_string() == True:
                send_mail(
                      'New {} User {}'.format(settings.SITE_TITLE, user.username),
                      'New User {} {} Username {} Email {} just registered'.format(user.first_name, user.last_name, user.username, user.email),
                      settings.SERVER_EMAIL,
                      [ mail for name, mail in settings.ADMINS],
                      fail_silently=False,
                     )
        except (SiteOption.DoesNotExist, OperationalError):
            pass 
Example #17
Source File: models.py    From diting with GNU General Public License v2.0 5 votes vote down vote up
def refresh_all_settings(cls):
        try:
            settings_list = cls.objects.all()
            for setting in settings_list:
                setting.refresh_setting()
        except (ProgrammingError, OperationalError):
            pass 
Example #18
Source File: doctor.py    From hoover-search with MIT License 5 votes vote down vote up
def check_database(self):
        db_conn = connections['default']
        try:
            _ = db_conn.cursor()
        except OperationalError:
            self.print_error("The database settings are not valid.")
            self.print_error("Please check the database access data under DATABASES.")
            return False
        return True 
Example #19
Source File: queries.py    From everbug with MIT License 5 votes vote down vote up
def wrap_queries():
    """ Wraps queries with their explains
    For example: {alias: [list of queries with explains], ...}
    List of queries like: [{'time': ..., 'explain': ..., 'sql': ...}, ...]
    :return: (None or dict)
    """

    def query_explain(connection, raw_sql):
        if connection.vendor == 'sqlite':
            sql = "EXPLAIN QUERY PLAN %s" % raw_sql
        elif connection.vendor == 'postgresql':
            sql = "EXPLAIN ANALYZE %s" % raw_sql
        elif connection.vendor == 'oracle':
            sql = "EXPLAIN PLAN FOR %s" % raw_sql
        else:
            sql = "EXPLAIN %s" % raw_sql
        try:
            with connections[connection.alias].cursor() as cursor:
                cursor.execute(sql)
                return cursor.fetchall()
        except OperationalError:
            return []

    active_connections = [c for c in connections.all() if c.queries]
    if not active_connections:
        return None

    queries = {connection.alias: [] for connection in active_connections}
    for connection in active_connections:
        for query in connection.queries:
            explain = {'explain': query_explain(connection, query['sql'])}
            queries[connection.alias].append(dict(query, **explain))
    return queries 
Example #20
Source File: forms.py    From eventoL with GNU General Public License v3.0 5 votes vote down vote up
def get(self, request, *args, **kwargs):
        try:
            return super().get(request, *args, **kwargs)
        except OperationalError as error:
            logger.error(error)
            self.use_unaccent = False
            return super().get(request, *args, **kwargs) 
Example #21
Source File: models.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def get_for_model(self, model, for_concrete_model=True):
        """
        Returns the ContentType object for a given model, creating the
        ContentType if necessary. Lookups are cached so that subsequent lookups
        for the same model don't hit the database.
        """
        opts = self._get_opts(model, for_concrete_model)
        try:
            return self._get_from_cache(opts)
        except KeyError:
            pass

        # The ContentType entry was not found in the cache, therefore we
        # proceed to load or create it.
        try:
            try:
                # We start with get() and not get_or_create() in order to use
                # the db_for_read (see #20401).
                ct = self.get(app_label=opts.app_label, model=opts.model_name)
            except self.model.DoesNotExist:
                # Not found in the database; we proceed to create it.  This time we
                # use get_or_create to take care of any race conditions.
                ct, created = self.get_or_create(
                    app_label=opts.app_label,
                    model=opts.model_name,
                )
        except (OperationalError, ProgrammingError, IntegrityError):
            # It's possible to migrate a single app before contenttypes,
            # as it's not a required initial dependency (it's contrib!)
            # Have a nice error for this.
            raise RuntimeError(
                "Error creating new content types. Please make sure contenttypes "
                "is migrated before trying to migrate apps individually."
            )
        self._add_to_cache(self.db, ct)
        return ct 
Example #22
Source File: schema.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def create_spatial_indexes(self):
        for sql in self.geometry_sql:
            try:
                self.execute(sql)
            except OperationalError:
                logger.error(
                    "Cannot create SPATIAL INDEX %s. Only MyISAM and (as of "
                    "MySQL 5.7.5) InnoDB support them." % sql
                )
        self.geometry_sql = [] 
Example #23
Source File: orm.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def make_deadlock_failure():
    """Make a deadlock exception.

    Artificially construct an exception that resembles what Django's ORM would
    raise when PostgreSQL fails a transaction because of a deadlock
    failure.

    :returns: an instance of :py:class:`OperationalError` that will pass the
        `is_deadlock_failure` predicate.
    """
    exception = OperationalError()
    exception.__cause__ = DeadlockFailure()
    assert is_deadlock_failure(exception)
    return exception 
Example #24
Source File: operations.py    From django-cockroachdb with Apache License 2.0 5 votes vote down vote up
def execute_sql_flush(self, using, sql_list):
        # Retry TRUNCATE if it fails with a serialization error.
        num_retries = 10
        initial_retry_delay = 0.5  # The initial retry delay, in seconds.
        backoff_ = 1.5  # For each retry, the last delay is multiplied by this.
        next_retry_delay = initial_retry_delay
        for retry in range(1, num_retries + 1):
            try:
                return super().execute_sql_flush(using, sql_list)
            except OperationalError as exc:
                if (getattr(exc.__cause__, 'pgcode', '') != errorcodes.SERIALIZATION_FAILURE or
                        retry >= num_retries):
                    raise
                time.sleep(next_retry_delay)
                next_retry_delay *= backoff_ 
Example #25
Source File: test_commands.py    From Project-Dashboard-with-Django with MIT License 5 votes vote down vote up
def test_wait_for_db(self, ts):
        """Test if django retry db connection after fail"""
        with patch('django.db.utils.ConnectionHandler.__getitem__')as gi:
            """Raise operational error 5 times, then succeed """
            gi.side_effect = [OperationalError] * 5 + [True]
            call_command('wait_for_db')
            self.assertEqual(gi.call_count, 6) 
Example #26
Source File: wait_for_db.py    From Project-Dashboard-with-Django with MIT License 5 votes vote down vote up
def handle(self, *args, **kwargs):
        self.stdout.write('Waiting for database...')  # print with stdout to terminal
        db_conn = None
        while not db_conn:
            try:
                db_conn = connections['default']
            except OperationalError:
                self.stdout.write('Database unavailable, waiting 1 second...')
                time.sleep(1)

        self.stdout.write(self.style.SUCCESS('Database available.')) 
Example #27
Source File: check_missing_migrations.py    From django-andablog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def handle(self, *args, **kwargs):

        changed = set()
        ignore_list = ['authtools']  # dependencies that we don't care about migrations for (usually for testing only)

        self.stdout.write("Checking...")
        for db in settings.DATABASES.keys():

            try:
                executor = MigrationExecutor(connections[db])
            except OperationalError:
                sys.exit("Unable to check migrations: cannot connect to database\n")

            autodetector = MigrationAutodetector(
                executor.loader.project_state(),
                ProjectState.from_apps(apps),
            )

            changed.update(autodetector.changes(graph=executor.loader.graph).keys())

        for ignore in ignore_list:
            if ignore in changed:
                changed.remove(ignore)

        if changed:
            sys.exit("Apps with model changes but no corresponding migration file: %(changed)s\n" % {
                'changed': list(changed)
            })
        else:
            sys.stdout.write("All migration files present\n") 
Example #28
Source File: test_django.py    From python-dockerflow with Mozilla Public License 2.0 5 votes vote down vote up
def test_check_database_connected_cannot_connect(mocker):
    ensure_connection = mocker.patch("django.db.connection.ensure_connection")
    ensure_connection.side_effect = OperationalError
    errors = checks.check_database_connected([])
    assert len(errors) == 1
    assert errors[0].id == health.ERROR_CANNOT_CONNECT_DATABASE 
Example #29
Source File: test_orm.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_rejects_operational_error_without_matching_cause(self):
        error = OperationalError()
        cause = self.patch(error, "__cause__", Exception())
        cause.pgcode = factory.make_name("pgcode")
        self.assertFalse(is_deadlock_failure(error)) 
Example #30
Source File: test_orm.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_rejects_operational_error_without_matching_cause(self):
        error = OperationalError()
        cause = self.patch(error, "__cause__", Exception())
        cause.pgcode = factory.make_name("pgcode")
        self.assertFalse(is_serialization_failure(error))