mirror of
https://github.com/django/django.git
synced 2025-08-07 20:38:21 +00:00
Refs #32061 -- Unified DatabaseClient.runshell() in db backends.
This commit is contained in:
parent
4ac2d4fa42
commit
bbe6fbb876
16 changed files with 272 additions and 206 deletions
|
@ -1,6 +1,4 @@
|
|||
import os
|
||||
import signal
|
||||
import subprocess
|
||||
|
||||
from django.db.backends.base.client import BaseDatabaseClient
|
||||
|
||||
|
@ -9,18 +7,19 @@ class DatabaseClient(BaseDatabaseClient):
|
|||
executable_name = 'psql'
|
||||
|
||||
@classmethod
|
||||
def runshell_db(cls, conn_params, parameters):
|
||||
def settings_to_cmd_args_env(cls, settings_dict, parameters):
|
||||
args = [cls.executable_name]
|
||||
options = settings_dict.get('OPTIONS', {})
|
||||
|
||||
host = conn_params.get('host', '')
|
||||
port = conn_params.get('port', '')
|
||||
dbname = conn_params.get('database', '')
|
||||
user = conn_params.get('user', '')
|
||||
passwd = conn_params.get('password', '')
|
||||
sslmode = conn_params.get('sslmode', '')
|
||||
sslrootcert = conn_params.get('sslrootcert', '')
|
||||
sslcert = conn_params.get('sslcert', '')
|
||||
sslkey = conn_params.get('sslkey', '')
|
||||
host = settings_dict.get('HOST')
|
||||
port = settings_dict.get('PORT')
|
||||
dbname = settings_dict.get('NAME') or 'postgres'
|
||||
user = settings_dict.get('USER')
|
||||
passwd = settings_dict.get('PASSWORD')
|
||||
sslmode = options.get('sslmode')
|
||||
sslrootcert = options.get('sslrootcert')
|
||||
sslcert = options.get('sslcert')
|
||||
sslkey = options.get('sslkey')
|
||||
|
||||
if user:
|
||||
args += ['-U', user]
|
||||
|
@ -31,25 +30,25 @@ class DatabaseClient(BaseDatabaseClient):
|
|||
args += [dbname]
|
||||
args.extend(parameters)
|
||||
|
||||
sigint_handler = signal.getsignal(signal.SIGINT)
|
||||
subprocess_env = os.environ.copy()
|
||||
env = {}
|
||||
if passwd:
|
||||
subprocess_env['PGPASSWORD'] = str(passwd)
|
||||
env['PGPASSWORD'] = str(passwd)
|
||||
if sslmode:
|
||||
subprocess_env['PGSSLMODE'] = str(sslmode)
|
||||
env['PGSSLMODE'] = str(sslmode)
|
||||
if sslrootcert:
|
||||
subprocess_env['PGSSLROOTCERT'] = str(sslrootcert)
|
||||
env['PGSSLROOTCERT'] = str(sslrootcert)
|
||||
if sslcert:
|
||||
subprocess_env['PGSSLCERT'] = str(sslcert)
|
||||
env['PGSSLCERT'] = str(sslcert)
|
||||
if sslkey:
|
||||
subprocess_env['PGSSLKEY'] = str(sslkey)
|
||||
env['PGSSLKEY'] = str(sslkey)
|
||||
return args, env
|
||||
|
||||
def runshell(self, parameters):
|
||||
sigint_handler = signal.getsignal(signal.SIGINT)
|
||||
try:
|
||||
# Allow SIGINT to pass to psql to abort queries.
|
||||
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
subprocess.run(args, check=True, env=subprocess_env)
|
||||
super().runshell(parameters)
|
||||
finally:
|
||||
# Restore the original SIGINT handler.
|
||||
signal.signal(signal.SIGINT, sigint_handler)
|
||||
|
||||
def runshell(self, parameters):
|
||||
self.runshell_db(self.connection.get_connection_params(), parameters)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue