2017-04-02 09:01:08 +08:00
|
|
|
import signal
|
2009-04-11 19:41:35 +08:00
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.client import BaseDatabaseClient
|
2015-06-17 21:37:09 +08:00
|
|
|
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseClient(BaseDatabaseClient):
|
2008-09-09 10:13:58 +08:00
|
|
|
executable_name = 'psql'
|
|
|
|
|
2015-06-17 21:37:09 +08:00
|
|
|
@classmethod
|
2020-10-05 06:25:29 +08:00
|
|
|
def settings_to_cmd_args_env(cls, settings_dict, parameters):
|
2015-06-17 21:37:09 +08:00
|
|
|
args = [cls.executable_name]
|
2020-10-05 06:25:29 +08:00
|
|
|
options = settings_dict.get('OPTIONS', {})
|
|
|
|
|
|
|
|
host = settings_dict.get('HOST')
|
|
|
|
port = settings_dict.get('PORT')
|
2021-02-27 03:53:01 +08:00
|
|
|
dbname = settings_dict.get('NAME')
|
2020-10-05 06:25:29 +08:00
|
|
|
user = settings_dict.get('USER')
|
|
|
|
passwd = settings_dict.get('PASSWORD')
|
2021-02-25 00:16:45 +08:00
|
|
|
passfile = options.get('passfile')
|
2020-12-24 06:39:43 +08:00
|
|
|
service = options.get('service')
|
2020-10-05 06:25:29 +08:00
|
|
|
sslmode = options.get('sslmode')
|
|
|
|
sslrootcert = options.get('sslrootcert')
|
|
|
|
sslcert = options.get('sslcert')
|
|
|
|
sslkey = options.get('sslkey')
|
2015-06-17 21:37:09 +08:00
|
|
|
|
2021-02-27 03:53:01 +08:00
|
|
|
if not dbname and not service:
|
|
|
|
# Connect to the default 'postgres' db.
|
|
|
|
dbname = 'postgres'
|
2015-06-17 21:37:09 +08:00
|
|
|
if user:
|
|
|
|
args += ['-U', user]
|
|
|
|
if host:
|
|
|
|
args += ['-h', host]
|
|
|
|
if port:
|
|
|
|
args += ['-p', str(port)]
|
2021-02-27 03:53:01 +08:00
|
|
|
if dbname:
|
|
|
|
args += [dbname]
|
2020-04-14 15:56:40 +08:00
|
|
|
args.extend(parameters)
|
2015-06-17 21:37:09 +08:00
|
|
|
|
2020-10-05 06:25:29 +08:00
|
|
|
env = {}
|
2019-02-11 09:17:24 +08:00
|
|
|
if passwd:
|
2020-10-05 06:25:29 +08:00
|
|
|
env['PGPASSWORD'] = str(passwd)
|
2020-12-24 06:39:43 +08:00
|
|
|
if service:
|
|
|
|
env['PGSERVICE'] = str(service)
|
2019-04-16 17:40:22 +08:00
|
|
|
if sslmode:
|
2020-10-05 06:25:29 +08:00
|
|
|
env['PGSSLMODE'] = str(sslmode)
|
2019-04-16 17:40:22 +08:00
|
|
|
if sslrootcert:
|
2020-10-05 06:25:29 +08:00
|
|
|
env['PGSSLROOTCERT'] = str(sslrootcert)
|
2019-04-16 17:40:22 +08:00
|
|
|
if sslcert:
|
2020-10-05 06:25:29 +08:00
|
|
|
env['PGSSLCERT'] = str(sslcert)
|
2019-04-16 17:40:22 +08:00
|
|
|
if sslkey:
|
2020-10-05 06:25:29 +08:00
|
|
|
env['PGSSLKEY'] = str(sslkey)
|
2021-02-25 00:16:45 +08:00
|
|
|
if passfile:
|
|
|
|
env['PGPASSFILE'] = str(passfile)
|
2021-04-26 20:19:13 +08:00
|
|
|
return args, (env or None)
|
2020-10-05 06:25:29 +08:00
|
|
|
|
|
|
|
def runshell(self, parameters):
|
|
|
|
sigint_handler = signal.getsignal(signal.SIGINT)
|
2015-06-17 21:37:09 +08:00
|
|
|
try:
|
2017-04-02 09:01:08 +08:00
|
|
|
# Allow SIGINT to pass to psql to abort queries.
|
|
|
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
2020-10-05 06:25:29 +08:00
|
|
|
super().runshell(parameters)
|
2015-06-17 21:37:09 +08:00
|
|
|
finally:
|
2017-11-26 23:27:37 +08:00
|
|
|
# Restore the original SIGINT handler.
|
2017-04-02 09:01:08 +08:00
|
|
|
signal.signal(signal.SIGINT, sigint_handler)
|