From 679a6866278dea4dbe6c20d1fcc4391014cba46a Mon Sep 17 00:00:00 2001 From: Artem Sheptunov <106321977+Infindery@users.noreply.github.com> Date: Mon, 22 Sep 2025 13:11:08 +0500 Subject: [PATCH 1/6] refactor(odb): #22: change how credentials for a database connection string are obtained using environment variables --- src/main.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 794a34e..6f08590 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,14 +14,13 @@ int main() { // TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/27): add migration update on startup - // TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/22): parse that variables from .env or .env.dev - const std::string user = "postgres"; - const std::string password = "admin"; - const std::string db_name = "to-dos-api-cpp_db"; - const std::string host = "localhost"; - const unsigned port = 5432; + const std::string user = std::getenv("POSTGRES_USER"); + const std::string password = std::getenv("POSTGRES_PASSWORD"); + const std::string db_name = std::getenv("POSTGRES_DB"); + const std::string host = std::getenv("POSTGRES_HOST"); + const std::string port = std::getenv("POSTGRES_PORT"); - std::string conninfo = "host=" + host + " port=" + std::to_string(port) + " dbname=" + db_name + " user=" + user + " password=" + password; + std::string conninfo = "host=" + host + " port=" + port + " dbname=" + db_name + " user=" + user + " password=" + password; std::unique_ptr db(new odb::pgsql::database(conninfo)); From 3bde1f1252cf4f820492d894b89fd450c4981a6e Mon Sep 17 00:00:00 2001 From: Artem Sheptunov <106321977+Infindery@users.noreply.github.com> Date: Mon, 22 Sep 2025 13:14:40 +0500 Subject: [PATCH 2/6] docs(odb): #22: add a clause about the need to extract environment variables to the "Project run" section --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 34c6b7d..cbbbf22 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ When the project configuration is finished, click Build to build the project. #### Before launching web server: - Run the database container via docker compose command `docker compose -f docker-compose.dev.yaml up -d` from workspace. +- Import environment variables declared in the `.env.dev` file while in the dev container and using the command `export $(grep -v '^#' .env.dev | xargs)`. If the file containing the environment variables is named something other than .env.dev, you should modify the command to specify the correct name. To launch the executable, click Launch in the CMake extension.

cmakeLaunch

From 55b5a79a49e7baec0cc24b335bdcc7d861cbc20e Mon Sep 17 00:00:00 2001 From: Artem Sheptunov <106321977+Infindery@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:28:06 +0500 Subject: [PATCH 3/6] refactor(alembic): #22: change the logic for generate a database connection string, add a step for extract credentials from environment variables --- src/data/alembic.ini | 8 -------- src/data/migrations/env.py | 11 ++++++++++- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/data/alembic.ini b/src/data/alembic.ini index 8666a89..fe66372 100644 --- a/src/data/alembic.ini +++ b/src/data/alembic.ini @@ -81,14 +81,6 @@ path_separator = os # are written from script.py.mako # output_encoding = utf-8 -# database URL. This is consumed by the user-maintained env.py script only. -# other means of configuring database URLs may be customized within the env.py -# file. - -# TODO(https://github.com/TourmalineCore/to-dos-api-cpp/issues/22): parse connection data variables from .env or .env.dev -sqlalchemy.url = postgresql://postgres:admin@localhost/to-dos-api-cpp_db - - [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run # on newly generated revision scripts. See the documentation for further diff --git a/src/data/migrations/env.py b/src/data/migrations/env.py index bcdb47f..9d034ea 100644 --- a/src/data/migrations/env.py +++ b/src/data/migrations/env.py @@ -29,6 +29,15 @@ # my_important_option = config.get_main_option("my_important_option") # ... etc. +def get_database_url(): + """Getting the Database URL from Environment Variables""" + user = os.getenv('POSTGRES_USER') + password = os.getenv('POSTGRES_PASSWORD') + host = os.getenv('POSTGRES_HOST') + port = os.getenv('POSTGRES_PORT') + name = os.getenv('POSTGRES_DB') + + return f"postgresql://{user}:{password}@{host}:{port}/{name}" def run_migrations_offline() -> None: """Run migrations in 'offline' mode. @@ -42,7 +51,7 @@ def run_migrations_offline() -> None: script output. """ - url = config.get_main_option("sqlalchemy.url") + url = get_database_url() context.configure( url=url, target_metadata=target_metadata, From 4461eea4f86da3d7795f727100e9bc54714d047f Mon Sep 17 00:00:00 2001 From: Artem Sheptunov <106321977+Infindery@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:29:57 +0500 Subject: [PATCH 4/6] refactor(env): #22: delete the .env.dev file, switch to a single .env file format, add .env.example --- .env.dev | 5 ----- .env.example | 6 ++++++ docker-compose.dev.yaml => docker-compose.yml | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) delete mode 100644 .env.dev create mode 100644 .env.example rename docker-compose.dev.yaml => docker-compose.yml (86%) diff --git a/.env.dev b/.env.dev deleted file mode 100644 index 6b0c291..0000000 --- a/.env.dev +++ /dev/null @@ -1,5 +0,0 @@ -POSTGRES_HOST=localhost -POSTGRES_PORT=5432 -POSTGRES_DB=to-dos-api-cpp_db -POSTGRES_USER=postgres -POSTGRES_PASSWORD=admin \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..ba3f92e --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +# Database +POSTGRES_HOST=tobemodified +POSTGRES_PORT=tobemodified +POSTGRES_DB=tobemodified +POSTGRES_USER=tobemodified +POSTGRES_PASSWORD=tobemodified \ No newline at end of file diff --git a/docker-compose.dev.yaml b/docker-compose.yml similarity index 86% rename from docker-compose.dev.yaml rename to docker-compose.yml index 5aeaa82..37cd437 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.yml @@ -4,11 +4,11 @@ services: container_name: postgresql image: postgres:14-alpine env_file: - - .env.dev + - .env volumes: - postgresql-data:/var/lib/postgresql/data ports: - "5432:5432" volumes: - postgresql-data: \ No newline at end of file + postgresql-data: From e343f85f9250dbc0540c0733b66f417663a68856 Mon Sep 17 00:00:00 2001 From: Artem Sheptunov <106321977+Infindery@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:52:00 +0500 Subject: [PATCH 5/6] fix(alembic): #22: fix a database connection error in online mode, add database URL substitution in online and offline modes --- src/data/migrations/env.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/data/migrations/env.py b/src/data/migrations/env.py index 9d034ea..f960b3f 100644 --- a/src/data/migrations/env.py +++ b/src/data/migrations/env.py @@ -39,6 +39,8 @@ def get_database_url(): return f"postgresql://{user}:{password}@{host}:{port}/{name}" +url = get_database_url() + def run_migrations_offline() -> None: """Run migrations in 'offline' mode. @@ -51,7 +53,6 @@ def run_migrations_offline() -> None: script output. """ - url = get_database_url() context.configure( url=url, target_metadata=target_metadata, @@ -70,6 +71,8 @@ def run_migrations_online() -> None: and associate a connection with the context. """ + config.set_main_option('sqlalchemy.url', url) + connectable = engine_from_config( config.get_section(config.config_ini_section, {}), prefix="sqlalchemy.", From 117fd23afc5b29e279a9768643b4a5e32e08b265 Mon Sep 17 00:00:00 2001 From: Artem Sheptunov <106321977+Infindery@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:31:37 +0500 Subject: [PATCH 6/6] docs(env): #22: change the instructions for retrieve environment variables accord to their current storage state docs: #22: correctof the docker-compose file name in the instructions, accord to the current state docs: #22: correctof the docker-compose file name in the instructions, accord to the current state --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cbbbf22..d88f327 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ When the project configuration is finished, click Build to build the project. ### Project run #### Before launching web server: -- Run the database container via docker compose command `docker compose -f docker-compose.dev.yaml up -d` from workspace. -- Import environment variables declared in the `.env.dev` file while in the dev container and using the command `export $(grep -v '^#' .env.dev | xargs)`. If the file containing the environment variables is named something other than .env.dev, you should modify the command to specify the correct name. +- Run the database container via docker compose command `docker compose up -d` from workspace. +- Import environment variables declared in the `.env` file while in the dev container and using the command `export $(grep -v '^#' .env | xargs)`. If the file containing the environment variables is named something other than `.env`, you should modify the command to specify the correct name. To launch the executable, click Launch in the CMake extension.

cmakeLaunch