1+ import subprocess
2+
13from django .core .management .base import BaseCommand , CommandError
24from django .db import DEFAULT_DB_ALIAS , connections
35
@@ -15,11 +17,13 @@ def add_arguments(self, parser):
1517 '--database' , default = DEFAULT_DB_ALIAS ,
1618 help = 'Nominates a database onto which to open a shell. Defaults to the "default" database.' ,
1719 )
20+ parameters = parser .add_argument_group ('parameters' , prefix_chars = '--' )
21+ parameters .add_argument ('parameters' , nargs = '*' )
1822
1923 def handle (self , ** options ):
2024 connection = connections [options ['database' ]]
2125 try :
22- connection .client .runshell ()
26+ connection .client .runshell (options [ 'parameters' ] )
2327 except FileNotFoundError :
2428 # Note that we're assuming the FileNotFoundError relates to the
2529 # command missing. It could be raised for some other reason, in
@@ -29,3 +33,11 @@ def handle(self, **options):
2933 'You appear not to have the %r program installed or on your path.' %
3034 connection .client .executable_name
3135 )
36+ except subprocess .CalledProcessError as e :
37+ raise CommandError (
38+ '"%s" returned non-zero exit status %s.' % (
39+ ' ' .join (e .cmd ),
40+ e .returncode ,
41+ ),
42+ returncode = e .returncode ,
43+ )
0 commit comments