Adding a script positional argument to dbshell

171 views
Skip to first unread message

dans...@gmail.com

unread,
Jul 5, 2022, 12:12:02 PM7/5/22
to Django developers (Contributions to Django itself)
In Adam's blog post https://adamj.eu/tech/2022/06/29/run-a-django-migration-by-hand/, he shows a pattern I've used in the past.  

Would it be useful to support passing an optional positional argument to dbshell that is then passed to the dbshell program appropriately?   e.g., with @filename for Oracle sqlplus or directly to mysql?  I suppose that might introduce new code to DatabaseOperations, but not too much.   I think only  Oracle's DatabaseOperations would be different.

I've had to extend Django to run some old PL/SQL via management commands.  This helped me with my 16 year old database to replace scripts that used combinations of bash, ksh, sh, expect, perl, sqlplus, and PL/SQL with just two three technologies - namely Python, Django, and PL/SQL.  However, since there are 20k lines of PL/SQL and 25k lines of Python, making the PL/SQL go away is not yet a priority.

I've done it basically by using code like this in a management command:


If you look at the "run_report" method of the basereport.py, you'll see that I am using os.pipe() and os.dup2() to enable passing the output of a template to dbshell.  This is in case there is any sqlplus reliance in these old reports.  In my case, it will be better to assure that all of this report code can run in management commands or directly via cursor execute - and so I haven't proposed this for my use case.

The one that Adam proposes makes a much better use case for this additional positional argument to dbshell.

Has this been discussed before?

Adam Johnson

unread,
Jul 8, 2022, 4:32:10 AM7/8/22
to Django developers (Contributions to Django itself)
Hi

dbshell already passes all unknown arguments through to the underlying program. For example on postgres you can use the psql '-c' option like so:

$ ./manage.py dbshell -c 'select 123'
 ?column?
----------
      123
(1 row)

argparse allows a -- separator to split arguments for dbshell and the underlying program, so you can even use "shadowed" options like --help like so:

$ ./manage.py dbshell -- --help
psql is the PostgreSQL interactive terminal.

Usage:
  psql [OPTION]... [DBNAME [USERNAME]]

General options:
...

This should allow you to pass your scripts through to the oracle tool, if I understand your request correctly.

Thanks,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/aeb4ef0d-e8d6-46c6-9aaa-167abd5de3e5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages