Opened 4 years ago

Closed 2 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 Changed 4 years ago by Carlton Gibson

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 Changed 4 years ago by Zijian He

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

comment:3 Changed 4 years ago by Shiva Prasanth

Owner: changed from Zijian He to Shiva Prasanth

comment:4 Changed 4 years ago by Shiva Prasanth

Owner: Shiva Prasanth deleted
Status: assignednew

comment:5 Changed 4 years ago by Artem Skoretskiy

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 Changed 4 years ago by Volodymyr Dmytriiev

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

comment:7 Changed 4 years ago by Artem Skoretskiy

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 Changed 4 years ago by Adam Johnson

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 Changed 4 years ago by introquest

Owner: set to introquest
Status: newassigned

comment:10 Changed 4 years ago by introquest

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

comment:11 Changed 4 years ago by Tim Graham

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 Changed 3 years ago by David Vaz

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 Changed 3 years ago by Claude Paroz

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

comment:14 Changed 2 years ago by Adam Johnson

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

comment:15 Changed 2 years ago by Mariusz Felisiak

Patch needs improvement: set

comment:16 Changed 2 years ago by Adam Johnson

Patch needs improvement: unset

comment:17 Changed 2 years ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:18 Changed 2 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In 8189976:

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

comment:19 Changed 2 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In 8bd9000:

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

comment:20 Changed 2 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In 6cad9116:

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

comment:21 Changed 2 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In 8e8c3f96:

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

comment:22 Changed 2 years ago by Adam Johnson <me@…>

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