openstack-ansible/scripts/upgrade-utilities/playbooks/db-collation-alter.yml
Jimmy McCrory 63012f0c40 Add upgrade playbook to update database collations
Since the default database collation has changed, include an upgrade
playbook to ensure that existing tables and databases with the previous
collation are converted during upgrades from Mitaka.

Change-Id: Iadbcf50c9611561b56fa1ea6ef3e80f636e0c0a8
Depends-on: I8507b6c9bd058bb308cc089f3802e52e24bea324
2016-07-13 09:02:16 +00:00

58 lines
2.3 KiB
YAML

---
# 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 foreign key checks
command: >
mysql -e
"SET 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 foreign key checks
command: >
mysql -e
"SET 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+$")