Skip to content

Commit

Permalink
Finish
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 committed Feb 22, 2024
1 parent 41a1981 commit 99120f4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
11 changes: 11 additions & 0 deletions plugins/modules/clickhouse_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
87 changes: 87 additions & 0 deletions tests/integration/targets/clickhouse_db/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 99120f4

Please sign in to comment.