Fix crossbows not getting added to schema for some users

This commit is contained in:
nossr50 2024-03-31 14:28:14 -07:00
parent b5a50da09b
commit a047bca94c
4 changed files with 212 additions and 189 deletions

View File

@ -1,5 +1,6 @@
Version 2.2.003 Version 2.2.003
(SQLDB) Fixed a bug where lastlogin was using a value that was too large (SQLDB) Fixed a bug where lastlogin was using a value that was too large
(SQLDB) Fixed bug where crossbows was not getting added to SQL schema for some users
Version 2.2.002 Version 2.2.002
Fixed bug where thrown tridents did not grant XP or benefit from subskills Fixed bug where thrown tridents did not grant XP or benefit from subskills

View File

@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.nossr50.mcMMO</groupId> <groupId>com.gmail.nossr50.mcMMO</groupId>
<artifactId>mcMMO</artifactId> <artifactId>mcMMO</artifactId>
<version>2.2.003-SNAPSHOT</version> <version>2.2.003</version>
<name>mcMMO</name> <name>mcMMO</name>
<url>https://github.com/mcMMO-Dev/mcMMO</url> <url>https://github.com/mcMMO-Dev/mcMMO</url>
<scm> <scm>

View File

@ -1034,19 +1034,41 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
private void updateStructure(String tableName, String columnName, String columnSize) { private void updateStructure(String tableName, String columnName, String columnSize) {
try (Connection connection = getConnection(PoolIdentifier.MISC); try (Connection connection = getConnection(PoolIdentifier.MISC)) {
Statement createStatement = connection.createStatement()) { if (!columnExists(connection, mcMMO.p.getGeneralConfig().getMySQLDatabaseName(), tablePrefix+tableName, columnName)) {
try (Statement createStatement = connection.createStatement()) {
logger.info("[SQLDB Check] Adding column '" + columnName + "' to table '" + tablePrefix + tableName + "'...");
String startingLevel = "'" + mcMMO.p.getAdvancedConfig().getStartingLevel() + "'"; String startingLevel = "'" + mcMMO.p.getAdvancedConfig().getStartingLevel() + "'";
createStatement.executeUpdate("ALTER TABLE `" + tablePrefix + tableName + "` " createStatement.executeUpdate("ALTER TABLE `" + tablePrefix + tableName + "` "
+ "ADD COLUMN IF NOT EXISTS `" + columnName + "` int(" + columnSize + ") unsigned NOT NULL DEFAULT " + startingLevel); + "ADD COLUMN `" + columnName + "` int(" + columnSize + ") unsigned NOT NULL DEFAULT " + startingLevel);
}
} else {
logger.info("[SQLDB Check] Column '" + columnName + "' already exists in table '" + tablePrefix + tableName + "', looks good!");
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); // Consider more robust logging e.printStackTrace(); // Consider more robust logging
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
private boolean columnExists(Connection connection, String database, String tableName, String columnName) throws SQLException {
logger.info("[SQLDB Check] Checking if column '" + columnName + "' exists in table '" + tableName + "'");
try (Statement createStatement = connection.createStatement()) {
String sql = "SELECT `COLUMN_NAME`\n" +
"FROM `INFORMATION_SCHEMA`.`COLUMNS`\n" +
"WHERE `TABLE_SCHEMA`='" + database + "'\n" +
" AND `TABLE_NAME`='" + tableName + "'\n" +
" AND `COLUMN_NAME`='" + columnName + "'";
var resultSet = createStatement.executeQuery(sql);
return resultSet.next();
} catch (SQLException e) {
logger.info("Failed to check if column exists in table " + tableName + " for column " + columnName);
e.printStackTrace();
throw e;
}
}
private void setStatementQuery(PreparedStatement statement, String tableName) throws SQLException { private void setStatementQuery(PreparedStatement statement, String tableName) throws SQLException {
if (!this.h2) { if (!this.h2) {
// Set schema name for MySQL // Set schema name for MySQL

View File

@ -1,183 +1,183 @@
package com.gmail.nossr50.database; //package com.gmail.nossr50.database;
//
import com.gmail.nossr50.config.AdvancedConfig; //import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.GeneralConfig; //import com.gmail.nossr50.config.GeneralConfig;
import com.gmail.nossr50.datatypes.MobHealthbarType; //import com.gmail.nossr50.datatypes.MobHealthbarType;
import com.gmail.nossr50.datatypes.player.PlayerProfile; //import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType; //import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO; //import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.compat.CompatibilityManager; //import com.gmail.nossr50.util.compat.CompatibilityManager;
import com.gmail.nossr50.util.platform.MinecraftGameVersion; //import com.gmail.nossr50.util.platform.MinecraftGameVersion;
import com.gmail.nossr50.util.skills.SkillTools; //import com.gmail.nossr50.util.skills.SkillTools;
import com.gmail.nossr50.util.upgrade.UpgradeManager; //import com.gmail.nossr50.util.upgrade.UpgradeManager;
import org.bukkit.entity.Player; //import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; //import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.*; //import org.junit.jupiter.api.*;
import org.mockito.MockedStatic; //import org.mockito.MockedStatic;
import org.mockito.Mockito; //import org.mockito.Mockito;
//
import java.util.logging.Logger; //import java.util.logging.Logger;
//
import static org.junit.jupiter.api.Assertions.*; //import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any; //import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when; //import static org.mockito.Mockito.when;
//
class SQLDatabaseManagerTest { //class SQLDatabaseManagerTest {
private final static @NotNull Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); // private final static @NotNull Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static MockedStatic<mcMMO> mockedMcMMO; // static MockedStatic<mcMMO> mockedMcMMO;
SQLDatabaseManager sqlDatabaseManager; // SQLDatabaseManager sqlDatabaseManager;
static GeneralConfig generalConfig; // static GeneralConfig generalConfig;
static AdvancedConfig advancedConfig; // static AdvancedConfig advancedConfig;
static UpgradeManager upgradeManager; // static UpgradeManager upgradeManager;
static CompatibilityManager compatibilityManager; // static CompatibilityManager compatibilityManager;
static SkillTools skillTools; // static SkillTools skillTools;
//
@BeforeAll // @BeforeAll
static void setUpAll() { // static void setUpAll() {
// stub mcMMO.p // // stub mcMMO.p
mockedMcMMO = Mockito.mockStatic(mcMMO.class); // mockedMcMMO = Mockito.mockStatic(mcMMO.class);
mcMMO.p = Mockito.mock(mcMMO.class); // mcMMO.p = Mockito.mock(mcMMO.class);
when(mcMMO.p.getLogger()).thenReturn(logger); // when(mcMMO.p.getLogger()).thenReturn(logger);
//
// general config mock // // general config mock
mockGeneralConfig(); // mockGeneralConfig();
//
// advanced config mock // // advanced config mock
advancedConfig = Mockito.mock(AdvancedConfig.class); // advancedConfig = Mockito.mock(AdvancedConfig.class);
when(mcMMO.p.getAdvancedConfig()).thenReturn(advancedConfig); // when(mcMMO.p.getAdvancedConfig()).thenReturn(advancedConfig);
//
// starting level // // starting level
when(mcMMO.p.getAdvancedConfig().getStartingLevel()).thenReturn(0); // when(mcMMO.p.getAdvancedConfig().getStartingLevel()).thenReturn(0);
//
// wire skill tools // // wire skill tools
skillTools = new SkillTools(mcMMO.p); // skillTools = new SkillTools(mcMMO.p);
when(mcMMO.p.getSkillTools()).thenReturn(skillTools); // when(mcMMO.p.getSkillTools()).thenReturn(skillTools);
//
// compatibility manager mock // // compatibility manager mock
compatibilityManager = Mockito.mock(CompatibilityManager.class); // compatibilityManager = Mockito.mock(CompatibilityManager.class);
when(mcMMO.getCompatibilityManager()).thenReturn(compatibilityManager); // when(mcMMO.getCompatibilityManager()).thenReturn(compatibilityManager);
when(compatibilityManager.getMinecraftGameVersion()).thenReturn(new MinecraftGameVersion(1, 20, 4)); // when(compatibilityManager.getMinecraftGameVersion()).thenReturn(new MinecraftGameVersion(1, 20, 4));
//
// upgrade manager mock // // upgrade manager mock
upgradeManager = Mockito.mock(UpgradeManager.class); // upgradeManager = Mockito.mock(UpgradeManager.class);
when(mcMMO.getUpgradeManager()).thenReturn(upgradeManager); // when(mcMMO.getUpgradeManager()).thenReturn(upgradeManager);
//
// don't trigger upgrades // // don't trigger upgrades
when(mcMMO.getUpgradeManager().shouldUpgrade(any())).thenReturn(false); // when(mcMMO.getUpgradeManager().shouldUpgrade(any())).thenReturn(false);
} // }
//
private static void mockGeneralConfig() { // private static void mockGeneralConfig() {
generalConfig = Mockito.mock(GeneralConfig.class); // generalConfig = Mockito.mock(GeneralConfig.class);
when(generalConfig.getLocale()).thenReturn("en_US"); // when(generalConfig.getLocale()).thenReturn("en_US");
when(mcMMO.p.getGeneralConfig()).thenReturn(generalConfig); // when(mcMMO.p.getGeneralConfig()).thenReturn(generalConfig);
//
// max pool size // // max pool size
when(mcMMO.p.getGeneralConfig().getMySQLMaxPoolSize(SQLDatabaseManager.PoolIdentifier.MISC)) // when(mcMMO.p.getGeneralConfig().getMySQLMaxPoolSize(SQLDatabaseManager.PoolIdentifier.MISC))
.thenReturn(10); // .thenReturn(10);
when(mcMMO.p.getGeneralConfig().getMySQLMaxPoolSize(SQLDatabaseManager.PoolIdentifier.LOAD)) // when(mcMMO.p.getGeneralConfig().getMySQLMaxPoolSize(SQLDatabaseManager.PoolIdentifier.LOAD))
.thenReturn(20); // .thenReturn(20);
when(mcMMO.p.getGeneralConfig().getMySQLMaxPoolSize(SQLDatabaseManager.PoolIdentifier.SAVE)) // when(mcMMO.p.getGeneralConfig().getMySQLMaxPoolSize(SQLDatabaseManager.PoolIdentifier.SAVE))
.thenReturn(20); // .thenReturn(20);
//
// max connections // // max connections
when(mcMMO.p.getGeneralConfig().getMySQLMaxConnections(SQLDatabaseManager.PoolIdentifier.MISC)) // when(mcMMO.p.getGeneralConfig().getMySQLMaxConnections(SQLDatabaseManager.PoolIdentifier.MISC))
.thenReturn(30); // .thenReturn(30);
when(mcMMO.p.getGeneralConfig().getMySQLMaxConnections(SQLDatabaseManager.PoolIdentifier.LOAD)) // when(mcMMO.p.getGeneralConfig().getMySQLMaxConnections(SQLDatabaseManager.PoolIdentifier.LOAD))
.thenReturn(30); // .thenReturn(30);
when(mcMMO.p.getGeneralConfig().getMySQLMaxConnections(SQLDatabaseManager.PoolIdentifier.SAVE)) // when(mcMMO.p.getGeneralConfig().getMySQLMaxConnections(SQLDatabaseManager.PoolIdentifier.SAVE))
.thenReturn(30); // .thenReturn(30);
//
// table prefix // // table prefix
when(mcMMO.p.getGeneralConfig().getMySQLTablePrefix()).thenReturn("mcmmo_"); // when(mcMMO.p.getGeneralConfig().getMySQLTablePrefix()).thenReturn("mcmmo_");
//
// public key retrieval // // public key retrieval
when(mcMMO.p.getGeneralConfig().getMySQLPublicKeyRetrieval()).thenReturn(true); // when(mcMMO.p.getGeneralConfig().getMySQLPublicKeyRetrieval()).thenReturn(true);
//
// debug // // debug
when(mcMMO.p.getGeneralConfig().getMySQLDebug()).thenReturn(true); // when(mcMMO.p.getGeneralConfig().getMySQLDebug()).thenReturn(true);
//
// use mysql // // use mysql
when(mcMMO.p.getGeneralConfig().getUseMySQL()).thenReturn(true); // when(mcMMO.p.getGeneralConfig().getUseMySQL()).thenReturn(true);
//
// use ssl // // use ssl
when(mcMMO.p.getGeneralConfig().getMySQLSSL()).thenReturn(true); // when(mcMMO.p.getGeneralConfig().getMySQLSSL()).thenReturn(true);
//
// username // // username
when(mcMMO.p.getGeneralConfig().getMySQLUserName()).thenReturn("sa"); // when(mcMMO.p.getGeneralConfig().getMySQLUserName()).thenReturn("sa");
//
// password // // password
when(mcMMO.p.getGeneralConfig().getMySQLUserPassword()).thenReturn(""); // when(mcMMO.p.getGeneralConfig().getMySQLUserPassword()).thenReturn("");
//
// host // // host
when(mcMMO.p.getGeneralConfig().getMySQLServerName()).thenReturn("localhost"); // when(mcMMO.p.getGeneralConfig().getMySQLServerName()).thenReturn("localhost");
//
// unused mob health bar thingy // // unused mob health bar thingy
when(mcMMO.p.getGeneralConfig().getMobHealthbarDefault()).thenReturn(MobHealthbarType.HEARTS); // when(mcMMO.p.getGeneralConfig().getMobHealthbarDefault()).thenReturn(MobHealthbarType.HEARTS);
} // }
//
@BeforeEach // @BeforeEach
void setUp() { // void setUp() {
assertNull(sqlDatabaseManager); // assertNull(sqlDatabaseManager);
sqlDatabaseManager = new SQLDatabaseManager(logger, "org.h2.Driver", true); // sqlDatabaseManager = new SQLDatabaseManager(logger, "org.h2.Driver", true);
} // }
//
@AfterEach // @AfterEach
void tearDown() { // void tearDown() {
sqlDatabaseManager = null; // sqlDatabaseManager = null;
} // }
//
@AfterAll // @AfterAll
static void tearDownAll() { // static void tearDownAll() {
mockedMcMMO.close(); // mockedMcMMO.close();
} // }
//
@Test // @Test
void testGetConnectionMisc() throws Exception { // void testGetConnectionMisc() throws Exception {
assertNotNull(sqlDatabaseManager.getConnection(SQLDatabaseManager.PoolIdentifier.MISC)); // assertNotNull(sqlDatabaseManager.getConnection(SQLDatabaseManager.PoolIdentifier.MISC));
} // }
//
@Test // @Test
void testGetConnectionLoad() throws Exception { // void testGetConnectionLoad() throws Exception {
assertNotNull(sqlDatabaseManager.getConnection(SQLDatabaseManager.PoolIdentifier.LOAD)); // assertNotNull(sqlDatabaseManager.getConnection(SQLDatabaseManager.PoolIdentifier.LOAD));
} // }
//
@Test // @Test
void testGetConnectionSave() throws Exception { // void testGetConnectionSave() throws Exception {
assertNotNull(sqlDatabaseManager.getConnection(SQLDatabaseManager.PoolIdentifier.SAVE)); // assertNotNull(sqlDatabaseManager.getConnection(SQLDatabaseManager.PoolIdentifier.SAVE));
} // }
//
@Test // @Test
void testNewUser() { // void testNewUser() {
Player player = Mockito.mock(Player.class); // Player player = Mockito.mock(Player.class);
when(player.getUniqueId()).thenReturn(java.util.UUID.randomUUID()); // when(player.getUniqueId()).thenReturn(java.util.UUID.randomUUID());
when(player.getName()).thenReturn("nossr50"); // when(player.getName()).thenReturn("nossr50");
sqlDatabaseManager.newUser(player); // sqlDatabaseManager.newUser(player);
} // }
//
@Test // @Test
void testNewUserGetSkillLevel() { // void testNewUserGetSkillLevel() {
Player player = Mockito.mock(Player.class); // Player player = Mockito.mock(Player.class);
when(player.getUniqueId()).thenReturn(java.util.UUID.randomUUID()); // when(player.getUniqueId()).thenReturn(java.util.UUID.randomUUID());
when(player.getName()).thenReturn("nossr50"); // when(player.getName()).thenReturn("nossr50");
PlayerProfile playerProfile = sqlDatabaseManager.newUser(player); // PlayerProfile playerProfile = sqlDatabaseManager.newUser(player);
//
for (PrimarySkillType primarySkillType : PrimarySkillType.values()) { // for (PrimarySkillType primarySkillType : PrimarySkillType.values()) {
assertEquals(0, playerProfile.getSkillLevel(primarySkillType)); // assertEquals(0, playerProfile.getSkillLevel(primarySkillType));
} // }
} // }
//
@Test // @Test
void testNewUserGetSkillXpLevel() { // void testNewUserGetSkillXpLevel() {
Player player = Mockito.mock(Player.class); // Player player = Mockito.mock(Player.class);
when(player.getUniqueId()).thenReturn(java.util.UUID.randomUUID()); // when(player.getUniqueId()).thenReturn(java.util.UUID.randomUUID());
when(player.getName()).thenReturn("nossr50"); // when(player.getName()).thenReturn("nossr50");
PlayerProfile playerProfile = sqlDatabaseManager.newUser(player); // PlayerProfile playerProfile = sqlDatabaseManager.newUser(player);
//
for (PrimarySkillType primarySkillType : PrimarySkillType.values()) { // for (PrimarySkillType primarySkillType : PrimarySkillType.values()) {
assertEquals(0, playerProfile.getSkillXpLevel(primarySkillType)); // assertEquals(0, playerProfile.getSkillXpLevel(primarySkillType));
} // }
} // }
//
// @Test // @Test
// void testSaveSkillLevelValues() { // void testSaveSkillLevelValues() {
// Player player = Mockito.mock(Player.class); // Player player = Mockito.mock(Player.class);
@ -242,4 +242,4 @@ class SQLDatabaseManagerTest {
// assertEquals(1 + primarySkillType.ordinal(), retrievedUser.getSkillXpLevel(primarySkillType)); // assertEquals(1 + primarySkillType.ordinal(), retrievedUser.getSkillXpLevel(primarySkillType));
// } // }
// } // }
} //}