|
| 1 | +upstream_upgrade_info() { |
| 2 | + echo -n "For upstream documentation about upgrading, see: " |
| 3 | + case ${MYSQL_VERSION} in |
| 4 | + 10.0) echo "https://mariadb.com/kb/en/library/upgrading-from-mariadb-55-to-mariadb-100/" ;; |
| 5 | + 10.1) echo "https://mariadb.com/kb/en/library/upgrading-from-mariadb-100-to-mariadb-101/" ;; |
| 6 | + 10.2) echo "https://mariadb.com/kb/en/library/upgrading-from-mariadb-101-to-mariadb-102/" ;; |
| 7 | + 5.6) echo "https://dev.mysql.com/doc/refman/5.6/en/upgrading-from-previous-series.html" ;; |
| 8 | + 5.7) echo "https://dev.mysql.com/doc/refman/5.7/en/upgrading-from-previous-series.html" ;; |
| 9 | + *) echo "Non expected version '${MYSQL_VERSION}'" ; return 1 ;; |
| 10 | + esac |
| 11 | +} |
| 12 | + |
| 13 | +check_datadir_version() { |
| 14 | + local datadir="$1" |
| 15 | + local datadir_version=$(get_datadir_version "$datadir") |
| 16 | + local mysqld_version=$(mysqld_compat_version) |
| 17 | + local datadir_version_dot=$(number2version "${datadir_version}") |
| 18 | + local mysqld_version_dot=$(number2version "${mysqld_version}") |
| 19 | + |
| 20 | + for upgrade_action in ${MYSQL_UPGRADE//,/ } ; do |
| 21 | + log_info "Running upgrade action: ${upgrade_action}" |
| 22 | + case ${upgrade_action} in |
| 23 | + auto|warn) |
| 24 | + if [ -z "${datadir_version}" ] || [ "${datadir_version}" -eq 0 ] ; then |
| 25 | + # Writing the info file, since historically it was not written |
| 26 | + log_warn "Version of the data could not be determined."\ |
| 27 | + "It is because the file mysql_upgrade_info is missing in the data directory, which"\ |
| 28 | + "is most probably because it was not created when initialization of data directory."\ |
| 29 | + "In order to allow seamless updates to the next higher version in the future,"\ |
| 30 | + "the file mysql_upgrade_info will be created."\ |
| 31 | + "If the data directory was created with a different version than ${mysqld_version_dot},"\ |
| 32 | + "it is required to run this container with the MYSQL_UPGRADE environment variable"\ |
| 33 | + "set to 'force', or run 'mysql_upgrade' utility manually; the mysql_upgrade tool"\ |
| 34 | + "checks the tables and creates such a file as well. $(upstream_upgrade_info)" |
| 35 | + write_mysql_upgrade_info_file "${MYSQL_DATADIR}" |
| 36 | + continue |
| 37 | + # This is currently a dead-code, but should be enabled after the mysql_upgrade_info |
| 38 | + # file gets to the deployments (after few monts most of the deployments should already have the file) |
| 39 | + log_warn "Version of the data could not be determined."\ |
| 40 | + "Running such a container is risky."\ |
| 41 | + "The current daemon version is ${mysqld_version_dot}."\ |
| 42 | + "If you are not sure whether the data directory is compatible with the current"\ |
| 43 | + "version ${mysqld_version_dot}, restore the data from a back-up."\ |
| 44 | + "If restoring from a back-up is not possible, create a file 'mysql_upgrade_info'"\ |
| 45 | + "that includes version information (${mysqld_version_dot} in this case) in the root"\ |
| 46 | + "of the data directory."\ |
| 47 | + "In order to create the 'mysql_upgrade_info' file, either run this container with"\ |
| 48 | + "the MYSQL_UPGRADE environment variable set to 'force', or run 'mysql_upgrade' utility"\ |
| 49 | + "manually; the mysql_upgrade tool checks the tables and creates such a file as well."\ |
| 50 | + "That will enable correct upgrade check in the future. $(upstream_upgrade_info)" |
| 51 | + fi |
| 52 | + |
| 53 | + if [ "${datadir_version}" -eq "${mysqld_version}" ] ; then |
| 54 | + log_info "MySQL server version check passed, both server and data directory"\ |
| 55 | + "are version ${mysqld_version_dot}." |
| 56 | + continue |
| 57 | + fi |
| 58 | + |
| 59 | + if [ $(( ${datadir_version} + 1 )) -eq "${mysqld_version}" -o "${datadir_version}" -eq 505 -a "${mysqld_version}" -eq 1000 ] ; then |
| 60 | + log_warn "MySQL server is version ${mysqld_version_dot} and datadir is version"\ |
| 61 | + "${datadir_version_dot}, which is a compatible combination." |
| 62 | + if [ "${MYSQL_UPGRADE}" == 'auto' ] ; then |
| 63 | + log_info "The data directory will be upgraded automatically from ${datadir_version_dot}"\ |
| 64 | + "to version ${mysqld_version_dot}. $(upstream_upgrade_info)" |
| 65 | + log_and_run mysql_upgrade --socket=/tmp/mysql.sock |
| 66 | + else |
| 67 | + log_warn "Automatic upgrade is not turned on, proceed with the upgrade."\ |
| 68 | + "In order to upgrade the data directory, run this container with the MYSQL_UPGRADE"\ |
| 69 | + "environment variable set to 'auto' or run running mysql_upgrade manually. $(upstream_upgrade_info)" |
| 70 | + fi |
| 71 | + else |
| 72 | + log_warn "MySQL server is version ${mysqld_version_dot} and datadir is version"\ |
| 73 | + "${datadir_version_dot}, which are incompatible. Remember, that upgrade is only supported"\ |
| 74 | + "by upstream from previous version and it is not allowed to skip versions. $(upstream_upgrade_info)" |
| 75 | + if [ "${datadir_version}" -gt "${mysqld_version}" ] ; then |
| 76 | + log_warn "Downgrading to the lower version is not supported. Consider"\ |
| 77 | + "dumping data and load them again into a fresh instance. $(upstream_upgrade_info)" |
| 78 | + fi |
| 79 | + log_warn "Consider restoring the database from a back-up. To ignore this"\ |
| 80 | + "warning, set 'MYSQL_UPGRADE' variable to 'force', but this may result in data corruption. $(upstream_upgrade_info)" |
| 81 | + return 1 |
| 82 | + fi |
| 83 | + ;; |
| 84 | + |
| 85 | + force) |
| 86 | + log_and_run mysql_upgrade --socket=/tmp/mysql.sock --force |
| 87 | + ;; |
| 88 | + |
| 89 | + optimize) |
| 90 | + log_and_run mysqlcheck --socket=/tmp/mysql.sock --optimize --all-databases --force |
| 91 | + ;; |
| 92 | + |
| 93 | + analyze) |
| 94 | + log_and_run mysqlcheck --socket=/tmp/mysql.sock --analyze --all-databases --force |
| 95 | + ;; |
| 96 | + |
| 97 | + disable) |
| 98 | + log_info "Nothing is done about the data directory version." |
| 99 | + ;; |
| 100 | + *) |
| 101 | + log_warn "Unknown value of MYSQL_UPGRADE variable: '${MYSQL_UPGRADE}', ignoring." |
| 102 | + ;; |
| 103 | + esac |
| 104 | + done |
| 105 | +} |
| 106 | + |
| 107 | +check_datadir_version "${MYSQL_DATADIR}" |
| 108 | + |
| 109 | +unset -f check_datadir_version upstream_upgrade_info |
| 110 | + |
| 111 | + |
0 commit comments