Skip to content

Commit

Permalink
SEBSERV-562 DB migration charset utf8mb4
Browse files Browse the repository at this point in the history
  • Loading branch information
anhefti committed Jul 9, 2024
1 parent fc6e5be commit b7fb95a
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@Lazy
@Component
@WebServiceProfile
@Deprecated // This should not be needed anymore since we have a migration task now that alters the table and set charset
public class TableCharsetCheck implements DBIntegrityCheck {

private static final String SCHEMA_NAME_PROPERTY = "sebserver.init.database.integrity.check.schema";
Expand Down Expand Up @@ -63,50 +64,51 @@ public String description() {

@Override
public Result<String> applyCheck(final boolean tryFix) {

if (StringUtils.isEmpty(this.schemaName)) {
return Result.of("Skip check since sebserver.init.database.integrity.check.schema is not defined");
}

Connection connection = null;
try {
connection = this.dataSource.getConnection();

final PreparedStatement prepareStatement =
connection.prepareStatement(
"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = \"" + this.schemaName + "\"");
prepareStatement.execute();
final ResultSet resultSet = prepareStatement.getResultSet();
final Map<String, String> tablesWithWrongCollation = new HashMap<>();
while (resultSet.next()) {
final String collation = resultSet.getString(TABLE_COLLATION);
if (!UTF8MB4_GENERAL_CI.equals(collation)) {
tablesWithWrongCollation.put(resultSet.getString(TABLE_NAME), collation);
}
}

final Connection con = connection;
if (!tablesWithWrongCollation.isEmpty()) {
if (tryFix) {
tablesWithWrongCollation.entrySet().forEach(entry -> tryFix(con, entry));
} else {
return Result.of("Found tables with wrong collation: " + tablesWithWrongCollation);
}
}

} catch (final Exception e) {
log.error("Failed to apply database table check: ", e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (final SQLException e) {
log.error("Failed to close connection: ", e);
}
}
}

return Result.of("OK");
return Result.of("Skip check since this is done by a migration task");

// if (StringUtils.isEmpty(this.schemaName)) {
// return Result.of("Skip check since sebserver.init.database.integrity.check.schema is not defined");
// }
//
// Connection connection = null;
// try {
// connection = this.dataSource.getConnection();
//
// final PreparedStatement prepareStatement =
// connection.prepareStatement(
// "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = \"" + this.schemaName + "\"");
// prepareStatement.execute();
// final ResultSet resultSet = prepareStatement.getResultSet();
// final Map<String, String> tablesWithWrongCollation = new HashMap<>();
// while (resultSet.next()) {
// final String collation = resultSet.getString(TABLE_COLLATION);
// if (!UTF8MB4_GENERAL_CI.equals(collation)) {
// tablesWithWrongCollation.put(resultSet.getString(TABLE_NAME), collation);
// }
// }
//
// final Connection con = connection;
// if (!tablesWithWrongCollation.isEmpty()) {
// if (tryFix) {
// tablesWithWrongCollation.entrySet().forEach(entry -> tryFix(con, entry));
// } else {
// return Result.of("Found tables with wrong collation: " + tablesWithWrongCollation);
// }
// }
//
// } catch (final Exception e) {
// log.error("Failed to apply database table check: ", e);
// } finally {
// if (connection != null) {
// try {
// connection.close();
// } catch (final SQLException e) {
// log.error("Failed to close connection: ", e);
// }
// }
// }
//
// return Result.of("OK");
}

private void tryFix(final Connection connection, final Map.Entry<String, String> entry) {
Expand Down
Loading

0 comments on commit b7fb95a

Please sign in to comment.