Fixed AlterTableV16 placing all worlds under the same server

This commit is contained in:
Rsl1122 2018-04-07 12:32:39 +03:00
parent 8c2feb312c
commit 1e993ed0dd
4 changed files with 111 additions and 6 deletions

View File

@ -27,8 +27,10 @@ import java.util.stream.Collectors;
*/
public class SessionsTable extends UserIDTable {
public static final String TABLE_NAME = "plan_sessions";
public SessionsTable(SQLDB db) {
super("plan_sessions", db);
super(TABLE_NAME, db);
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " ("
+ Col.USER_ID + ", "

View File

@ -29,11 +29,12 @@ import java.util.stream.Collectors;
*/
public class WorldTable extends Table {
public static final String TABLE_NAME = "plan_worlds";
public final String statementSelectID;
private final ServerTable serverTable;
public WorldTable(SQLDB db) {
super("plan_worlds", db);
super(TABLE_NAME, db);
serverTable = db.getServerTable();
statementSelectID = "(SELECT " + Col.ID + " FROM " + tableName +
" WHERE (" + Col.NAME + "=?)" +
@ -175,7 +176,7 @@ public class WorldTable extends Table {
public void prepare(PreparedStatement statement) throws SQLException {
for (String world : worldsToSave) {
statement.setString(1, world);
statement.setString(2, ServerInfo.getServerUUID().toString());
statement.setString(2, serverUUID.toString());
statement.addBatch();
}
}
@ -292,6 +293,7 @@ public class WorldTable extends Table {
.collect(Collectors.toMap(
Function.identity(),
oldWorld -> worldObjects.stream()
.filter(worldObj -> worldObj.serverId != 0)
.filter(worldObj -> worldObj.equals(oldWorld))
.collect(Collectors.toList()
)));
@ -346,7 +348,6 @@ public class WorldTable extends Table {
public List<WorldObj> processResults(ResultSet set) throws SQLException {
List<WorldObj> objects = new ArrayList<>();
while (set.next()) {
int worldID = set.getInt(Col.ID.get());
int serverID = set.getInt(Col.SERVER_ID.get());
String worldName = set.getString(Col.NAME.get());
@ -403,4 +404,13 @@ class WorldObj {
public int hashCode() {
return Objects.hashCode(name);
}
@Override
public String toString() {
return "{" +
"id=" + id +
", serverId=" + serverId +
", name='" + name + '\'' +
'}';
}
}

View File

@ -35,13 +35,14 @@ import java.util.stream.Collectors;
*/
public class WorldTimesTable extends UserIDTable {
public static final String TABLE_NAME = "plan_world_times";
private final ServerTable serverTable;
private final WorldTable worldTable;
private final SessionsTable sessionsTable;
private String insertStatement;
public WorldTimesTable(SQLDB db) {
super("plan_world_times", db);
super(TABLE_NAME, db);
worldTable = db.getWorldTable();
sessionsTable = db.getSessionsTable();
serverTable = db.getServerTable();

View File

@ -70,7 +70,7 @@ public class SQLiteTest {
StaticHolder.saveInstance(SQLiteTest.class, Plan.class);
Log.setErrorManager(new TestErrorManager());
// Log.setDebugMode("console");
Log.setDebugMode("console");
// Settings.DEV_MODE.setTemporaryValue(true);
db.init();
@ -936,4 +936,96 @@ public class SQLiteTest {
assertEquals(3, geolocations.size());
assertTrue(geolocations.contains(secondInfo.getGeolocation()));
}
@Test
public void testWorldTableAlterV16() throws SQLException {
saveUserOne();
new Table("test", db) {
@Override
public void createTable() {
try {
execute(
"INSERT INTO " + WorldTable.TABLE_NAME + " (" +
WorldTable.Col.NAME + ", " +
WorldTable.Col.SERVER_ID +
") VALUES ('Test', '0')"
);
execute(
"INSERT INTO " + SessionsTable.TABLE_NAME + " (" +
SessionsTable.Col.SESSION_START + ", " +
SessionsTable.Col.SESSION_END + ", " +
SessionsTable.Col.AFK_TIME + ", " +
SessionsTable.Col.DEATHS + ", " +
SessionsTable.Col.MOB_KILLS + ", " +
SessionsTable.Col.SERVER_ID + ", " +
SessionsTable.Col.USER_ID +
") VALUES ('0', '0', '0', '0', '0', '1', '1')"
);
execute(
"INSERT INTO " + WorldTimesTable.TABLE_NAME + " (" +
WorldTimesTable.Col.SERVER_ID + ", " +
WorldTimesTable.Col.SESSION_ID + ", " +
WorldTimesTable.Col.USER_ID + ", " +
WorldTimesTable.Col.WORLD_ID + ", " +
WorldTimesTable.Col.SURVIVAL + ", " +
WorldTimesTable.Col.CREATIVE + ", " +
WorldTimesTable.Col.SPECTATOR + ", " +
WorldTimesTable.Col.ADVENTURE +
") VALUES ('1', '1', '1', '1', '0','0','0','0')"
);
execute(
"INSERT INTO " + ServerTable.TABLE_NAME + " (" +
ServerTable.Col.SERVER_UUID + ", " +
ServerTable.Col.SERVER_ID + ", " +
ServerTable.Col.MAX_PLAYERS + ", " +
ServerTable.Col.WEBSERVER_ADDRESS + ", " +
ServerTable.Col.INSTALLED + ", " +
ServerTable.Col.NAME +
") VALUES ('" + UUID.randomUUID() + "', '2', '0', '0', '1', '2')"
);
execute(
"INSERT INTO " + SessionsTable.TABLE_NAME + " (" +
SessionsTable.Col.SESSION_START + ", " +
SessionsTable.Col.SESSION_END + ", " +
SessionsTable.Col.AFK_TIME + ", " +
SessionsTable.Col.DEATHS + ", " +
SessionsTable.Col.MOB_KILLS + ", " +
SessionsTable.Col.SERVER_ID + ", " +
SessionsTable.Col.USER_ID +
") VALUES ('0', '0', '0', '0', '0', '2', '1')"
);
execute(
"INSERT INTO " + WorldTimesTable.TABLE_NAME + " (" +
WorldTimesTable.Col.SERVER_ID + ", " +
WorldTimesTable.Col.SESSION_ID + ", " +
WorldTimesTable.Col.USER_ID + ", " +
WorldTimesTable.Col.WORLD_ID + ", " +
WorldTimesTable.Col.SURVIVAL + ", " +
WorldTimesTable.Col.CREATIVE + ", " +
WorldTimesTable.Col.SPECTATOR + ", " +
WorldTimesTable.Col.ADVENTURE +
") VALUES ('2', '2', '1', '1', '0','0','0','0')"
);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
}.createTable();
WorldTable worldTable = db.getWorldTable();
Map<Integer, List<Integer>> before = worldTable.getWorldIDsByServerIDs();
System.out.println("\nBefore: " + before);
System.out.println("Before: " + worldTable.getWorldObjects() + "\n");
worldTable.alterTableV16();
Map<Integer, List<Integer>> after = worldTable.getWorldIDsByServerIDs();
System.out.println("\nAfter: " + after);
System.out.println("After: " + worldTable.getWorldObjects() + "\n");
assertNull(after.get(0));
assertNotNull(after.get(1));
assertEquals(1, after.get(1).size());
assertNotNull(after.get(2));
assertEquals(1, after.get(2).size());
}
}