m1kc / django-spinproject

Opinionated version of `startproject` with some popular third-party packages.
MIT License
5 stars 1 forks source link

pg_readonly: psycopg3 support #55

Open m1kc opened 1 year ago

m1kc commented 1 year ago
from django.db.backends.postgresql import base
class DatabaseWrapper(base.DatabaseWrapper):
    def get_new_connection(self, conn_params):
        conn = super(DatabaseWrapper, self).get_new_connection(conn_params)
        if hasattr(conn, 'read_only'):
            # pyscopg3
            conn.read_only = True
        elif hasattr(conn, 'set_session'):
            # pyscopg2
            conn.set_session(readonly=True)
        else:
            raise RuntimeError("pg_readonly: unknown adapter type (only pyscopg2 and pyscopg3 are supported)")
        return conn