Fix oversized player statistic keys
This commit is contained in:
@@ -12,6 +12,8 @@ import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
final class SqlStatsStorage implements StatsStorage {
|
||||
private static final int STAT_KEY_LENGTH = 512;
|
||||
|
||||
private final String url;
|
||||
private final String username;
|
||||
private final String password;
|
||||
@@ -54,7 +56,7 @@ final class SqlStatsStorage implements StatsStorage {
|
||||
CREATE TABLE IF NOT EXISTS rose_player_stats (
|
||||
uuid CHAR(36) NOT NULL,
|
||||
player_name VARCHAR(64) NOT NULL,
|
||||
stat_key VARCHAR(190) NOT NULL,
|
||||
stat_key VARCHAR(512) NOT NULL,
|
||||
stat_value BIGINT NOT NULL,
|
||||
PRIMARY KEY (uuid, stat_key),
|
||||
INDEX idx_stats_player_name (player_name),
|
||||
@@ -75,6 +77,31 @@ final class SqlStatsStorage implements StatsStorage {
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
""");
|
||||
}
|
||||
ensureStatKeyCapacity(db);
|
||||
}
|
||||
|
||||
private void ensureStatKeyCapacity(Connection db) throws SQLException {
|
||||
long currentLength = 0L;
|
||||
try (PreparedStatement statement = db.prepareStatement("""
|
||||
SELECT CHARACTER_MAXIMUM_LENGTH
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'rose_player_stats'
|
||||
AND COLUMN_NAME = 'stat_key'
|
||||
""")) {
|
||||
try (ResultSet result = statement.executeQuery()) {
|
||||
if (result.next()) {
|
||||
currentLength = result.getLong(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (currentLength > 0L && currentLength < STAT_KEY_LENGTH) {
|
||||
try (Statement statement = db.createStatement()) {
|
||||
statement.executeUpdate(
|
||||
"ALTER TABLE rose_player_stats MODIFY COLUMN stat_key VARCHAR(" + STAT_KEY_LENGTH + ") NOT NULL");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user