From 2f72df78a4c283f5558f22f171b1c26e0452b37f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Tue, 16 Nov 2021 09:43:03 +0100 Subject: [PATCH] Implement the `config migrate` command. This command moves the old configuration files (if any) into the `config` subdirectory. See --- README.md | 22 ++++++++++------------ bin/pgenv | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d327100..d860e5c 100644 --- a/README.md +++ b/README.md @@ -616,6 +616,7 @@ The `config` command accepts the following subcommands: - `edit` opens the current or specified version configuration file in your favourite text editor (Using `$EDITOR`, e.g: `export EDITOR=/usr/bin/emacs`) - `delete` removes the specified configuration +- `migrate` is a command used to change the configuration format between versions of `pgenv` Each sub-command accepts a PostgreSQL version number (e.g., `10.5`) or a @@ -738,23 +739,20 @@ configuration file, please issue a rename like the following cp .pgenv.conf .pgenv.default.conf ``` -Please note also that, since version `1.2.1` [811ba05], all the configuration files -have been moved into the `config` subdirectory, so to keep them in a single place. -In order to "migrate" your existing configuration, you have to manually -copy all the `.pgenv.*.conf` files into the `config` subdirectory. -The best way to quickly migrate your configuration files to the new -naming scheme, is running a small shell loop as the following one: + +The `migrate` command allows `pgenv` to change the configuration format of +the files between different releases. For example, it must be run if +you are upgrading `pgenv` from a version before `1.2.1` [811ba05], that changed the +location of configuration files into the `config` subdirectory. ``` -for f in .pgenv.*.conf; do - F=$( echo $f | sed 's/\.pgenv\.//' ); mv $f config/$F; -done +pgenv config migrate +Migrated 3 configuration files from previous versions (0 missing) +Your configuration files are now into [~/git/misc/PostgreSQL/pgenv/config] ``` -that has to be run from your `$PGENV_ROOT`, that is from the directory that -contains the `.pgenv.*.conf` files. - + ### pgenv log The `log` command provides a dump of the cluster log, if it exists, so that you diff --git a/bin/pgenv b/bin/pgenv index cf6a9e6..344a0cb 100755 --- a/bin/pgenv +++ b/bin/pgenv @@ -1449,6 +1449,38 @@ EOF configuration_file=$( pgenv_configuration_file_name $v ) case $action in + migrate) + if [ -z "$PGENV_CONFIG_ROOT" ]; then + PGENV_CONFIG_ROOT="$PGENV_ROOT/config" + echo "No configuration directory set, using [$PGENV_CONFIG_ROOT]" + fi + + if [ ! -d "$PGENV_CONFIG_ROOT" ]; then + mkdir "$PGENV_CONFIG_ROOT" + fi + + counter=0 + missing=0 + for old_config_file in "$PGENV_ROOT"/.pgenv.*.conf; do + if [ -f "$old_config_file" ]; then + new_config_file=$( basename "$old_config_file" | sed 's/\.pgenv\.//' ) + new_config_file="$PGENV_CONFIG_ROOT/${new_config_file}" + pgenv_debug "Migrating [$old_config_file] to [$new_config_file]" + mv "$old_config_file" "$new_config_file" + if [ $? -eq 0 ]; then + counter=$(( counter + 1 )) + else + missing=$(( missing + 1 )) + fi + fi + done + + if [ $counter -gt 0 ]; then + echo "Migrated $counter configuration files from previous versions ($missing missing)" + echo "Your configuration files are now into [$PGENV_CONFIG_ROOT]" + fi + ;; + show) pgenv_configuration_dump_or_exit "$v" "$title" ;; init)