Skip to content

Commit

Permalink
convert database tables to utf8mb4
Browse files Browse the repository at this point in the history
  • Loading branch information
rsehr committed May 8, 2019
1 parent e167114 commit b2fbd68
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions Goobi/src/de/sub/goobi/persistence/managers/DatabaseVersion.java
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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<Object[]> 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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b2fbd68

Please sign in to comment.