2015-06-17 21:37:09 +08:00
|
|
|
import os
|
2017-04-02 09:01:08 +08:00
|
|
|
import signal
|
2014-03-09 19:00:42 +08:00
|
|
|
import subprocess
|
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
|
2016-06-03 05:03:03 +08:00
|
|
|
def runshell_db(cls, conn_params):
|
2015-06-17 21:37:09 +08:00
|
|
|
args = [cls.executable_name]
|
|
|
|
|
2016-06-03 05:03:03 +08:00
|
|
|
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', '')
|
2015-06-17 21:37:09 +08:00
|
|
|
|
|
|
|
if user:
|
|
|
|
args += ['-U', user]
|
|
|
|
if host:
|
|
|
|
args += ['-h', host]
|
|
|
|
if port:
|
|
|
|
args += ['-p', str(port)]
|
2016-06-03 05:03:03 +08:00
|
|
|
args += [dbname]
|
2015-06-17 21:37:09 +08:00
|
|
|
|
2017-04-02 09:01:08 +08:00
|
|
|
sigint_handler = signal.getsignal(signal.SIGINT)
|
2019-02-11 09:17:24 +08:00
|
|
|
subprocess_env = os.environ.copy()
|
|
|
|
if passwd:
|
|
|
|
subprocess_env['PGPASSWORD'] = str(passwd)
|
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)
|
2019-02-11 09:17:24 +08:00
|
|
|
subprocess.run(args, check=True, env=subprocess_env)
|
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)
|
2015-06-17 21:37:09 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def runshell(self):
|
2016-06-03 05:03:03 +08:00
|
|
|
DatabaseClient.runshell_db(self.connection.get_connection_params())
|