Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env file support with retry mechanism #907

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hanefi
Copy link
Contributor

@hanefi hanefi commented Feb 21, 2025

With this change, pgcopydb will read the connection strings from .env file on retrying connections. This allows users to change the connection strings in .env file while pgcopydb is running.

This particular change is useful for users who are using managed identities or other similar solutions, where the connection strings might change during the execution of pgcopydb. On such systems, the passwords are not stored in the environment variables, but managed in the .env file.


Changes Made

  • Added --connection-retry-timeout parameter to stop retrying after N seconds (60 default)

  • Added .env file support that will be fetched from ($XDG_CONFIG_HOME OR $HOME/.config)/pgcopydb/.env

  • following parameters will be read from .env file on init, overriding environment variables, but have lower priority than cli parameters: PGCOPYDB_SOURCE_PGURI PGCOPYDB_TARGET_PGURI PGCOPYDB_TABLE_JOBS PGCOPYDB_INDEX_JOBS PGCOPYDB_RESTORE_JOBS PGCOPYDB_LARGE_OBJECTS_JOBS PGCOPYDB_SPLIT_MAX_PARTS PGCOPYDB_ESTIMATE_TABLE_SIZES PGCOPYDB_SNAPSHOT PGCOPYDB_WAL2JSON_NUMERIC_AS_STRING PGCOPYDB_DROP_IF_EXISTS PGCOPYDB_FAIL_FAST PGCOPYDB_SKIP_VACUUM

  • Added mechanism to re-read .env file and retry with new connection string in pgsql connections.


Description

  • param > .env file > environment variable on init, .env file on retry

  • ($XDG_CONFIG_HOME OR $HOME/.config)/pgcopydb/.env

  • Retry only will fetch connection strings (PGCOPYDB_SOURCE_PGURI, PGCOPYDB_TARGET_PGURI) from .env file


Testing

  • I have a url with a wrong password in my env variable, but my .env file have the correct one. The clone works without problem.

  • I run the clone with timeout variable set to 5, with wrong url in both env file and env variable, after 5 seconds of trying to connect, the process stops retrying.

  • I have a url with a wrong password in my env variable, my .env file also have the wrong password. I run the clone, while it's retrying, I edit the .env file with correct password. In the next try, the clone continues to work as expected.

With this change, pgcopydb will read the connection strings from .env
file on retrying connections. This allows users to change the connection
strings in .env file while pgcopydb is running.

This particular change is useful for users who are using managed
identities or other similar solutions, where the connection strings
might change during the execution of pgcopydb. On such systems, the
passwords are not stored in the environment variables, but managed in
the .env file.

--------------------

Changes Made

* Added `--connection-retry-timeout` parameter to stop retrying after N
  seconds (60 default)

* Added .env file support that will be fetched from ($XDG_CONFIG_HOME OR
  $HOME/.config)/pgcopydb/.env

* following parameters will be read from .env file on init, overriding
  environment variables, but have lower priority than cli parameters:
  PGCOPYDB_SOURCE_PGURI
  PGCOPYDB_TARGET_PGURI
  PGCOPYDB_TABLE_JOBS
  PGCOPYDB_INDEX_JOBS
  PGCOPYDB_RESTORE_JOBS
  PGCOPYDB_LARGE_OBJECTS_JOBS
  PGCOPYDB_SPLIT_MAX_PARTS
  PGCOPYDB_ESTIMATE_TABLE_SIZES
  PGCOPYDB_SNAPSHOT
  PGCOPYDB_WAL2JSON_NUMERIC_AS_STRING
  PGCOPYDB_DROP_IF_EXISTS
  PGCOPYDB_FAIL_FAST
  PGCOPYDB_SKIP_VACUUM

* Added mechanism to re-read .env file and retry with new connection
  string in pgsql connections.

--------------------

Description

* param > .env file > environment variable on init, .env file on retry

* ($XDG_CONFIG_HOME OR $HOME/.config)/pgcopydb/.env

* Retry only will fetch connection strings
  (PGCOPYDB_SOURCE_PGURI, PGCOPYDB_TARGET_PGURI) from .env file

--------------------

Testing

* I have a url with a wrong password in my env variable, but my .env
  file have the correct one. The clone works without problem.

* I run the clone with timeout variable set to 5, with wrong url in both
  env file and env variable, after 5 seconds of trying to connect, the
  process stops retrying.

* I have a url with a wrong password in my env variable, my .env file
  also have the wrong password. I run the clone, while it's retrying, I
  edit the .env file with correct password. In the next try, the clone
  continues to work as expected.
Copy link
Owner

@dimitri dimitri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for your contribution Hanefi!

I left a comment which might be wrong, but on a quick read I was confused. On a higher design note, why using an env file (which looks like something from Docker or other software inspiration) rather than a service file, like Postgres is doing, see https://www.postgresql.org/docs/current/libpq-pgservice.html

We could have our own .ini format (we already have a parser for that format) and parse all our configuration from there, including at connection retry. The filename would probably be pgcopydb.conf rather than .env, too.

Comment on lines +218 to +229
if (env_exists("XDG_CONFIG_HOME"))
{
char *configHome;
get_env_dup("XDG_CONFIG_HOME", &configHome);
sformat(envFilePath, sizeof(envFilePath), "%s/pgcopydb/.env", configHome);
}
else if (env_exists("HOME"))
{
char *homeDir;
get_env_dup("HOME", &homeDir);
sformat(envFilePath, sizeof(envFilePath), "%s/.config/pgcopydb/.env", homeDir);
}
Copy link
Owner

@dimitri dimitri Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have a XDG_CONFIG_HOME environment variable defined, then we never look at te HOME path, even if the file happens to only exists there. I don't think that's the intended behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants