From 6323da1cb0f3f7fa02dc49c1ce5945a411975600 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Mon, 6 Sep 2021 13:12:39 +0200 Subject: [PATCH 1/3] postgresql_info: fix crash when extension version is 0. --- plugins/modules/postgresql_ext.py | 12 +++-- plugins/modules/postgresql_info.py | 14 +++++- .../tasks/postgresql_ext_version_opt.yml | 47 +++++++++++++++++++ .../setup_postgresql_db/files/dummy--0.sql | 2 + .../setup_postgresql_db/tasks/main.yml | 2 + 5 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 tests/integration/targets/setup_postgresql_db/files/dummy--0.sql diff --git a/plugins/modules/postgresql_ext.py b/plugins/modules/postgresql_ext.py index 7514a0c0..579c59b7 100644 --- a/plugins/modules/postgresql_ext.py +++ b/plugins/modules/postgresql_ext.py @@ -276,7 +276,7 @@ def ext_get_versions(cursor, ext): query = ("SELECT extversion FROM pg_catalog.pg_extension " "WHERE extname = %(ext)s") - current_version = '0' + current_version = None cursor.execute(query, {'ext': ext}) res = cursor.fetchone() if res: @@ -290,7 +290,7 @@ def ext_get_versions(cursor, ext): available_versions = parse_ext_versions(current_version, res) - if current_version == '0': + if current_version is None: current_version = False return (current_version, available_versions) @@ -314,8 +314,12 @@ def parse_ext_versions(current_version, ext_ver_list): continue try: - if LooseVersion(line['version']) > LooseVersion(current_version): - available_versions.append(line['version']) + if current_version is None: + if LooseVersion(line['version']) >= LooseVersion('0'): + available_versions.append(line['version']) + else: + if LooseVersion(line['version']) > LooseVersion(current_version): + available_versions.append(line['version']) except Exception: # When a version cannot be compared, skip it # (there's a note in the documentation) diff --git a/plugins/modules/postgresql_info.py b/plugins/modules/postgresql_info.py index f8ea5253..edb7d980 100644 --- a/plugins/modules/postgresql_info.py +++ b/plugins/modules/postgresql_info.py @@ -197,7 +197,7 @@ type: dict sample: - { "plpgsql": { "description": "PL/pgSQL procedural language", - "extversion": { "major": 1, "minor": 0 } } } + "extversion": { "major": 1, "minor": 0, "raw": '1.0' } } } contains: extdescription: description: Extension description. @@ -219,6 +219,11 @@ returned: always type: int sample: 0 + raw: + description: Extension full version. + returned: always + type: str + sample: '1.0' nspname: description: Namespace where the extension is. returned: always @@ -746,12 +751,17 @@ def get_ext_info(self): res = self.__exec_sql(query) ext_dict = {} for i in res: + ext_ver_raw = i[1] ext_ver = i[1].split('.') + if len(ext_ver) < 2: + ext_ver.append(None) + ext_dict[i[0]] = dict( extversion=dict( major=int(ext_ver[0]), - minor=int(ext_ver[1]), + minor=int(ext_ver[1]) if ext_ver[1] else None, + raw=ext_ver_raw, ), nspname=i[2], description=i[3], diff --git a/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_version_opt.yml b/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_version_opt.yml index f90340c5..72776450 100644 --- a/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_version_opt.yml +++ b/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_version_opt.yml @@ -347,6 +347,53 @@ that: - result is changed + # https://github.com/ansible-collections/community.postgresql/issues/137 + - name: Drop extension + <<: *task_parameters + postgresql_ext: + <<: *pg_parameters + name: '{{ test_ext }}' + schema: '{{ test_schema }}' + state: absent + + - name: Non standard version + <<: *task_parameters + postgresql_ext: + <<: *pg_parameters + name: '{{ test_ext }}' + schema: '{{ test_schema }}' + version: 0 + + - name: Test + <<: *task_parameters + postgresql_info: + <<: *pg_parameters + + - assert: + that: + - result['databases']['postgres']['extensions']['dummy']['extversion']['major'] == 0 + - result['databases']['postgres']['extensions']['dummy']['extversion']['minor'] == None + - result['databases']['postgres']['extensions']['dummy']['extversion']['raw'] == '0' + + - name: Upgrade extension to the latest + <<: *task_parameters + postgresql_ext: + <<: *pg_parameters + name: '{{ test_ext }}' + schema: '{{ test_schema }}' + version: latest + + - name: Test + <<: *task_parameters + postgresql_info: + <<: *pg_parameters + + - assert: + that: + - result['databases']['postgres']['extensions']['dummy']['extversion']['major'] == 3 + - result['databases']['postgres']['extensions']['dummy']['extversion']['minor'] == 0 + - result['databases']['postgres']['extensions']['dummy']['extversion']['raw'] == '3.0' + # Cleanup: - name: postgresql_ext_version - drop the extension <<: *task_parameters diff --git a/tests/integration/targets/setup_postgresql_db/files/dummy--0.sql b/tests/integration/targets/setup_postgresql_db/files/dummy--0.sql new file mode 100644 index 00000000..626b0fb4 --- /dev/null +++ b/tests/integration/targets/setup_postgresql_db/files/dummy--0.sql @@ -0,0 +1,2 @@ +CREATE OR REPLACE FUNCTION dummy_display_ext_version() +RETURNS text LANGUAGE SQL AS 'SELECT (''0'')::text'; diff --git a/tests/integration/targets/setup_postgresql_db/tasks/main.yml b/tests/integration/targets/setup_postgresql_db/tasks/main.yml index 49fa0f04..1e3e4343 100644 --- a/tests/integration/targets/setup_postgresql_db/tasks/main.yml +++ b/tests/integration/targets/setup_postgresql_db/tasks/main.yml @@ -217,6 +217,7 @@ dest: /usr/share/postgresql/{{ pg_ver }}/extension/{{ item }} mode: '0444' with_items: + - dummy--0.sql - dummy--1.0.sql - dummy--2.0.sql - dummy--3.0.sql @@ -228,6 +229,7 @@ mode: '0444' state: touch with_items: + - dummy--0--1.0.sql - dummy--1.0--2.0.sql - dummy--2.0--3.0.sql when: ansible_os_family == 'Debian' From 9919cd35dea548872b904204531b4e2651bdd963 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Mon, 6 Sep 2021 13:19:42 +0200 Subject: [PATCH 2/3] Add a changelog fragment --- .../fragments/138-postgresql_info_fix_ver_handling.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelogs/fragments/138-postgresql_info_fix_ver_handling.yml diff --git a/changelogs/fragments/138-postgresql_info_fix_ver_handling.yml b/changelogs/fragments/138-postgresql_info_fix_ver_handling.yml new file mode 100644 index 00000000..9c41b583 --- /dev/null +++ b/changelogs/fragments/138-postgresql_info_fix_ver_handling.yml @@ -0,0 +1,6 @@ +bugfixes: +- postgresql_info - Fix extension version handling when it has 0 value (https://github.com/ansible-collections/community.postgresql/issues/137). +- postgresql_ext - Fix extension version handling when it has 0 value (https://github.com/ansible-collections/community.postgresql/pull/138). + +minor_changes: +- postgresql_info - Add the ``raw`` return value for extension version (https://github.com/ansible-collections/community.postgresql/pull/138). From 999a4388e42f3f241506be775861da2209134f17 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Tue, 7 Sep 2021 08:13:58 +0300 Subject: [PATCH 3/3] Update changelogs/fragments/138-postgresql_info_fix_ver_handling.yml Co-authored-by: Tobias Birkefeld --- changelogs/fragments/138-postgresql_info_fix_ver_handling.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/138-postgresql_info_fix_ver_handling.yml b/changelogs/fragments/138-postgresql_info_fix_ver_handling.yml index 9c41b583..75a3300e 100644 --- a/changelogs/fragments/138-postgresql_info_fix_ver_handling.yml +++ b/changelogs/fragments/138-postgresql_info_fix_ver_handling.yml @@ -1,6 +1,6 @@ bugfixes: - postgresql_info - Fix extension version handling when it has 0 value (https://github.com/ansible-collections/community.postgresql/issues/137). -- postgresql_ext - Fix extension version handling when it has 0 value (https://github.com/ansible-collections/community.postgresql/pull/138). +- postgresql_ext - Fix extension version handling when it has 0 value (https://github.com/ansible-collections/community.postgresql/issues/136). minor_changes: - postgresql_info - Add the ``raw`` return value for extension version (https://github.com/ansible-collections/community.postgresql/pull/138).