From 763cb44b51adde3fc23592066d98ea3040643025 Mon Sep 17 00:00:00 2001 From: Honza Horak Date: Sun, 23 Apr 2017 18:26:04 +0200 Subject: [PATCH] Add logic for upgrading datadir Based on the MYSQL_UPGRADE variable and based on the datadir version detected, we either warn user about incompatibilities, or run one of the specified tools: * mysql_upgrade * mysqlcheck --analyze --all-databases * mysqlcheck --optimize --all-databases This should also fix https://github.com/sclorg/mariadb-container/issues/33 --- 10.1/root/usr/bin/run-mysqld | 4 + 10.1/root/usr/bin/run-mysqld-master | 10 ++- 10.1/root/usr/bin/run-mysqld-slave | 10 ++- .../share/container-scripts/mysql/README.md | 36 ++++++++ .../container-scripts/mysql/check-upgrade.sh | 47 ++++++++++ .../share/container-scripts/mysql/common.sh | 63 +++++++++++++ 10.1/test/run | 90 ++++++++++++++++++- 7 files changed, 255 insertions(+), 5 deletions(-) create mode 100644 10.1/root/usr/share/container-scripts/mysql/check-upgrade.sh diff --git a/10.1/root/usr/bin/run-mysqld b/10.1/root/usr/bin/run-mysqld index cd899a73..f52ab709 100755 --- a/10.1/root/usr/bin/run-mysqld +++ b/10.1/root/usr/bin/run-mysqld @@ -26,6 +26,10 @@ if [ -f ${CONTAINER_SCRIPTS_PATH}/post-init.sh ]; then log_info 'Sourcing post-init.sh ...' source ${CONTAINER_SCRIPTS_PATH}/post-init.sh fi +if [ -f ${CONTAINER_SCRIPTS_PATH}/check-upgrade.sh ]; then + log_info 'Sourcing check-upgrade.sh ...' + source ${CONTAINER_SCRIPTS_PATH}/check-upgrade.sh +fi # Restart the MySQL server with public IP bindings shutdown_local_mysql diff --git a/10.1/root/usr/bin/run-mysqld-master b/10.1/root/usr/bin/run-mysqld-master index 054889e4..73544fce 100755 --- a/10.1/root/usr/bin/run-mysqld-master +++ b/10.1/root/usr/bin/run-mysqld-master @@ -39,8 +39,14 @@ mysql $mysql_flags < "${upgrade_info_file}" + log_info "Storing version '${version}' information into the data dir '${upgrade_info_file}'" +} diff --git a/10.1/test/run b/10.1/test/run index df157156..281c553d 100755 --- a/10.1/test/run +++ b/10.1/test/run @@ -62,7 +62,7 @@ function test_connection() { local ip ip=$(get_container_ip $name) echo " Testing MySQL connection to $ip..." - local max_attempts=20 + local max_attempts=8 local sleep_time=2 local i for i in $(seq $max_attempts); do @@ -412,6 +412,90 @@ run_doc_test() { echo } +function run_upgrade_test() { + local tmpdir=$(mktemp -d) + echo " Testing upgrade of the container image" + mkdir "${tmpdir}/data" && chmod -R a+rwx "${tmpdir}" + + # Create MySQL container with persistent volume and set the version from too old version + create_container "testupg1" -e MYSQL_USER=user -e MYSQL_PASSWORD=foo \ + -e MYSQL_DATABASE=db -v ${tmpdir}:/var/lib/mysql/data:Z + test_connection testupg1 user foo + docker stop $(get_cid testupg1) >/dev/null + + # Create version file that is too old + echo "5.0.12" >${tmpdir}/mysql_upgrade_info + + # Create another container with same data and upgrade set to 'auto' + create_container "testupg2" -e MYSQL_USER=user -e MYSQL_PASSWORD=foo \ + -e MYSQL_DATABASE=db -v ${tmpdir}:/var/lib/mysql/data:Z -e MYSQL_UPGRADE=auto + test_connection testupg2 user foo + docker stop $(get_cid testupg2) >/dev/null + + # Check whether some upgrade happened + if docker logs $(get_cid testupg2) | grep 'Checking and upgrading mysql database' &>/dev/null ; then + echo "Upgrade happened but it should not when upgrading from too old version" + return 1 + fi + + # Create version file that we can upgrade from + echo "10.0.12" >${tmpdir}/mysql_upgrade_info + + # Create another container with same data and upgrade set to 'auto' + create_container "testupg3" -e MYSQL_USER=user -e MYSQL_PASSWORD=foo \ + -e MYSQL_DATABASE=db -v ${tmpdir}:/var/lib/mysql/data:Z -e MYSQL_UPGRADE=auto + test_connection testupg3 user foo + docker stop $(get_cid testupg3) >/dev/null + + # Check whether some upgrade happened + if ! docker logs $(get_cid testupg3) | grep 'Checking and upgrading mysql database' &>/dev/null ; then + echo "Upgrade did not happen but it should when upgrading from previous version" + return 1 + fi + + # Create version file that we don't need to upgrade from + echo "10.1.12" >${tmpdir}/mysql_upgrade_info + + # Create another container with same data and upgrade set to 'auto' + create_container "testupg4" -e MYSQL_USER=user -e MYSQL_PASSWORD=foo \ + -e MYSQL_DATABASE=db -v ${tmpdir}:/var/lib/mysql/data:Z -e MYSQL_UPGRADE=auto + test_connection testupg4 user foo + docker stop $(get_cid testupg4) >/dev/null + + # Check whether some upgrade happened + if docker logs $(get_cid testupg4) | grep 'Checking and upgrading mysql database' &>/dev/null ; then + echo "Upgrade happened but it should not when upgrading from current version" + return 1 + fi + + # Create second container with same data and upgrade set to 'analyze' + create_container "testupg5" -e MYSQL_USER=user -e MYSQL_PASSWORD=foo \ + -e MYSQL_DATABASE=db -v ${tmpdir}:/var/lib/mysql/data:Z -e MYSQL_UPGRADE=analyze + test_connection testupg5 user foo + docker stop $(get_cid testupg5) >/dev/null + + # Check whether analyze happened + if ! docker logs $(get_cid testupg5) | grep -e '--analyze --all-databases' &>/dev/null ; then + echo "Analyze did not happen but it should" + return 1 + fi + + # Create another container with same data and upgrade set to 'optimize' + create_container "testupg6" -e MYSQL_USER=user -e MYSQL_PASSWORD=foo \ + -e MYSQL_DATABASE=db -v ${tmpdir}:/var/lib/mysql/data:Z -e MYSQL_UPGRADE=optimize + test_connection testupg6 user foo + docker stop $(get_cid testupg6) >/dev/null + + # Check whether optimize happened + if ! docker logs $(get_cid testupg6) | grep -e '--optimize --all-databases' &>/dev/null ; then + echo "Optimize did not happen but it should" + return 1 + fi + + echo " Success!" + echo +} + # Tests. run_container_creation_tests @@ -434,4 +518,8 @@ run_change_password_test # Replication tests run_replication_test +# Documentation tests run_doc_test + +# Upgrade, optimaze and analyze tests +run_upgrade_test