From b2fbd68681082e3ccbfc6a24064f8ea55537a270 Mon Sep 17 00:00:00 2001 From: Robert Sehr Date: Wed, 8 May 2019 10:01:07 +0200 Subject: [PATCH] convert database tables to utf8mb4 --- .../persistence/managers/DatabaseVersion.java | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/Goobi/src/de/sub/goobi/persistence/managers/DatabaseVersion.java b/Goobi/src/de/sub/goobi/persistence/managers/DatabaseVersion.java index 441ab4e66..c5f476141 100644 --- a/Goobi/src/de/sub/goobi/persistence/managers/DatabaseVersion.java +++ b/Goobi/src/de/sub/goobi/persistence/managers/DatabaseVersion.java @@ -3,7 +3,7 @@ /** * This file is part of the Goobi Application - a Workflow tool for the support of mass digitization. * - * Visit the websites for more information. + * Visit the websites for more information. * - https://goobi.io * - https://www.intranda.com * - https://github.com/intranda/goobi @@ -45,7 +45,7 @@ public class DatabaseVersion { - public static final int EXPECTED_VERSION = 28; + public static final int EXPECTED_VERSION = 29; private static final Logger logger = Logger.getLogger(DatabaseVersion.class); // TODO ALTER TABLE metadata add fulltext(value) after mysql is version 5.6 or higher @@ -220,6 +220,11 @@ public static void updateDatabase(int currentVersion) { } updateToVersion28(); + case 28: + if (logger.isTraceEnabled()) { + logger.trace("Update database to version 29."); + } + updateToVersion29(); case 999: // this has to be the last case updateDatabaseVersion(currentVersion); @@ -229,13 +234,46 @@ public static void updateDatabase(int currentVersion) { } } + private static void updateToVersion29() { + Connection connection = null; + try { + connection = MySQLHelper.getInstance().getConnection(); + QueryRunner runner = new QueryRunner(); + + String sql = + "SELECT T.table_name, CCSA.character_set_name FROM information_schema.TABLES T, information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA WHERE CCSA.collation_name = T.table_collation AND T.table_schema = DATABASE() "; + + List tables = runner.query(connection, sql, MySQLHelper.resultSetToObjectListHandler); + + if (tables != null && !tables.isEmpty()) { + for (Object[] table : tables) { + if (!"utf8mb4".equals(table[1])) { + String tableName = (String) table[0]; + String conversion = "ALTER TABLE " + tableName + " CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"; + logger.info("Convert table to utf8mb4, old encoding of " + tableName + " is " + table[1]); + runner.update(connection, conversion); + } + } + } + } catch (SQLException e) { + logger.error(e); + } finally { + if (connection != null) { + try { + MySQLHelper.closeConnection(connection); + } catch (SQLException e) { + } + } + } + + } + private static void updateToVersion28() { Connection connection = null; try { connection = MySQLHelper.getInstance().getConnection(); QueryRunner runner = new QueryRunner(); - runner.update(connection, - "alter table schritte add column httpCloseStep tinyint(1);"); + runner.update(connection, "alter table schritte add column httpCloseStep tinyint(1);"); } catch (SQLException e) { logger.error(e); } finally { @@ -253,14 +291,10 @@ private static void updateToVersion27() { try { connection = MySQLHelper.getInstance().getConnection(); QueryRunner runner = new QueryRunner(); - runner.update(connection, - "alter table schritte add column httpStep boolean DEFAULT false;"); - runner.update(connection, - "alter table schritte add column httpMethod varchar(15);"); - runner.update(connection, - "alter table schritte add column httpUrl text;"); - runner.update(connection, - "alter table schritte add column httpJsonBody text;"); + runner.update(connection, "alter table schritte add column httpStep boolean DEFAULT false;"); + runner.update(connection, "alter table schritte add column httpMethod varchar(15);"); + runner.update(connection, "alter table schritte add column httpUrl text;"); + runner.update(connection, "alter table schritte add column httpJsonBody text;"); } catch (SQLException e) { logger.error(e); } finally { @@ -1138,8 +1172,8 @@ public static boolean checkIfColumnExists(String tableName, String columnName) { Connection connection = null; try { connection = MySQLHelper.getInstance().getConnection(); - String value = - new QueryRunner().query(connection, sql, MySQLHelper.resultSetToStringHandler, connection.getCatalog(), tableName, columnName); + String value = new QueryRunner().query(connection, sql, MySQLHelper.resultSetToStringHandler, connection.getCatalog(), tableName, + columnName); return StringUtils.isNotBlank(value); } catch (SQLException e) { logger.error(e);