When an older user no longer has a username that a new user now has, we will update the old entry to be _INVALID_OLD_USERNAME_, however we no longer strictly enforce name uniqueness, so people altering their DB's need to tweek with caution. Invalid old users will not display in the leaderboards. Fixes #2503

This commit is contained in:
t00thpick1 2015-06-20 12:57:01 -04:00
parent b4e21a7817
commit 4388430491
2 changed files with 52 additions and 3 deletions

View File

@ -393,7 +393,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try { try {
connection = getConnection(PoolIdentifier.MISC); connection = getConnection(PoolIdentifier.MISC);
statement = connection.prepareStatement("SELECT " + query + ", user, NOW() FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON (user_id = id) WHERE " + query + " > 0 ORDER BY " + query + " DESC, user LIMIT ?, ?"); statement = connection.prepareStatement("SELECT " + query + ", user, NOW() FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON (user_id = id) WHERE " + query + " > 0 AND NOT user = '\\_INVALID\\_OLD\\_USERNAME\\_' ORDER BY " + query + " DESC, user LIMIT ?, ?");
statement.setInt(1, (pageNumber * statsPerPage) - statsPerPage); statement.setInt(1, (pageNumber * statsPerPage) - statsPerPage);
statement.setInt(2, statsPerPage); statement.setInt(2, statsPerPage);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -583,6 +583,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
statement = connection.prepareStatement(
"UPDATE `" + tablePrefix + "users` "
+ "SET user = ? "
+ "WHERE user = ?");
statement.setString(1, "_INVALID_OLD_USERNAME_");
statement.setString(2, playerName);
statement.executeUpdate();
statement.close();
statement = connection.prepareStatement("INSERT INTO " + tablePrefix + "users (user, uuid, lastlogin) VALUES (?, ?, UNIX_TIMESTAMP())", Statement.RETURN_GENERATED_KEYS); statement = connection.prepareStatement("INSERT INTO " + tablePrefix + "users (user, uuid, lastlogin) VALUES (?, ?, UNIX_TIMESTAMP())", Statement.RETURN_GENERATED_KEYS);
statement.setString(1, playerName); statement.setString(1, playerName);
statement.setString(2, uuid != null ? uuid.toString() : null); statement.setString(2, uuid != null ? uuid.toString() : null);
@ -683,6 +691,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.close(); statement.close();
if (!playerName.isEmpty() && !playerName.equals(name)) { if (!playerName.isEmpty() && !playerName.equals(name)) {
statement = connection.prepareStatement(
"UPDATE `" + tablePrefix + "users` "
+ "SET user = ? "
+ "WHERE user = ?");
statement.setString(1, "_INVALID_OLD_USERNAME_");
statement.setString(2, name);
statement.executeUpdate();
statement.close();
statement = connection.prepareStatement( statement = connection.prepareStatement(
"UPDATE `" + tablePrefix + "users` " "UPDATE `" + tablePrefix + "users` "
+ "SET user = ?, uuid = ? " + "SET user = ?, uuid = ? "
@ -979,7 +995,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ "`uuid` varchar(36) NULL DEFAULT NULL," + "`uuid` varchar(36) NULL DEFAULT NULL,"
+ "`lastlogin` int(32) unsigned NOT NULL," + "`lastlogin` int(32) unsigned NOT NULL,"
+ "PRIMARY KEY (`id`)," + "PRIMARY KEY (`id`),"
+ "UNIQUE KEY `user` (`user`)," + "INDEX(`user`(20) ASC),"
+ "UNIQUE KEY `uuid` (`uuid`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;"); + "UNIQUE KEY `uuid` (`uuid`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
createStatement.close(); createStatement.close();
} }
@ -1197,6 +1213,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
checkUpgradeAddScoreboardTips(statement); checkUpgradeAddScoreboardTips(statement);
return; return;
case DROP_NAME_UNIQUENESS:
checkNameUniqueness(statement);
return;
default: default:
break; break;
@ -1351,6 +1371,34 @@ public final class SQLDatabaseManager implements DatabaseManager {
return DatabaseType.SQL; return DatabaseType.SQL;
} }
private void checkNameUniqueness(final Statement statement) throws SQLException {
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery("SHOW INDEXES"
+ "FROM `" + tablePrefix + "users` "
+ "WHERE Column_name='user' "
+ " AND NOT Non_unique");
if (!resultSet.next()) {
return;
}
resultSet.close();
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables to drop name uniqueness...");
statement.execute("ALTER TABLE `" + tablePrefix + "users`"
+ "DROP INDEX `user`,"
+ "ADD INDEX `user` (`user`(20) ASC)");
} catch (SQLException ex) {
} finally {
if (resultSet != null) {
try {
resultSet.close();
}
catch (SQLException e) {
// Ignore
}
}
}
}
private void checkUpgradeAddAlchemy(final Statement statement) throws SQLException { private void checkUpgradeAddAlchemy(final Statement statement) throws SQLException {
try { try {
statement.executeQuery("SELECT `alchemy` FROM `" + tablePrefix + "skills` LIMIT 1"); statement.executeQuery("SELECT `alchemy` FROM `" + tablePrefix + "skills` LIMIT 1");

View File

@ -10,5 +10,6 @@ public enum UpgradeType {
ADD_ALCHEMY, ADD_ALCHEMY,
ADD_UUIDS, ADD_UUIDS,
ADD_UUIDS_PARTY, ADD_UUIDS_PARTY,
ADD_SCOREBOARD_TIPS; ADD_SCOREBOARD_TIPS,
DROP_NAME_UNIQUENESS;
} }