Skip to content

Commit

Permalink
Add a switch command to pgenv (#53)
Browse files Browse the repository at this point in the history
Add `switch` command.

The switch command acts mostly like use, however, it does
not manage any database for you, it simply changes the version
linked and thus in the path of the user.
It is implemented as a subcase of `use` that will not start the selected instance.
Documentation updated.
  • Loading branch information
thanodnl authored May 11, 2022
1 parent 85d0e4c commit 6612ffe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Synopsis

# Build PostgreSQL server
pgenv build 10.4

# Switch PostgreSQL version
pgenv switch 10.4

# Use PostgreSQL version
pgenv use 10.4
Expand Down Expand Up @@ -234,6 +237,20 @@ pgenv use latest 10
will select the most recent PostgreSQL version of the 10 series installed.
### pgenv switch
Sets the version of PostgreSQL to be used in all shells by symlinking its
directory to `$PGENV_ROOT/pgsql`. Contrary to `pgenv use` this command does
_not_ manage a database for you. Meaning, it will not start, stop and
initialize a postgres database with the given version. Instead it simply
changes the environment to a different version of PostgreSQL. This can be
useful if the user has other tools to automate the provisioning and lifecycle
of a database.
```
$ pgenv switch 10.4
```
### pgenv versions
Lists all PostgreSQL versions known to `pgenv`, and shows an asterisk next to
Expand Down Expand Up @@ -571,10 +588,11 @@ Usage: pgenv <command> [<args>]
The pgenv commands are:
use Set and start the current PostgreSQL version
clear Stop and unset the current PostgreSQL version
start Start the current PostgreSQL server
stop Stop the current PostgreSQL server
restart Restart the current PostgreSQL server
switch Set the current PostgreSQL version
clear Stop and unset the current PostgreSQL version
build Build a specific version of PostgreSQL
rebuild Re-build a specific version of PostgreSQL
remove Remove a specific version of PostgreSQL
Expand Down
20 changes: 14 additions & 6 deletions bin/pgenv
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ Usage: pgenv <command> [<args>]
The pgenv commands are:
use Set and start the current PostgreSQL version
clear Stop and unset the current PostgreSQL version
start Start the current PostgreSQL server
stop Stop the current PostgreSQL server
restart Restart the current PostgreSQL server
switch Set the current PostgreSQL version
clear Stop and unset the current PostgreSQL version
build Build a specific version of PostgreSQL
rebuild Re-build a specific version of PostgreSQL
remove Remove a specific version of PostgreSQL
Expand Down Expand Up @@ -1034,7 +1035,10 @@ pgenv_patch_source_tree() {
}

case $1 in
use)
use|switch)
# save the 'command' to check if we should initialize and start the database or not
command=$1

v=$( pgenv_guess_postgresql_version $2 $3)
pgenv_input_version_or_exit "$v" 'show-installed-versions' 'build'
pgenv_installed_version_or_exit $v
Expand All @@ -1057,11 +1061,15 @@ case $1 in
ln -nsf pgsql-$v pgsql
fi

# Init if needed.
pgenv_initdb
# only start the database if the user specified the need to use the database
if [ "$command" == 'use' ]; then
# Init if needed.
pgenv_initdb

# start the instance
pgenv_start_instance
fi

# start the instance
pgenv_start_instance
exit
;;

Expand Down

0 comments on commit 6612ffe

Please sign in to comment.