Python django.db.ProgrammingError() Examples

The following are 24 code examples of django.db.ProgrammingError(). 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: filters.py    From caluma with GNU General Public License v3.0 6 votes vote down vote up
def _validate_lookup(self, question, lookup):
        try:
            valid_lookups = self.VALID_LOOKUPS[question.type]
        except KeyError:  # pragma: no cover
            # Not covered in tests - this only happens when you add a new
            # question type and forget to update the lookup config above.  In
            # that case, the fix is simple - go up a few lines and adjust the
            # VALID_LOOKUPS dict.
            raise ProgrammingError(
                f"Valid lookups not configured for question type {question.type}"
            )

        if lookup not in valid_lookups:
            raise exceptions.ValidationError(
                f"Invalid lookup for question slug={question.slug} ({question.type.upper()}): {lookup.upper()}"
            ) 
Example #2
Source File: middleware.py    From peering-manager with Apache License 2.0 6 votes vote down vote up
def process_exception(self, request, exception):
        # Ignore exception catching if debug mode is on
        if settings.DEBUG:
            return

        # Lets Django handling 404
        if isinstance(exception, Http404):
            return

        template = None
        if isinstance(exception, ProgrammingError):
            template = "errors/programming_error.html"
        elif isinstance(exception, ImportError):
            template = "errors/import_error.html"

        if template:
            return ServerError(request, template_name=template) 
Example #3
Source File: test_tenants.py    From django-pgschemas with MIT License 6 votes vote down vote up
def test_cross_authentication(self):
        with SchemaDescriptor.create(schema_name="www"):
            self.assertTrue(authenticate(email="main@test.com", password="weakpassword"))  # good
            self.assertFalse(authenticate(email="blog@test.com", password="weakpassword"))  # bad
            self.assertFalse(authenticate(email="tenant@test.com", password="weakpassword"))  # bad
        with SchemaDescriptor.create(schema_name="blog"):
            self.assertTrue(authenticate(email="blog@test.com", password="weakpassword"))  # good
            self.assertFalse(authenticate(email="main@test.com", password="weakpassword"))  # bad
            self.assertFalse(authenticate(email="tenant@test.com", password="weakpassword"))  # bad
        with TenantModel.objects.first():
            self.assertTrue(authenticate(email="tenant@test.com", password="weakpassword"))  # good
            self.assertFalse(authenticate(email="main@test.com", password="weakpassword"))  # bad
            self.assertFalse(authenticate(email="blog@test.com", password="weakpassword"))  # bad
        # Switching to public schema
        TenantModel.deactivate_all()
        with self.assertRaises(ProgrammingError):
            authenticate(email="unexisting@test.com", password="unexisting")  # unexisting, error 
Example #4
Source File: test_tenants.py    From django-pgschemas with MIT License 6 votes vote down vote up
def test_synced_tenant_apps(self):
        with TenantModel.objects.first():
            # Expected synced apps
            self.assertEqual(2, Catalog.objects.count())
            self.assertEqual(1, TenantData.objects.count())
            self.assertEqual(1, User.objects.count())
            # Direct and reverse relations
            self.assertEqual(User.objects.first(), TenantData.objects.first().user)
            self.assertEqual(User.objects.first().tenant_objects.first(), TenantData.objects.first())
            self.assertEqual(Catalog.objects.first(), TenantData.objects.first().catalog)
            self.assertEqual(Catalog.objects.first().tenant_objects.first(), TenantData.objects.first())
            # Not expected synced apps
            with self.assertRaises(ProgrammingError):
                list(MainData.objects.all())
            with self.assertRaises(ProgrammingError):
                list(BlogEntry.objects.all()) 
Example #5
Source File: configuration.py    From palanaeum with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_config(key: str):
    from palanaeum.models import ConfigEntry
    value = cache.get(key)

    if value is not None:
        return _deserialize_value(key, value)

    try:
        value = ConfigEntry.objects.get(key=key).value
        cache.set(key, value)
    except ConfigEntry.DoesNotExist:
        value = CONFIG_ENTRIES[key][1]
    except ProgrammingError:
        logger.exception("Can't get config entry for %s.", key)
        return None

    return _deserialize_value(key, value) 
Example #6
Source File: db_actions.py    From tfrs with Apache License 2.0 6 votes vote down vote up
def create_db_comments(table_name, table_comment, column_comments=None):
    """Populate comments for non-model tables (like Django-specific tables)"""

    if connection.vendor != 'postgresql':
        return

    with connection.cursor() as cursor:
        try:
            cursor.execute(
                'comment on table "{}" is %s'.format(table_name), [table_comment]
            )
        except ProgrammingError:
            print(_exception_message)

        if column_comments is not None:
            for column, comment in column_comments.items():
                try:
                    cursor.execute(
                        'comment on column "{}"."{}" is %s'.format(table_name, column), [comment]
                    )
                except ProgrammingError as e:
                    print('{} -- {}'.format(_exception_message, e)) 
Example #7
Source File: migration_linter.py    From django-migration-linter with Apache License 2.0 6 votes vote down vote up
def get_sql(self, app_label, migration_name):
        logger.info(
            "Calling sqlmigrate command {} {}".format(app_label, migration_name)
        )
        dev_null = open(os.devnull, "w")
        try:
            sql_statement = call_command(
                "sqlmigrate",
                app_label,
                migration_name,
                database=self.database,
                stdout=dev_null,
            )
        except (ValueError, ProgrammingError):
            logger.warning(
                (
                    "Error while executing sqlmigrate on (%s, %s). "
                    "Continuing execution with empty SQL."
                ),
                app_label,
                migration_name,
            )
            sql_statement = ""
        return sql_statement.splitlines() 
Example #8
Source File: checks.py    From zing with GNU General Public License v3.0 6 votes vote down vote up
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 #9
Source File: checks.py    From zing with GNU General Public License v3.0 6 votes vote down vote up
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 #10
Source File: utils.py    From kpi with GNU Affero General Public License v3.0 6 votes vote down vote up
def set_kc_require_auth(user_id, require_auth):
    """
    Configure whether or not authentication is required to see and submit data
    to a user's projects.
    WRITES to KobocatUserProfile.require_auth

    :param int user_id: ID/primary key of the :py:class:`User` object.
    :param bool require_auth: The desired setting.
    """
    user = User.objects.get(pk=user_id)
    _trigger_kc_profile_creation(user)
    token, _ = Token.objects.get_or_create(user=user)
    with transaction.atomic():
        try:
            profile = KobocatUserProfile.objects.get(user_id=user_id)
        except ProgrammingError as e:
            raise ProgrammingError('set_kc_require_auth error accessing '
                                   'kobocat tables: {}'.format(repr(e)))
        else:
            if profile.require_auth != require_auth:
                profile.require_auth = require_auth
                profile.save() 
Example #11
Source File: db_actions.py    From tfrs with Apache License 2.0 5 votes vote down vote up
def create_db_comments_from_models(models):
    """Populate comments for model tables"""

    if connection.vendor != 'postgresql':
        return

    with connection.cursor() as cursor:
        for model_class in models:
            table = model_class.db_table_name() \
                if hasattr(model_class, 'db_table_name') else None
            table_comment = model_class.db_table_comment_or_name() \
                if hasattr(model_class, 'db_table_comment_or_name') else None
            column_comments = model_class.db_column_comments() \
                if hasattr(model_class, 'db_column_comments') else None

            if table_comment is not None:
                try:
                    cursor.execute(
                        'comment on table "{}" is %s'.format(table), [table_comment]
                    )
                except ProgrammingError as e:
                    print('{} -- {}'.format(_exception_message, e))

            if column_comments is not None:
                for column, comment in column_comments.items():
                    try:
                        if comment is not None:
                            cursor.execute(
                                'comment on column "{}"."{}" is %s'.format(table, column), [comment]
                            )
                    except ProgrammingError as e:
                        print('{} -- {}'.format(_exception_message, e)) 
Example #12
Source File: base.py    From django-postgres-extra with MIT License 5 votes vote down vote up
def prepare_database(self):
        """Ran to prepare the configured database.

        This is where we enable the `hstore` extension if it wasn't
        enabled yet.
        """

        super().prepare_database()

        setup_ext = getattr(
            settings, "POSTGRES_EXTRA_AUTO_EXTENSION_SET_UP", True
        )
        if not setup_ext:
            return False

        with self.cursor() as cursor:
            try:
                cursor.execute("CREATE EXTENSION IF NOT EXISTS hstore")
            except ProgrammingError:  # permission denied
                logger.warning(
                    'Failed to create "hstore" extension. '
                    "Tables with hstore columns may fail to migrate. "
                    "If hstore is needed, make sure you are connected "
                    "to the database as a superuser "
                    "or add the extension manually.",
                    exc_info=True,
                ) 
Example #13
Source File: residential_information.py    From omniport-backend with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        """
        Add the residence ChoiceField on the serializer based on the existence
        of Residence models
        :param args: arguments
        :param kwargs: keyword arguments
        """

        super().__init__(*args, **kwargs)

        try:
            residences = Residence.objects.all()
        except ProgrammingError:
            residences = list()

        self.fields['residence'] = serializers.ChoiceField(
            choices=[
                (residence.id, residence.name)
                for residence in residences
            ],
            write_only=True,
        ) 
Example #14
Source File: test_tenants.py    From django-pgschemas with MIT License 5 votes vote down vote up
def test_synced_blog_apps(self):
        with SchemaDescriptor.create(schema_name="blog"):
            # Expected synced apps
            self.assertEqual(2, Catalog.objects.count())
            self.assertEqual(1, BlogEntry.objects.count())
            self.assertEqual(1, User.objects.count())
            # Direct and reverse relations
            self.assertEqual(User.objects.first(), BlogEntry.objects.first().user)
            self.assertEqual(User.objects.first().blogs.first(), BlogEntry.objects.first())
            # Not expected synced apps
            with self.assertRaises(ProgrammingError):
                list(MainData.objects.all())
            with self.assertRaises(ProgrammingError):
                list(TenantData.objects.all()) 
Example #15
Source File: test_tenants.py    From django-pgschemas with MIT License 5 votes vote down vote up
def test_synced_main_apps(self):
        with SchemaDescriptor.create(schema_name="www"):
            # Expected synced apps
            self.assertEqual(2, Catalog.objects.count())
            self.assertEqual(1, MainData.objects.count())
            self.assertEqual(1, User.objects.count())
            # Not expected synced apps
            with self.assertRaises(ProgrammingError):
                list(BlogEntry.objects.all())
            with self.assertRaises(ProgrammingError):
                list(TenantData.objects.all()) 
Example #16
Source File: test_tenants.py    From django-pgschemas with MIT License 5 votes vote down vote up
def test_synced_public_apps(self):
        # Expected synced apps
        self.assertEqual(2, Catalog.objects.count())
        # Not expected synced apps
        with self.assertRaises(ProgrammingError):
            list(User.objects.all())
        with self.assertRaises(ProgrammingError):
            list(MainData.objects.all())
        with self.assertRaises(ProgrammingError):
            list(BlogEntry.objects.all())
        with self.assertRaises(ProgrammingError):
            list(TenantData.objects.all()) 
Example #17
Source File: logging.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def emit(self, record):
        """
        Handles an emitted log entry and stores it in the database, optionally linking it to the
        model object `obj`
        """
        obj = record.__dict__.get('obj', None)

        if obj is None or not isinstance(obj, models.Model) or obj.pk is None:
            content_type, object_id = None, None
        else:
            content_type = apps.get_model('contenttypes', 'ContentType').objects.get_for_model(obj)
            object_id = obj.pk

        try:
            log_entry = apps.get_model('instance', 'LogEntry').objects.create(
                level=record.levelname, text=self.format(record), content_type=content_type, object_id=object_id
            )
        except ProgrammingError:
            # This can occur if django tries to log something before migrations have created the log table.
            # Make sure that is actually what happened:
            assert 'instance_logentry' not in connection.introspection.table_names()

        # Send notice of entries related to any resource. Skip generic log entries that occur
        # in debug mode, like "GET /static/img/favicon/favicon-96x96.png":
        if content_type:
            log_event = {
                'type': 'object_log_line',
                'log_entry': LogEntrySerializer(log_entry).data
            }
            if hasattr(obj, 'event_context'):
                log_event.update(obj.event_context)
            # TODO: Filter out log entries for which the user doesn't have view rights
            # TODO: More targetted events - only emit events for what the user is looking at
            publish_data(log_event) 
Example #18
Source File: search.py    From palanaeum with GNU Affero General Public License v3.0 5 votes vote down vote up
def _get_results_for_token(self, token):
        token_results = {}
        if ' ' in token:
            # Currently, this should never happen
            query = " <-> ".join(token.split(' '))
        else:
            query = token

        try:
            for esv in EntrySearchVector.objects.raw(self.SQL_QUERY, [query, query]):
                token_results[esv.entry_id] = esv.rank
        except ProgrammingError:
            token_results = {}

        return token_results 
Example #19
Source File: test_runner.py    From zulip with Apache License 2.0 5 votes vote down vote up
def destroy_test_databases(worker_id: Optional[int]=None) -> None:
    for alias in connections:
        connection = connections[alias]
        try:
            # In the parallel mode, the test databases are created
            # through the N=self.parallel child processes, and in the
            # parent process (which calls `destroy_test_databases`),
            # `settings_dict` remains unchanged, with the original
            # template database name (zulip_test_template).  So to
            # delete the database zulip_test_template_<number>, we
            # need to pass `number` to `destroy_test_db`.
            #
            # When we run in serial mode (self.parallel=1), we don't
            # fork and thus both creation and destruction occur in the
            # same process, which means `settings_dict` has been
            # updated to have `zulip_test_template_<number>` as its
            # database name by the creation code.  As a result, to
            # delete that database, we need to not pass a number
            # argument to destroy_test_db.
            if worker_id is not None:
                """Modified from the Django original to """
                database_id = get_database_id(worker_id)
                connection.creation.destroy_test_db(suffix=database_id)
            else:
                connection.creation.destroy_test_db()
        except ProgrammingError:
            # DB doesn't exist. No need to do anything.
            pass 
Example #20
Source File: generate_realm_creation_link.py    From zulip with Apache License 2.0 5 votes vote down vote up
def handle(self, *args: Any, **options: Any) -> None:
        try:
            # first check if the db has been initialized
            Realm.objects.first()
        except ProgrammingError:
            raise CommandError("The Zulip database does not appear to exist. "
                               "Have you run initialize-database?")

        url = generate_realm_creation_url(by_admin=True)
        self.stdout.write(self.style.SUCCESS("Please visit the following "
                                             "secure single-use link to register your "))
        self.stdout.write(self.style.SUCCESS("new Zulip organization:\033[0m"))
        self.stdout.write("")
        self.stdout.write(self.style.SUCCESS(f"    \033[1;92m{url}\033[0m"))
        self.stdout.write("") 
Example #21
Source File: status.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def database_status(self):
        """Collect database connection information.

        :returns: A dict of db connection info.
        """
        try:
            with connection.cursor() as cursor:
                cursor.execute(
                    """
                    SELECT datname AS database,
                        numbackends as database_connections
                    FROM pg_stat_database
                    """
                )
                raw = cursor.fetchall()

                # get pg_stat_database column names
                names = [desc[0] for desc in cursor.description]
        except (InterfaceError, NotSupportedError, OperationalError, ProgrammingError) as exc:
            LOG.warning("Unable to connect to DB: %s", str(exc))
            return {"ERROR": str(exc)}

        # transform list-of-lists into list-of-dicts including column names.
        result = [dict(zip(names, row)) for row in raw]

        return result 
Example #22
Source File: status.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def database_status(self):
        """Collect database connection information.

        :returns: A dict of db connection info.
        """
        try:
            with connection.cursor() as cursor:
                cursor.execute(
                    """
                    SELECT datname AS database,
                        numbackends as database_connections
                    FROM pg_stat_database
                    """
                )
                raw = cursor.fetchall()

                # get pg_stat_database column names
                names = [desc[0] for desc in cursor.description]
        except (InterfaceError, NotSupportedError, OperationalError, ProgrammingError) as exc:
            LOG.warning("Unable to connect to DB: %s", str(exc))
            return {"ERROR": str(exc)}

        # transform list-of-lists into list-of-dicts including column names.
        result = [dict(zip(names, row)) for row in raw]

        return result 
Example #23
Source File: manager.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_max_notification_id(self) -> int:
        assert self.notification_id_name
        try:
            objects = self.record_class.objects  # type: ignore
            if hasattr(self.record_class, "application_name"):
                objects = objects.filter(application_name=self.application_name)
            if hasattr(self.record_class, "pipeline_id"):
                objects = objects.filter(pipeline_id=self.pipeline_id)
            latest = objects.latest(self.notification_id_name)
            return getattr(latest, self.notification_id_name)
        except (self.record_class.DoesNotExist, ProgrammingError):  # type: ignore
            return 0 
Example #24
Source File: test_fixtures.py    From zulip with Apache License 2.0 4 votes vote down vote up
def destroy_leaked_test_databases(expiry_time: int = 60 * 60) -> int:
    """The logic in zerver/lib/test_runner.py tries to delete all the
    temporary test databases generated by test-backend threads, but it
    cannot guarantee it handles all race conditions correctly.  This
    is a catch-all function designed to delete any that might have
    been leaked due to crashes (etc.).  The high-level algorithm is to:

    * Delete every database with a name like zulip_test_template_*
    * Unless it is registered in a file under TEMPLATE_DATABASE_DIR as
      part of a currently running test-backend invocation
    * And that file is less expiry_time old.

    This should ensure we ~never break a running test-backend process,
    while also ensuring we will eventually delete all leaked databases.
    """
    files = glob.glob(os.path.join(UUID_VAR_DIR, TEMPLATE_DATABASE_DIR, "*"))
    test_databases: Set[str] = set()
    try:
        with connection.cursor() as cursor:
            cursor.execute("SELECT datname FROM pg_database;")
            rows = cursor.fetchall()
            for row in rows:
                if 'zulip_test_template_' in row[0]:
                    test_databases.add(row[0])
    except ProgrammingError:
        pass

    databases_in_use: Set[str] = set()
    for file in files:
        if round(time.time()) - os.path.getmtime(file) < expiry_time:
            with open(file) as f:
                for line in f:
                    databases_in_use.add(f'zulip_test_template_{line}'.rstrip())
        else:
            # Any test-backend run older than expiry_time can be
            # cleaned up, both the database and the file listing its
            # databases.
            os.remove(file)

    databases_to_drop = test_databases - databases_in_use

    if not databases_to_drop:
        return 0

    commands = "\n".join(f"DROP DATABASE IF EXISTS {db};" for db in databases_to_drop)
    p = subprocess.Popen(["psql", "-q", "-v", "ON_ERROR_STOP=1", "-h", "localhost",
                          "postgres", "zulip_test"],
                         stdin=subprocess.PIPE)
    p.communicate(input=commands.encode())
    if p.returncode != 0:
        raise RuntimeError("Error cleaning up test databases!")
    return len(databases_to_drop)