• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lorinkoz / django-pgschemas / 6618841281

23 Oct 2023 08:47PM CUT coverage: 93.617%. Remained the same
6618841281

Pull #187

github

web-flow
Merge a81f35a98 into b4182e6e1
Pull Request #187: Bump ruff from 0.1.0 to 0.1.1

1188 of 1269 relevant lines covered (93.62%)

5.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.24
/django_pgschemas/postgresql_backend/base.py
1
from django.core.exceptions import ImproperlyConfigured
6✔
2
from django.db.utils import DatabaseError
6✔
3

4
from ..schema import get_current_schema, get_default_schema
6✔
5
from ..utils import check_schema_name, get_limit_set_calls
6✔
6
from .introspection import DatabaseSchemaIntrospection
6✔
7
from .settings import EXTRA_SEARCH_PATHS, original_backend
6✔
8

9
try:
6✔
10
    try:
6✔
11
        import psycopg as _psycopg
6✔
12
    except ImportError:
3✔
13
        import psycopg2 as _psycopg
3✔
14
except ImportError:
×
15
    raise ImproperlyConfigured("Error loading psycopg2 or psycopg module")
×
16

17
IntegrityError = _psycopg.IntegrityError
6✔
18

19

20
def get_search_path(schema=None):
6✔
21
    if schema is None:
6✔
22
        schema = get_default_schema()
×
23

24
    search_path = ["public"] if schema.schema_name == "public" else [schema.schema_name, "public"]
6✔
25
    search_path.extend(EXTRA_SEARCH_PATHS)
6✔
26

27
    for part in search_path:
6✔
28
        check_schema_name(part)
6✔
29

30
    return ", ".join(search_path)
6✔
31

32

33
class DatabaseWrapper(original_backend.DatabaseWrapper):
6✔
34
    def __init__(self, *args, **kwargs):
6✔
35
        self._search_path = None
6✔
36
        self._setting_search_path = False
6✔
37
        super().__init__(*args, **kwargs)
6✔
38

39
        # Patched version of DatabaseIntrospection that only returns the table list for the currently selected schema
40
        self.introspection = DatabaseSchemaIntrospection(self)
6✔
41

42
    def close(self):
6✔
43
        self._search_path = None
6✔
44
        self._setting_search_path = False
6✔
45
        super().close()
6✔
46

47
    def _handle_search_path(self, cursor=None):
6✔
48
        search_path_for_current_schema = get_search_path(get_current_schema())
6✔
49

50
        skip = (
6✔
51
            self._setting_search_path
52
            or self._search_path == search_path_for_current_schema
53
            or get_limit_set_calls()
54
        )
55

56
        if not skip:
6✔
57
            self._setting_search_path = True
6✔
58
            cursor_for_search_path = self.connection.cursor() if cursor is None else cursor
6✔
59

60
            try:
6✔
61
                cursor_for_search_path.execute(
6✔
62
                    f"SET search_path = {search_path_for_current_schema}"
63
                )
64
            except (DatabaseError, _psycopg.InternalError):
×
65
                self._search_path = None
×
66
            else:
67
                self._search_path = search_path_for_current_schema
6✔
68
            finally:
69
                self._setting_search_path = False
6✔
70

71
            if cursor is None:
6✔
72
                cursor_for_search_path.close()
×
73

74
    def _cursor(self, name=None):
6✔
75
        cursor = super()._cursor(name=name)
6✔
76

77
        cursor_for_search_path = cursor if name is None else None  # Named cursors cannot be reused
6✔
78
        self._handle_search_path(cursor_for_search_path)
6✔
79

80
        return cursor
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc