Opened 7 years ago

Closed 5 years ago

#29501 closed New feature (fixed)

Add support for ./manage.py dbshell -c SQL

Reported by: Artem Skoretskiy Owned by: Adam Johnson
Component: Core (Management commands) Version: dev
Severity: Normal Keywords:
Cc: tonn81@…, Zijian He Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

At the moment you cannot run specific SQL directly with dbshell:

./manage.py dbshell -c "select * from auth_group"

You have to use pipes, that are not always convenient:

echo "select * from auth_group" | ./manage.py dbshell

If we add -c argument, it would be in sync with shell command, where you could provide commands already.

Change History (22)

comment:1 by Carlton Gibson, 7 years ago

Triage Stage: UnreviewedAccepted
Version: 2.0master

This seems reasonable, at first glance at least. The trick will be to pass the command to each of the backend clients correctly, but there's no reason that's not possible right? Accepting on that basis. A PR is very welcome.

comment:2 by Zijian He, 7 years ago

Cc: Zijian He added
Owner: changed from nobody to Zijian He
Status: newassigned

comment:3 by Shiva Prasanth, 7 years ago

Owner: changed from Zijian He to Shiva Prasanth

comment:4 by Shiva Prasanth, 7 years ago

Owner: Shiva Prasanth removed
Status: assignednew

comment:5 by Artem Skoretskiy, 7 years ago

It should be easy to implement since we don’t need to use database-specific backend

Something like that:

from django.db import connection

if command:
    cursor = connection.cursor()
    cursor.execute(command)
    # show `cursor.fetchall()` result

comment:6 by Volodymyr Dmytriiev, 6 years ago

any progress on this one? Is the previously suggested approach good enough? If so, I'd love to give a try?

comment:7 by Artem Skoretskiy, 6 years ago

No progress AKAIK.

Maybe it would be easier to call database-specific CLI tool with right parameters than run cursor.

Pros:

  • No need to care about printing results (user could execute SELECT)
  • Could run commands that are unique for the CLI (e.g. \dt to show tables in psql)

Cons:

  • Each backend would need its own command to run (but we already have dbshell, so we know name of binary; just need a parameter to execute a query).
  • Each backend would show results in its own format, so you cannot e. g. save result as cab file)

Another completely different approach would be to pass all command line arguments provided to dbshell, e.g. after “—“ divider to database CLI app. For example:

./manage.py dbshell — -c “select * from auth_user”

comment:8 by Adam Johnson, 6 years ago

I don't think opening a cursor is the right way. My mental model of dbshell is that it always passes through to the underlying tool, which can have subtle implications on even what SQL/commands are accepted.

I am actually most in favour of passing all arguments through after a -- divider as that is the most flexible.

comment:9 by introquest, 6 years ago

Owner: set to introquest
Status: newassigned

comment:10 by introquest, 6 years ago

Has patch: set
Needs documentation: set
Needs tests: set
Triage Stage: AcceptedReady for checkin

comment:11 by Tim Graham, 6 years ago

Triage Stage: Ready for checkinAccepted

"Ready for checkin" is set by a patch reviewer. And the patch isn't ready for review without tests and documentation.

comment:12 by David Vaz, 5 years ago

So the PR is closed due to missing Tests and Documentation. Does anybody care that I take this?
I could make a PR based on the original one and add tests and docs

comment:13 by Claude Paroz, 5 years ago

Absolutely, feel free to work on issues obviously stalled. Just credit original patch writers if appropriate.

comment:14 by Adam Johnson, 5 years ago

Needs documentation: unset
Needs tests: unset
Owner: changed from introquest to Adam Johnson
Last edited 5 years ago by Mariusz Felisiak (previous) (diff)

comment:15 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:16 by Adam Johnson, 5 years ago

Patch needs improvement: unset

comment:17 by Mariusz Felisiak, 5 years ago

Triage Stage: AcceptedReady for checkin

comment:18 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 8189976:

Refs #29501 -- Added test for missing dbshell executable.

comment:19 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 8bd9000:

Refs #29501 -- Made dbshell catch more specific FileNotFoundError.

comment:20 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 6cad9116:

Refs #29501 -- Simplified BaseCommand.run_from_argv() a bit.

comment:21 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 8e8c3f96:

Refs #29501 -- Allowed customizing exit status for management commands.

comment:22 by Adam Johnson <me@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 5b884d45:

Fixed #29501 -- Allowed dbshell to pass options to underlying tool.

Note: See TracTickets for help on using tickets.
Back to Top