Correct MySQL charset and collation encoding if they're wrong

Affects issues:
- Close #2293
This commit is contained in:
Aurora Lahtela 2023-04-06 18:58:42 +03:00
parent 9dd6fab80b
commit 720cf4374e
3 changed files with 105 additions and 5 deletions

View File

@ -242,6 +242,7 @@ public abstract class SQLDB extends AbstractDatabase {
new ComponentColumnToExtensionDataPatch(), new ComponentColumnToExtensionDataPatch(),
new BadJoinAddressDataCorrectionPatch(), new BadJoinAddressDataCorrectionPatch(),
new AfterBadJoinAddressDataCorrectionPatch(), new AfterBadJoinAddressDataCorrectionPatch(),
new CorrectWrongCharacterEncodingPatch(logger, config),
}; };
} }

View File

@ -0,0 +1,104 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.storage.database.transactions.patches;
import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.DatabaseSettings;
import com.djrapitops.plan.storage.database.DBType;
import com.djrapitops.plan.storage.database.queries.Query;
import net.playeranalytics.plugin.server.PluginLogger;
import org.intellij.lang.annotations.Language;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
/**
* Corrects database charset and collation.
*
* @author AuroraLS3
*/
@Singleton
public class CorrectWrongCharacterEncodingPatch extends Patch {
private final PluginLogger logger;
private final PlanConfig config;
private List<String> correctionSqlQueries;
@Inject
public CorrectWrongCharacterEncodingPatch(PluginLogger logger, PlanConfig config) {
this.logger = logger;
this.config = config;
}
@Override
public boolean hasBeenApplied() {
if (dbType != DBType.MYSQL) return true;
correctionSqlQueries = query(getBadTableCorrectionQueries());
return correctionSqlQueries.isEmpty();
}
public Query<List<String>> getBadTableCorrectionQueries() {
String databaseName = config.get(DatabaseSettings.MYSQL_DATABASE);
@Language("MySQL")
String selectTablesWithWrongCharset = "SELECT CONCAT('ALTER TABLE `', table_name, '` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;')\n" +
"FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C\n" +
"WHERE C.collation_name = T.table_collation\n" +
"AND T.table_schema = '" + databaseName + "'\n" +
"AND\n" +
"(\n" +
" C.CHARACTER_SET_NAME != 'utf8mb4'\n" +
" OR\n" +
" C.COLLATION_NAME != 'utf8mb4_general_ci'\n" +
")";
@Language("MySQL")
String selectColumnsWithWrongCharset = "SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH, ') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci', (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')\n" +
"FROM information_schema.COLUMNS \n" +
"WHERE TABLE_SCHEMA = '" + databaseName + "'\n" +
"AND DATA_TYPE = 'varchar'\n" +
"AND\n" +
"(\n" +
" CHARACTER_SET_NAME != 'utf8mb4'\n" +
" OR\n" +
" COLLATION_NAME != 'utf8mb4_general_ci'\n" +
")";
@Language("MySQL")
String sql = selectTablesWithWrongCharset + " UNION " + selectColumnsWithWrongCharset;
return db -> db.queryList(sql, resultSet -> resultSet.getString(0));
}
@Override
protected void applyPatch() {
try {
for (String correctionSqlQuery : correctionSqlQueries) {
execute(correctionSqlQuery);
}
@Language("MySQL")
String sql = "ALTER DATABASE " + config.get(DatabaseSettings.MYSQL_DATABASE) + " CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";
execute(sql);
} catch (DBOpException e) {
logger.warn("CorrectWrongCharacterEncodingPatch could not be applied: " + e.getMessage());
logger.warn("Apply it manually or correct access privileges for MySQL user.");
logger.warn("Instructions for manual application https://github.com/plan-player-analytics/Plan/issues/2293");
} finally {
correctionSqlQueries.clear();
}
}
}

View File

@ -1151,11 +1151,6 @@
minimatch "^3.0.4" minimatch "^3.0.4"
strip-json-comments "^3.1.1" strip-json-comments "^3.1.1"
"@fortawesome/fontawesome-common-types@6.3.0":
version "6.3.0"
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-6.3.0.tgz#51f734e64511dbc3674cd347044d02f4dd26e86b"
integrity sha512-4BC1NMoacEBzSXRwKjZ/X/gmnbp/HU5Qqat7E8xqorUtBFZS+bwfGH5/wqOC2K6GV0rgEobp3OjGRMa5fK9pFg==
"@fortawesome/fontawesome-common-types@6.4.0": "@fortawesome/fontawesome-common-types@6.4.0":
version "6.4.0" version "6.4.0"
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-6.4.0.tgz#88da2b70d6ca18aaa6ed3687832e11f39e80624b" resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-6.4.0.tgz#88da2b70d6ca18aaa6ed3687832e11f39e80624b"