File: db_introspection.py

package info (click to toggle)
python-django-postgres-extra 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: python: 9,057; makefile: 17; sh: 7; sql: 1
file content (103 lines) | stat: -rw-r--r-- 2,876 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Thin wrappers over functions in connection.introspection that don't require
creating a cursor.

This makes test code less verbose and easier to read/write.
"""

from contextlib import contextmanager
from typing import Optional

from django.db import connection

from psqlextra.settings import postgres_set_local


@contextmanager
def introspect(schema_name: Optional[str] = None):
    with postgres_set_local(search_path=schema_name or None):
        with connection.cursor() as cursor:
            yield connection.introspection, cursor


def table_names(
    include_views: bool = True, *, schema_name: Optional[str] = None
):
    """Gets a flat list of tables in the default database."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.table_names(cursor, include_views)


def get_partitioned_table(
    table_name: str,
    *,
    schema_name: Optional[str] = None,
):
    """Gets the definition of a partitioned table in the default database."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.get_partitioned_table(cursor, table_name)


def get_partitions(
    table_name: str,
    *,
    schema_name: Optional[str] = None,
):
    """Gets a list of partitions for the specified partitioned table in the
    default database."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.get_partitions(cursor, table_name)


def get_columns(
    table_name: str,
    *,
    schema_name: Optional[str] = None,
):
    """Gets a list of columns for the specified table."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.get_columns(cursor, table_name)


def get_relations(
    table_name: str,
    *,
    schema_name: Optional[str] = None,
):
    """Gets a list of relations for the specified table."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.get_relations(cursor, table_name)


def get_constraints(
    table_name: str,
    *,
    schema_name: Optional[str] = None,
):
    """Gets a list of constraints and indexes for the specified table."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.get_constraints(cursor, table_name)


def get_sequences(
    table_name: str,
    *,
    schema_name: Optional[str] = None,
):
    """Gets a list of sequences own by the specified table."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.get_sequences(cursor, table_name)


def get_storage_settings(table_name: str, *, schema_name: Optional[str] = None):
    """Gets a list of all storage settings that have been set on the specified
    table."""

    with introspect(schema_name) as (introspection, cursor):
        return introspection.get_storage_settings(cursor, table_name)