---
# Copyright 2016, @WalmartLabs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Update database collations
  hosts: galera_all[0]
  gather_facts: false
  user: root
  tasks:
    - name: Find tables with utf8_unicode_ci collation
      command: >
        mysql -e
        "SELECT T.table_schema, T.table_name FROM information_schema.`TABLES` T,
         information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
         WHERE CCSA.collation_name = T.table_collation AND CCSA.CHARACTER_SET_NAME = 'utf8'
         AND CCSA.COLLATION_NAME = 'utf8_unicode_ci';"
      register: utf8_unicode_ci_tables

    - name: Disable global foreign key checks
      command: >
        mysql -e
        "SET GLOBAL FOREIGN_KEY_CHECKS = 0;"
      when: utf8_unicode_ci_tables.stdout_lines | length > 0

    - name: Convert tables to utf8_general_ci collation
      command: >
       mysql -e
         "ALTER TABLE {{ item.split()[1] }} CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"
         {{ item.split()[0] }}
      with_items: "{{ utf8_unicode_ci_tables.stdout_lines }}"
      when: item | search("^(?!table_schema)\w+\t\w+$")

    - name: Enable global foreign key checks
      command: >
        mysql -e
        "SET GLOBAL FOREIGN_KEY_CHECKS = 1;"
      when: utf8_unicode_ci_tables.stdout_lines | length > 0

    - name: Find databases with utf8_unicode_ci collation
      command: >
        mysql -e
        "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA
         WHERE DEFAULT_CHARACTER_SET_NAME = 'utf8' AND DEFAULT_COLLATION_NAME = 'utf8_unicode_ci';"
      register: utf8_unicode_ci_databases

    - name: Convert databases to utf8_general_ci collation
      command: >
        mysql -e
        "ALTER DATABASE {{ item }} CHARACTER SET utf8 COLLATE utf8_general_ci;"
      with_items: "{{ utf8_unicode_ci_databases.stdout_lines }}"
      when: item | search("^(?!SCHEMA_NAME)\w+$")