From 9cf17827d25ea8c65b7096975325668c2bcc5be6 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Wed, 13 Nov 2024 17:38:04 +0100 Subject: [PATCH 01/14] [WIP] Add tables count per database --- plugins/modules/mysql_info.py | 32 +++++--- .../tasks/filter_databases_tables.yml | 75 +++++++++++++++++++ .../targets/test_mysql_info/tasks/main.yml | 4 + 3 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index 3a30597c..bed3f0c4 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -35,7 +35,7 @@ exclude_fields: description: - List of fields which are not needed to collect. - - "Supports elements: C(db_size). Unsupported elements will be ignored." + - "Supports elements: C(db_size), C(db_table_count). Unsupported elements will be ignored." type: list elements: str version_added: '0.1.0' @@ -656,15 +656,20 @@ def __get_users_info(self): def __get_databases(self, exclude_fields, return_empty_dbs): """Get info about databases.""" - if not exclude_fields: - query = ('SELECT table_schema AS "name", ' - 'SUM(data_length + index_length) AS "size" ' - 'FROM information_schema.TABLES GROUP BY table_schema') - else: - if 'db_size' in exclude_fields: - query = ('SELECT table_schema AS "name" ' - 'FROM information_schema.TABLES GROUP BY table_schema') + cmd_start = 'SELECT table_schema AS "name"' + cmd_db_size = 'SUM(data_length + index_length) AS "size"' + cmd_db_table_count = 'COUNT(table_name) as "tables"' + cmd_end = ' FROM information_schema.TABLES GROUP BY table_schema' + cmd_start_and_fields = [cmd_start, cmd_db_size, cmd_db_table_count] + + if exclude_fields and 'db_size' in exclude_fields: + cmd_start_and_fields.remove(cmd_db_size) + + if exclude_fields and 'db_table_count' in exclude_fields: + cmd_start_and_fields.remove(cmd_db_table_count) + + query = ', '.join(cmd_start_and_fields) + cmd_end res = self.__exec_sql(query) if res: @@ -677,6 +682,12 @@ def __get_databases(self, exclude_fields, return_empty_dbs): self.info['databases'][db['name']]['size'] = int(db['size']) + if not exclude_fields or 'db_table_count' not in exclude_fields: + if db['tables'] is None: + db['tables'] = 0 + + self.info['databases'][db['name']]['tables'] = int(db['tables']) + # If empty dbs are not needed in the returned dict, exit from the method if not return_empty_dbs: return None @@ -691,6 +702,9 @@ def __get_databases(self, exclude_fields, return_empty_dbs): if not exclude_fields or 'db_size' not in exclude_fields: self.info['databases'][db['Database']]['size'] = 0 + if not exclude_fields or 'db_table_count' not in exclude_fields: + self.info['databases'][db['Database']]['tables'] = 0 + def __exec_sql(self, query, ddl=False): """Execute SQL. diff --git a/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml b/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml new file mode 100644 index 00000000..5aa87f57 --- /dev/null +++ b/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml @@ -0,0 +1,75 @@ +--- + +- module_defaults: + community.mysql.mysql_db: &mysql_defaults + login_user: "{{ mysql_user }}" + login_password: "{{ mysql_password }}" + login_host: "{{ mysql_host }}" + login_port: "{{ mysql_primary_port }}" + community.mysql.mysql_query: *mysql_defaults + community.mysql.mysql_info: *mysql_defaults + community.mysql.mysql_user: *mysql_defaults + + block: + + # ================================ Prepare ============================== + - name: Mysql_info users_info | Create databases + community.mysql.mysql_db: + name: + - db_tables_count_empty + - db_tables_count_1 + - db_tables_count_2 + state: present + + - name: Mysql_info users_info | Create tables + community.mysql.mysql_query: + query: + - >- + CREATE TABLE IF NOT EXISTS db_tables_count_1.t1 + (id int, name varchar(9)) + - >- + CREATE TABLE IF NOT EXISTS db_tables_count_2.t1 + (id int, name1 varchar(9)) + - >- + CREATE TABLE IF NOT EXISTS db_tables_count_2.t2 + (id int, name1 varchar(9)) + + # ================================== Tests ============================== + + - name: Mysql_info users_info | Collect all databases fields + community.mysql.mysql_info: + filter: + - databases + register: result + # failed_when: + # - TODO + + - name: Mysql_info users_info | Collect all databases fields except db_size + community.mysql.mysql_info: + filter: + - databases + exclude_fileds: + - db_size + register: result + # failed_when: + # - TODO + + - name: Mysql_info users_info | Collect all databases fields except db_table_count + community.mysql.mysql_info: + filter: + - databases + exclude_fileds: + - db_table_count + register: result + # failed_when: + # - TODO + + # ================================== Cleanup ============================ + + - name: Mysql_info users_info | Cleanup databases + community.mysql.mysql_db: + name: + - db_tables_count_empty + - db_tables_count_1 + - db_tables_count_2 + state: present diff --git a/tests/integration/targets/test_mysql_info/tasks/main.yml b/tests/integration/targets/test_mysql_info/tasks/main.yml index 42350c6e..bfe7708a 100644 --- a/tests/integration/targets/test_mysql_info/tasks/main.yml +++ b/tests/integration/targets/test_mysql_info/tasks/main.yml @@ -221,6 +221,10 @@ that: - result.databases.allviews.size == 0 + - name: Import tasks file to tests tables count in database filter + ansible.builtin.import_tasks: + file: filter_databases_tables.yml + - name: Import tasks file to tests users_info filter ansible.builtin.import_tasks: file: filter_users_info.yml From 32e6e9e8c1e364560e3c309f82482b3aa2f08012 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Fri, 15 Nov 2024 16:55:47 +0100 Subject: [PATCH 02/14] Add integrations tests --- .../tasks/filter_databases_tables.yml | 49 +++++++++++++++---- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml b/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml index 5aa87f57..6b88deed 100644 --- a/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml +++ b/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml @@ -36,33 +36,62 @@ # ================================== Tests ============================== - - name: Mysql_info users_info | Collect all databases fields + - name: Mysql_info users_info | Collect all non-empty databases fields community.mysql.mysql_info: filter: - databases register: result - # failed_when: - # - TODO + failed_when: + - > + result.databases['db_tables_count_1'].size != 16384 or + result.databases['db_tables_count_1'].tables != 1 or + result.databases['db_tables_count_2'].size != 32768 or + result.databases['db_tables_count_2'].tables != 2 or + 'db_tables_count_empty' in result.databases | dict2items + | map(attribute='key') - name: Mysql_info users_info | Collect all databases fields except db_size community.mysql.mysql_info: filter: - databases - exclude_fileds: + exclude_fields: - db_size register: result - # failed_when: - # - TODO + failed_when: + - > + result.databases['db_tables_count_1'].size is defined or + result.databases['db_tables_count_2'].size is defined or + 'db_tables_count_empty' in result.databases | dict2items + | map(attribute='key') - - name: Mysql_info users_info | Collect all databases fields except db_table_count + - name: Mysql_info users_info | Collect all databases fields except tables community.mysql.mysql_info: filter: - databases - exclude_fileds: + exclude_fields: - db_table_count register: result - # failed_when: - # - TODO + failed_when: + - > + result.databases['db_tables_count_1'].tables is defined or + result.databases['db_tables_count_2'].tables is defined or + 'db_tables_count_empty' in result.databases | dict2items + | map(attribute='key') + + - name: Mysql_info users_info | Collect all databases even empty ones + community.mysql.mysql_info: + filter: + - databases + return_empty_dbs: true + register: result + failed_when: + - > + result.databases['db_tables_count_1'].size != 16384 or + result.databases['db_tables_count_1'].tables != 1 or + result.databases['db_tables_count_2'].size != 32768 or + result.databases['db_tables_count_2'].tables != 2 or + result.databases['db_tables_count_empty'].size != 0 or + result.databases['db_tables_count_empty'].tables != 0 # ================================== Cleanup ============================ From d76ac2f290f86fb5500c743f8d758390dbc43ac4 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Fri, 15 Nov 2024 17:07:35 +0100 Subject: [PATCH 03/14] Refactor for readability and extensibility --- plugins/modules/mysql_info.py | 81 ++++++++++++++--------------------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index bed3f0c4..978b6bc8 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -656,54 +656,39 @@ def __get_users_info(self): def __get_databases(self, exclude_fields, return_empty_dbs): """Get info about databases.""" - - cmd_start = 'SELECT table_schema AS "name"' - cmd_db_size = 'SUM(data_length + index_length) AS "size"' - cmd_db_table_count = 'COUNT(table_name) as "tables"' - cmd_end = ' FROM information_schema.TABLES GROUP BY table_schema' - cmd_start_and_fields = [cmd_start, cmd_db_size, cmd_db_table_count] - - if exclude_fields and 'db_size' in exclude_fields: - cmd_start_and_fields.remove(cmd_db_size) - - if exclude_fields and 'db_table_count' in exclude_fields: - cmd_start_and_fields.remove(cmd_db_table_count) - - query = ', '.join(cmd_start_and_fields) + cmd_end - res = self.__exec_sql(query) - - if res: - for db in res: - self.info['databases'][db['name']] = {} - - if not exclude_fields or 'db_size' not in exclude_fields: - if db['size'] is None: - db['size'] = 0 - - self.info['databases'][db['name']]['size'] = int(db['size']) - - if not exclude_fields or 'db_table_count' not in exclude_fields: - if db['tables'] is None: - db['tables'] = 0 - - self.info['databases'][db['name']]['tables'] = int(db['tables']) - - # If empty dbs are not needed in the returned dict, exit from the method - if not return_empty_dbs: - return None - - # Add info about empty databases (issue #65727): - res = self.__exec_sql('SHOW DATABASES') - if res: - for db in res: - if db['Database'] not in self.info['databases']: - self.info['databases'][db['Database']] = {} - - if not exclude_fields or 'db_size' not in exclude_fields: - self.info['databases'][db['Database']]['size'] = 0 - - if not exclude_fields or 'db_table_count' not in exclude_fields: - self.info['databases'][db['Database']]['tables'] = 0 + + def is_field_included(field_name): + return not exclude_fields or f'db_{field_name}' not in exclude_fields + + def create_db_info(db_data): + info = {} + if is_field_included('size'): + info['size'] = int(db_data.get('size', 0) or 0) + if is_field_included('table_count'): + info['tables'] = int(db_data.get('tables', 0) or 0) + return info + + # Build the main query + query_parts = ['SELECT table_schema AS "name"'] + if is_field_included('size'): + query_parts.append('SUM(data_length + index_length) AS "size"') + if is_field_included('table_count'): + query_parts.append('COUNT(table_name) as "tables"') + + query = f"{', '.join(query_parts)} FROM information_schema.TABLES GROUP BY table_schema" + + # Get and process databases with tables + databases = self.__exec_sql(query) or [] + for db in databases: + self.info['databases'][db['name']] = create_db_info(db) + + # Handle empty databases if requested + if return_empty_dbs: + empty_databases = self.__exec_sql('SHOW DATABASES') or [] + for db in empty_databases: + db_name = db['Database'] + if db_name not in self.info['databases']: + self.info['databases'][db_name] = create_db_info({}) def __exec_sql(self, query, ddl=False): """Execute SQL. From a066a1749d3be6ced64831271e560e853e5a0ee4 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 09:21:01 +0100 Subject: [PATCH 04/14] Rename test file with more broad term --- .../tasks/{filter_databases_tables.yml => filter_databases.yml} | 0 tests/integration/targets/test_mysql_info/tasks/main.yml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/integration/targets/test_mysql_info/tasks/{filter_databases_tables.yml => filter_databases.yml} (100%) diff --git a/tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml b/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml similarity index 100% rename from tests/integration/targets/test_mysql_info/tasks/filter_databases_tables.yml rename to tests/integration/targets/test_mysql_info/tasks/filter_databases.yml diff --git a/tests/integration/targets/test_mysql_info/tasks/main.yml b/tests/integration/targets/test_mysql_info/tasks/main.yml index bfe7708a..4246aeb0 100644 --- a/tests/integration/targets/test_mysql_info/tasks/main.yml +++ b/tests/integration/targets/test_mysql_info/tasks/main.yml @@ -223,7 +223,7 @@ - name: Import tasks file to tests tables count in database filter ansible.builtin.import_tasks: - file: filter_databases_tables.yml + file: filter_databases.yml - name: Import tasks file to tests users_info filter ansible.builtin.import_tasks: From 0b8a416af02110f942773a7241f9f25f53be6890 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 09:41:58 +0100 Subject: [PATCH 05/14] Deduplicate tests between main and new task file --- .../tasks/filter_databases.yml | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml b/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml index 6b88deed..3064eaac 100644 --- a/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml +++ b/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml @@ -13,15 +13,16 @@ block: # ================================ Prepare ============================== - - name: Mysql_info users_info | Create databases + - name: Mysql_info databases | Prepare | Create databases community.mysql.mysql_db: name: - db_tables_count_empty - db_tables_count_1 - db_tables_count_2 + - db_only_views # https://github.com/ansible-Getions/community.mysql/issues/204 state: present - - name: Mysql_info users_info | Create tables + - name: Mysql_info databases | Prepare | Create tables community.mysql.mysql_query: query: - >- @@ -33,10 +34,12 @@ - >- CREATE TABLE IF NOT EXISTS db_tables_count_2.t2 (id int, name1 varchar(9)) + - >- + CREATE VIEW db_only_views.v_today (today) AS SELECT CURRENT_DATE # ================================== Tests ============================== - - name: Mysql_info users_info | Collect all non-empty databases fields + - name: Mysql_info databases | Get all non-empty databases fields community.mysql.mysql_info: filter: - databases @@ -47,10 +50,11 @@ result.databases['db_tables_count_1'].tables != 1 or result.databases['db_tables_count_2'].size != 32768 or result.databases['db_tables_count_2'].tables != 2 or + result.databases['db_only_views'].size != 0 or 'db_tables_count_empty' in result.databases | dict2items | map(attribute='key') - - name: Mysql_info users_info | Collect all databases fields except db_size + - name: Mysql_info databases | Get all dbs fields except db_size community.mysql.mysql_info: filter: - databases @@ -64,7 +68,25 @@ 'db_tables_count_empty' in result.databases | dict2items | map(attribute='key') - - name: Mysql_info users_info | Collect all databases fields except tables + # 'unsupported' element is passed to check that an unsupported value + # won't break anything (will be ignored regarding to the module's + # documentation). + - name: Mysql_info databases | Get all dbs fields with unsupported value + community.mysql.mysql_info: + filter: + - databases + exclude_fields: + - db_size + - unsupported + register: result + failed_when: + - > + result.databases['db_tables_count_1'].size is defined or + result.databases['db_tables_count_2'].size is defined or + 'db_tables_count_empty' in result.databases | dict2items + | map(attribute='key') + + - name: Mysql_info databases | Get all dbs fields except tables community.mysql.mysql_info: filter: - databases @@ -78,7 +100,7 @@ 'db_tables_count_empty' in result.databases | dict2items | map(attribute='key') - - name: Mysql_info users_info | Collect all databases even empty ones + - name: Mysql_info databases | Get all dbs even empty ones community.mysql.mysql_info: filter: - databases @@ -93,12 +115,29 @@ result.databases['db_tables_count_empty'].size != 0 or result.databases['db_tables_count_empty'].tables != 0 + - name: Mysql_info databases | Get all dbs even empty ones without size + community.mysql.mysql_info: + filter: + - databases + exclude_fields: + - db_size + return_empty_dbs: true + register: result + failed_when: + - > + result.databases['db_tables_count_1'].size is defined or + result.databases['db_tables_count_1'].tables != 1 or + result.databases['db_tables_count_2'].size is defined or + result.databases['db_tables_count_2'].tables != 2 or + result.databases['db_tables_count_empty'].size is defined or + result.databases['db_tables_count_empty'].tables != 0 + # ================================== Cleanup ============================ - - name: Mysql_info users_info | Cleanup databases + - name: Mysql_info databases | Cleanup databases community.mysql.mysql_db: name: - db_tables_count_empty - db_tables_count_1 - db_tables_count_2 - state: present + - db_only_views From 84f7fc813584efc41038e30813701c130d4c738a Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 09:42:06 +0100 Subject: [PATCH 06/14] fix cleanup --- .../targets/test_mysql_info/tasks/filter_databases.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml b/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml index 3064eaac..be0091ad 100644 --- a/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml +++ b/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml @@ -141,3 +141,4 @@ - db_tables_count_1 - db_tables_count_2 - db_only_views + state: absent From 8cf6c2b749beb6bbb634296f229110c3c666f594 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 09:58:11 +0100 Subject: [PATCH 07/14] Add tests for all values --- .../test_mysql_info/tasks/filter_databases.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml b/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml index be0091ad..da1058b3 100644 --- a/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml +++ b/tests/integration/targets/test_mysql_info/tasks/filter_databases.yml @@ -51,6 +51,7 @@ result.databases['db_tables_count_2'].size != 32768 or result.databases['db_tables_count_2'].tables != 2 or result.databases['db_only_views'].size != 0 or + result.databases['db_only_views'].tables != 1 or 'db_tables_count_empty' in result.databases | dict2items | map(attribute='key') @@ -64,7 +65,11 @@ failed_when: - > result.databases['db_tables_count_1'].size is defined or + result.databases['db_tables_count_1'].tables != 1 or result.databases['db_tables_count_2'].size is defined or + result.databases['db_tables_count_2'].tables != 2 or + result.databases['db_only_views'].size is defined or + result.databases['db_only_views'].tables != 1 or 'db_tables_count_empty' in result.databases | dict2items | map(attribute='key') @@ -82,7 +87,11 @@ failed_when: - > result.databases['db_tables_count_1'].size is defined or + result.databases['db_tables_count_1'].tables != 1 or result.databases['db_tables_count_2'].size is defined or + result.databases['db_tables_count_2'].tables != 2 or + result.databases['db_only_views'].size is defined or + result.databases['db_only_views'].tables != 1 or 'db_tables_count_empty' in result.databases | dict2items | map(attribute='key') @@ -95,8 +104,12 @@ register: result failed_when: - > + result.databases['db_tables_count_1'].size != 16384 or result.databases['db_tables_count_1'].tables is defined or + result.databases['db_tables_count_2'].size != 32768 or result.databases['db_tables_count_2'].tables is defined or + result.databases['db_only_views'].size != 0 or + result.databases['db_only_views'].tables is defined or 'db_tables_count_empty' in result.databases | dict2items | map(attribute='key') @@ -112,6 +125,8 @@ result.databases['db_tables_count_1'].tables != 1 or result.databases['db_tables_count_2'].size != 32768 or result.databases['db_tables_count_2'].tables != 2 or + result.databases['db_only_views'].size != 0 or + result.databases['db_only_views'].tables != 1 or result.databases['db_tables_count_empty'].size != 0 or result.databases['db_tables_count_empty'].tables != 0 @@ -129,6 +144,8 @@ result.databases['db_tables_count_1'].tables != 1 or result.databases['db_tables_count_2'].size is defined or result.databases['db_tables_count_2'].tables != 2 or + result.databases['db_only_views'].size is defined or + result.databases['db_only_views'].tables != 1 or result.databases['db_tables_count_empty'].size is defined or result.databases['db_tables_count_empty'].tables != 0 From 7496167be47901df55b9873dbc42372f4eeb971a Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 10:00:29 +0100 Subject: [PATCH 08/14] Document module returned values --- plugins/modules/mysql_info.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index 978b6bc8..70c2adeb 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -204,13 +204,19 @@ returned: if not excluded by filter type: dict sample: - - { "mysql": { "size": 656594 }, "information_schema": { "size": 73728 } } + - { "mysql": { "size": 656594, "tables": 31 }, "information_schema": { "size": 73728, "tables": 79 } } contains: size: description: Database size in bytes. returned: if not excluded by filter type: dict sample: { 'size': 656594 } + tables: + description: Count of tables and views in that database. + returned: if not excluded by filter + type: dict + sample: { 'tables': 12 } + version_added: '3.11.0' settings: description: Global settings (variables) information. returned: if not excluded by filter From 1d56f8097617962890992e2d27e3df355e182892 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 10:01:08 +0100 Subject: [PATCH 09/14] Cut tests moved to the task file --- .../targets/test_mysql_info/tasks/main.yml | 87 ------------------- 1 file changed, 87 deletions(-) diff --git a/tests/integration/targets/test_mysql_info/tasks/main.yml b/tests/integration/targets/test_mysql_info/tasks/main.yml index 4246aeb0..61f238fe 100644 --- a/tests/integration/targets/test_mysql_info/tasks/main.yml +++ b/tests/integration/targets/test_mysql_info/tasks/main.yml @@ -132,95 +132,8 @@ - result.global_status is not defined - result.users is not defined - # Test exclude_fields: db_size - # 'unsupported' element is passed to check that an unsupported value - # won't break anything (will be ignored regarding to the module's documentation). - - name: Collect info about databases excluding their sizes - mysql_info: - <<: *mysql_params - filter: - - databases - exclude_fields: - - db_size - - unsupported - register: result - - - assert: - that: - - result is not changed - - result.databases != {} - - result.databases.mysql == {} - - ######################################################## - # Issue #65727, empty databases must be in returned dict - # - - name: Create empty database acme - mysql_db: - <<: *mysql_params - name: acme - - - name: Collect info about databases - mysql_info: - <<: *mysql_params - filter: - - databases - return_empty_dbs: true - register: result - - # Check acme is in returned dict - - assert: - that: - - result is not changed - - result.databases.acme.size == 0 - - result.databases.mysql != {} - - - name: Collect info about databases excluding their sizes - mysql_info: - <<: *mysql_params - filter: - - databases - exclude_fields: - - db_size - return_empty_dbs: true - register: result - - # Check acme is in returned dict - - assert: - that: - - result is not changed - - result.databases.acme == {} - - result.databases.mysql == {} - - - name: Remove acme database - mysql_db: - <<: *mysql_params - name: acme - state: absent - - include_tasks: issue-28.yml - # https://github.com/ansible-collections/community.mysql/issues/204 - - name: Create database containing only views - mysql_db: - <<: *mysql_params - name: allviews - - - name: Create view - mysql_query: - <<: *mysql_params - login_db: allviews - query: 'CREATE VIEW v_today (today) AS SELECT CURRENT_DATE' - - - name: Fetch info - mysql_info: - <<: *mysql_params - register: result - - - name: Check - assert: - that: - - result.databases.allviews.size == 0 - - name: Import tasks file to tests tables count in database filter ansible.builtin.import_tasks: file: filter_databases.yml From e40ff32d3d493e35821d7977e6bed6b7f9230499 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 11:48:20 +0100 Subject: [PATCH 10/14] sanity --- plugins/modules/mysql_info.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index 70c2adeb..c6f88f78 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -662,10 +662,10 @@ def __get_users_info(self): def __get_databases(self, exclude_fields, return_empty_dbs): """Get info about databases.""" - + def is_field_included(field_name): return not exclude_fields or f'db_{field_name}' not in exclude_fields - + def create_db_info(db_data): info = {} if is_field_included('size'): @@ -680,14 +680,14 @@ def create_db_info(db_data): query_parts.append('SUM(data_length + index_length) AS "size"') if is_field_included('table_count'): query_parts.append('COUNT(table_name) as "tables"') - + query = f"{', '.join(query_parts)} FROM information_schema.TABLES GROUP BY table_schema" - + # Get and process databases with tables databases = self.__exec_sql(query) or [] for db in databases: self.info['databases'][db['name']] = create_db_info(db) - + # Handle empty databases if requested if return_empty_dbs: empty_databases = self.__exec_sql('SHOW DATABASES') or [] From 0f4ec7acea03dd30375dc6d85a7377114adc14b5 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 12:06:10 +0100 Subject: [PATCH 11/14] Cut fstring for Python < 3.6 --- plugins/modules/mysql_info.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index c6f88f78..8c3845d3 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -664,7 +664,7 @@ def __get_databases(self, exclude_fields, return_empty_dbs): """Get info about databases.""" def is_field_included(field_name): - return not exclude_fields or f'db_{field_name}' not in exclude_fields + return not exclude_fields or 'db_{}'.format(field_name) not in exclude_fields def create_db_info(db_data): info = {} @@ -681,7 +681,7 @@ def create_db_info(db_data): if is_field_included('table_count'): query_parts.append('COUNT(table_name) as "tables"') - query = f"{', '.join(query_parts)} FROM information_schema.TABLES GROUP BY table_schema" + query = "{} FROM information_schema.TABLES GROUP BY table_schema".format(", ".join(query_parts)) # Get and process databases with tables databases = self.__exec_sql(query) or [] From 740a00ed9421d452eac9eedffb64b166c403f237 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 13:50:27 +0100 Subject: [PATCH 12/14] Add changelog fragment --- changelogs/fragments/591-mysql_info-db_tables_count.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/591-mysql_info-db_tables_count.yml diff --git a/changelogs/fragments/591-mysql_info-db_tables_count.yml b/changelogs/fragments/591-mysql_info-db_tables_count.yml new file mode 100644 index 00000000..abbc1cbc --- /dev/null +++ b/changelogs/fragments/591-mysql_info-db_tables_count.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - mysql_info - adds the count of tables for each database to the returned values. It is possible to exclude this new field using the ``db_table_count`` exclusion filter. (https://github.com/ansible-collections/community.mysql/pull/691) From bd24b78675d47ea5aacbfd82c47fec9ddf258947 Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 15:49:35 +0100 Subject: [PATCH 13/14] fix next release number --- plugins/modules/mysql_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index 8c3845d3..443dcd7d 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -216,7 +216,7 @@ returned: if not excluded by filter type: dict sample: { 'tables': 12 } - version_added: '3.11.0' + version_added: '3.10.4' settings: description: Global settings (variables) information. returned: if not excluded by filter From d42c0ce53bd9fd8a04fcfd120bda7c098da4b45f Mon Sep 17 00:00:00 2001 From: Laurent Indermuehle Date: Mon, 18 Nov 2024 19:01:29 +0100 Subject: [PATCH 14/14] fix version-added-must-be-major-or minor error --- plugins/modules/mysql_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/mysql_info.py b/plugins/modules/mysql_info.py index 443dcd7d..8c3845d3 100644 --- a/plugins/modules/mysql_info.py +++ b/plugins/modules/mysql_info.py @@ -216,7 +216,7 @@ returned: if not excluded by filter type: dict sample: { 'tables': 12 } - version_added: '3.10.4' + version_added: '3.11.0' settings: description: Global settings (variables) information. returned: if not excluded by filter