jackc / tern

The SQL Fan's Migrator
MIT License
850 stars 66 forks source link

tern psql #78

Closed abs3ntdev closed 1 year ago

abs3ntdev commented 1 year ago

This PR adds a new command tern psql. the command launches psql with a connstring derived from the config files/arguments passed to the command. It will look for PSQL_BIN_PATH for an alternative binary, such as pgcli, to use otherwise it will just use psql connstring.

jackc commented 1 year ago

I'm not sure if this feature is the best approach to sharing configuration between tern and other programs (personally, I use PGSERVICE). But would it make sense to generalize this to something like tern print-connstring?

Then it could be used with psql like this:

$ psql `tern print-connstring`

This would allow passing other arguments to psql as well and using it with other programs that need a connstring.


Also, I'm not sure that the conn string generation is correct. Unless I missed something, it will miss any other arguments like sslmode=require.

abs3ntdev commented 1 year ago

Yeah I totally overlooked arguments. This should account for the values you can provide via config/cli (ssl mode and cert) and it will just use the connstring if its available from the config file or passed via cli so you can pass anything.

As for restructuring to just print out the connstring, I personaly think being able to just use tern to connect is more usable than piping the output of a command to another program but its up to you, I can restructure.

jackc commented 1 year ago

I realize that

$ psql `tern print-connstring`

is not as convenient to type as

$ tern psql

but I think it is worth it because of the additional flexibility to work with any program and even for psql it allows passing arguments.


But one caveat with this whole feature is it is impossible to create a connstring for all connection configurations tern supports. At the very least SSH tunneling will never work, and I suspect there may be other settings as well. The only way to definitely work with every connection config would be to act as a proxy like I considered in https://github.com/jackc/tern/issues/47.

abs3ntdev commented 1 year ago

I changed it to print-connstring instead. I see your points about flexability and the ability to pass additional arguments. Let me know if I can do anything else