diff --git a/.github/workflows/impress.yml b/.github/workflows/impress.yml index 4b7b9acc..6e0d23bf 100644 --- a/.github/workflows/impress.yml +++ b/.github/workflows/impress.yml @@ -143,7 +143,6 @@ jobs: AWS_S3_ENDPOINT_URL: http://localhost:9000 AWS_S3_ACCESS_KEY_ID: impress AWS_S3_SECRET_ACCESS_KEY: password - DB_PSYCOPG_POOL_MAX_SIZE: 15 steps: - name: Checkout repository diff --git a/CHANGELOG.md b/CHANGELOG.md index efac567b..6ffcd73f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,16 @@ and this project adheres to ## [Unreleased] -## [v4.8.0] - 2026-03-13 +### Added + +- 🔧(backend) add DB_PSYCOPG_POOL_ENABLED settings ### Changed - ⬇️(backend) downgrade django-treebeard to version < 5.0.0 +## [v4.8.0] - 2026-03-13 + ### Added - ✨(backend) add a is_first_connection flag to the User model #1938 diff --git a/docs/env.md b/docs/env.md index 04ee1132..cd31f46d 100644 --- a/docs/env.md +++ b/docs/env.md @@ -46,6 +46,7 @@ These are the environment variables you can set for the `impress-backend` contai | DB_NAME | Name of the database | impress | | DB_PASSWORD | Password to authenticate with | pass | | DB_PORT | Port of the database | 5432 | +| DB_PSYCOPG_POOL_ENABLED | Enable or not the psycopg pool configuration in the default database options | False | | DB_PSYCOPG_POOL_MIN_SIZE | The psycopg min pool size | 4 | | DB_PSYCOPG_POOL_MAX_SIZE | The psycopg max pool size | None | | DB_PSYCOPG_POOL_TIMEOUT | The default maximum time in seconds that a client can wait to receive a connection from the pool | 3 | diff --git a/env.d/development/postgresql b/env.d/development/postgresql index 4d08ff97..81daebab 100644 --- a/env.d/development/postgresql +++ b/env.d/development/postgresql @@ -9,4 +9,3 @@ DB_NAME=impress DB_USER=dinum DB_PASSWORD=pass DB_PORT=5432 -DB_PSYCOPG_POOL_MAX_SIZE=15 diff --git a/src/backend/core/tests/test_settings.py b/src/backend/core/tests/test_settings.py index 44c7c8f7..44c2a4ce 100644 --- a/src/backend/core/tests/test_settings.py +++ b/src/backend/core/tests/test_settings.py @@ -28,3 +28,39 @@ def test_invalid_settings_oidc_email_configuration(): "Both OIDC_FALLBACK_TO_EMAIL_FOR_IDENTIFICATION and " "OIDC_ALLOW_DUPLICATE_EMAILS cannot be set to True simultaneously. " ) + + +def test_settings_psycopg_pool_not_enabled(): + """ + Test that not changing DB_PSYCOPG_POOL_ENABLED should not configure psycopg in the DATABASES + settings. + """ + + class TestSettings(Base): + """Fake test settings without enabling psycopg""" + + TestSettings.post_setup() + + assert TestSettings.DATABASES["default"].get("OPTIONS") == {} + + +def test_settings_psycopg_pool_enabled(monkeypatch): + """ + Test when DB_PSYCOPG_POOL_ENABLED is set to True, the psycopg pool options should be present + in the DATABASES OPTIONS. + """ + + monkeypatch.setenv("DB_PSYCOPG_POOL_ENABLED", "True") + + class TestSettings(Base): + """Fake test settings without enabling psycopg""" + + TestSettings.post_setup() + + assert TestSettings.DATABASES["default"].get("OPTIONS") == { + "pool": { + "min_size": 4, + "max_size": None, + "timeout": 3, + } + } diff --git a/src/backend/impress/settings.py b/src/backend/impress/settings.py index fea12040..b8ef63c9 100755 --- a/src/backend/impress/settings.py +++ b/src/backend/impress/settings.py @@ -99,24 +99,7 @@ class Base(Configuration): "localhost", environ_name="DB_HOST", environ_prefix=None ), "PORT": values.Value(5432, environ_name="DB_PORT", environ_prefix=None), - "OPTIONS": { - # https://www.psycopg.org/psycopg3/docs/api/pool.html#psycopg_pool.ConnectionPool - "pool": { - "min_size": values.IntegerValue( - 4, environ_name="DB_PSYCOPG_POOL_MIN_SIZE", environ_prefix=None - ), - "max_size": values.IntegerValue( - None, - environ_name="DB_PSYCOPG_POOL_MAX_SIZE", - environ_prefix=None, - ), - "timeout": values.IntegerValue( - 3, - environ_name="DB_PSYCOPG_POOL_TIMEOUT", - environ_prefix=None, - ), - } - }, + # Psycopg pool can be configured in the post_setup method } } DEFAULT_AUTO_FIELD = "django.db.models.AutoField" @@ -1017,6 +1000,36 @@ class Base(Configuration): "OIDC_ALLOW_DUPLICATE_EMAILS cannot be set to True simultaneously. " ) + psycopg_pool_enabled = values.BooleanValue( + False, environ_name="DB_PSYCOPG_POOL_ENABLED", environ_prefix="" + ) + + if psycopg_pool_enabled: + cls.DATABASES["default"].update( + { + "OPTIONS": { + # https://www.psycopg.org/psycopg3/docs/api/pool.html#psycopg_pool.ConnectionPool + "pool": { + "min_size": values.IntegerValue( + 4, + environ_name="DB_PSYCOPG_POOL_MIN_SIZE", + environ_prefix=None, + ), + "max_size": values.IntegerValue( + None, + environ_name="DB_PSYCOPG_POOL_MAX_SIZE", + environ_prefix=None, + ), + "timeout": values.IntegerValue( + 3, + environ_name="DB_PSYCOPG_POOL_TIMEOUT", + environ_prefix=None, + ), + } + }, + } + ) + class Build(Base): """Settings used when the application is built.