From fa7e306b1ac62fda38e22e9edd384786cd36e593 Mon Sep 17 00:00:00 2001 From: Joris van der Wel Date: Wed, 18 Jan 2023 13:03:05 +0100 Subject: [PATCH 1/2] Add missing psycopg2.extras import On some of our systems the `postgresql_query` task failed with the following error: AttributeError: module 'psycopg2' has no attribute 'extras' Applying the patch from this commit fixed the issue for me on those systems. This was previously reported in issue #283. However now that some time has passed, I am unable to reproduce the issue on our newer systems. In any case, this is still a mistake that could cause problems in the future, so I think it would be better to fix it. The reason that this mistake is not causing any actual errors in normal situations is because of how the python import system functions: All of the entrypoints (all the source files under plugins/modules) have an import for `psycopg2.extras`. This causes `extras` to be available on `psycopg2` for any code (in any module) that executes after that import statement, such as the function bodies in `module_utils/postgres.py`. Fixes #283 --- plugins/module_utils/postgres.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/module_utils/postgres.py b/plugins/module_utils/postgres.py index 887c61db..6d7c725b 100644 --- a/plugins/module_utils/postgres.py +++ b/plugins/module_utils/postgres.py @@ -18,6 +18,7 @@ psycopg2 = None # This line needs for unit tests try: import psycopg2 + import psycopg2.extras HAS_PSYCOPG2 = True except ImportError: HAS_PSYCOPG2 = False From 10c6e324391d219760820ecd1d2a523219eeb740 Mon Sep 17 00:00:00 2001 From: Joris van der Wel Date: Fri, 27 Jan 2023 16:24:19 +0100 Subject: [PATCH 2/2] Add changelog fragment --- changelogs/fragments/399-missing-import.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/399-missing-import.yml diff --git a/changelogs/fragments/399-missing-import.yml b/changelogs/fragments/399-missing-import.yml new file mode 100644 index 00000000..94eefdf0 --- /dev/null +++ b/changelogs/fragments/399-missing-import.yml @@ -0,0 +1,2 @@ +bugfixes: +- postgresql_query - could crash under certain conditions because of a missing import to `psycopg2.extras` (https://github.com/ansible-collections/community.postgresql/issues/283).