diff --git a/plugins/modules/clickhouse_db.py b/plugins/modules/clickhouse_db.py index edc7442..0bfc3d1 100644 --- a/plugins/modules/clickhouse_db.py +++ b/plugins/modules/clickhouse_db.py @@ -114,6 +114,8 @@ def __init__(self, module, client, name): self.__populate_info() def __populate_info(self): + # TODO: If anyone can determine the version when the comment feature + # was added to database more precisely, you're welcome to adjust it here if self.srv_version['year'] >= 22: # The comment is not supported in all versions query = ("SELECT engine, comment " @@ -150,6 +152,9 @@ def create(self, engine, comment): return True def update(self, engine, comment): + # IMPORTANT: In case in future any items here can change + # please add check_mode handling + # There's no way to change the engine # so just inform the users they have to recreate # the DB in order to change them @@ -220,6 +225,12 @@ def main(): changed = False database = ClickHouseDB(module, client, name) + if comment and database.srv_version['year'] < 22: + msg = ('The module supports the comment feature for ClickHouse ' + 'versions equal to or higher than 22.*. Ignored.') + module.warn(msg) + comment = None + if state == 'present': if not database.exists: changed = database.create(engine, comment) diff --git a/tests/integration/targets/clickhouse_db/tasks/initial.yml b/tests/integration/targets/clickhouse_db/tasks/initial.yml index f2f6943..0fc7166 100644 --- a/tests/integration/targets/clickhouse_db/tasks/initial.yml +++ b/tests/integration/targets/clickhouse_db/tasks/initial.yml @@ -222,3 +222,90 @@ that: - result is not changed - result.executed_statements == [] + +# Get srv version for the following block +- name: Get server version + register: server_info + community.clickhouse.clickhouse_info: + limit: version + +- name: Test comment + when: server_info['version']['year'] >= 22 + block: + + # Test + - name: Create database in check mode with comment + register: result + check_mode: true + community.clickhouse.clickhouse_db: + state: present + name: test_db + comment: Test DB + + - name: Check ret values + ansible.builtin.assert: + that: + - result is changed + - result.executed_statements == ["CREATE DATABASE test_db COMMENT 'Test DB'"] + + # Test + - name: Create database with comment real mode + register: result + community.clickhouse.clickhouse_db: + state: present + name: test_db + comment: Test DB + + - name: Check ret values + ansible.builtin.assert: + that: + - result is changed + - result.executed_statements == ["CREATE DATABASE test_db COMMENT 'Test DB'"] + + - name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT comment FROM system.databases WHERE name = 'test_db'" + + - name: Check the DB is there + ansible.builtin.assert: + that: + - result.result == [['Test DB']] + + # Test + # The change is not possible. It'll just show a warning and ignore + - name: Create database with another comment real mode + register: result + community.clickhouse.clickhouse_db: + state: present + name: test_db + comment: Test DB 1 + + - name: Check ret values + ansible.builtin.assert: + that: + - result is not changed + - result.executed_statements == [] + + - name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT comment FROM system.databases WHERE name = 'test_db'" + + - name: Check the DB is there + ansible.builtin.assert: + that: + - result.result == [['Test DB']] + + # Test + - name: Drop DB with comment + register: result + community.clickhouse.clickhouse_db: + state: absent + name: test_db + comment: Test DB + + - name: Check ret values + ansible.builtin.assert: + that: + - result is changed