Skip to content

CLI: command to execute arbitrary SQL #1375

Open
@abonander

Description

@abonander

I'm surprised this hasn't been requested on the issue tracker yet.

If you want to just run a SQL snippet on your database, currently you have to reach for something database-specific like psql. I personally use CLion's Database tool, but that can't be used in scripts and is a bit daunting to configure.

Of course, it would also be super convenient to have a CLI SQL client that uses the DATABASE_URL that's already set in your .env, which you don't get with psql.

I'm thinking, for an MVP of this:

sqlx exec: connect to DATABASE_URL and read from stdin a single statement terminated with a semicolon or EOF. This will probably be the most convenient way to directly invoke it in CLI.

sqlx exec -m: connect to DATABASE_URL and read stdin until EOF and issue the whole input as a multi-statement command. For sanity, sqlx exec should assume this flag in piped input mode instead of terminating on the first semicolon.

sqlx exec -c <quoted-statement> [bind-params...]: execute quoted-statement with CLI-delimited bind parameters (optional)

sqlx exec -f <file-path> [bind-params...]: load file-path as a SQL statement and execute with CLI-delimited bind parameters (optional)

All these options should support overriding the database URL, of course. We already have precedent for that with other subcommands.

Not sure what I want the output format to be by default. Similar to psql's, I guess? As a bonus feature we should be capable of outputting CSV or JSON as well, maybe with -o csv or -o json.

Also not really thinking about turning this into a REPL just yet, I'd like to keep this simple with an aim towards scripting and composability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions