Reduce join_address column length to 250 characters

- Add error code 1071 to error definition

Affects issues:
- Possibly fixed #2477
This commit is contained in:
Aurora Lahtela 2022-07-14 08:19:08 +03:00
parent 85321899ad
commit 47bc49a0d2
7 changed files with 21 additions and 8 deletions

View File

@ -142,6 +142,10 @@ public class DBOpException extends IllegalStateException implements ExceptionWit
context.related(CONSTRAINT_VIOLATION)
.whatToDo("Report this error. NOT NULL constraint violation occurred.");
break;
case 1071:
context.related("column byte length exceeded")
.whatToDo("Update your MySQL, column key size was exceeded (max key length is 767 bytes in 5.6) - MySQL 5.7 increases the limit.");
break;
default:
context.related("Unknown SQL Error code");
}

View File

@ -196,7 +196,7 @@ public class DataStoreQueries {
statement.setLong(2, registered);
statement.setString(3, serverUUID.toString());
statement.setBoolean(4, false); // Banned
statement.setString(5, joinAddress);
statement.setString(5, StringUtils.truncate(joinAddress, JoinAddressTable.JOIN_ADDRESS_MAX_LENGTH));
statement.setBoolean(6, false); // Operator
}
};

View File

@ -27,6 +27,8 @@ public class JoinAddressTable {
public static final String TABLE_NAME = "plan_join_address";
public static final String ID = "id";
public static final String JOIN_ADDRESS = "join_address";
public static final int JOIN_ADDRESS_MAX_LENGTH = 250;
public static final String SELECT_ID = '(' + SELECT + ID + FROM + TABLE_NAME + WHERE + JOIN_ADDRESS + "=LOWER(?))";
public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME +
" (" + JOIN_ADDRESS + ") VALUES (LOWER(?))";
@ -37,7 +39,7 @@ public class JoinAddressTable {
public static String createTableSQL(DBType dbType) {
return CreateTableBuilder.create(TABLE_NAME, dbType)
.column(ID, Sql.INT).primaryKey()
.column(JOIN_ADDRESS, Sql.varchar(255)).unique()
.column(JOIN_ADDRESS, Sql.varchar(JOIN_ADDRESS_MAX_LENGTH)).unique()
.toString();
}

View File

@ -66,7 +66,7 @@ public class UserInfoTable {
.column(ID, Sql.INT).primaryKey()
.column(USER_ID, Sql.INT).notNull()
.column(SERVER_ID, Sql.INT).notNull()
.column(JOIN_ADDRESS, Sql.varchar(255))
.column(JOIN_ADDRESS, Sql.varchar(JoinAddressTable.JOIN_ADDRESS_MAX_LENGTH))
.column(REGISTERED, Sql.LONG).notNull()
.column(OP, Sql.BOOL).notNull().defaultValue(false)
.column(BANNED, Sql.BOOL).notNull().defaultValue(false)

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.storage.database.queries.Query;
import com.djrapitops.plan.storage.database.sql.tables.JoinAddressTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import org.apache.commons.lang3.StringUtils;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ -53,18 +54,22 @@ public class StoreJoinAddressTransaction extends Transaction {
return new HasMoreThanZeroQueryStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
String address = joinAddress.get();
statement.setString(1, address.toLowerCase());
String address = getAddress();
statement.setString(1, address);
}
};
}
private String getAddress() {
return StringUtils.truncate(joinAddress.get().toLowerCase(), JoinAddressTable.JOIN_ADDRESS_MAX_LENGTH);
}
@Override
protected void performOperations() {
execute(new ExecStatement(JoinAddressTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, joinAddress.get().toLowerCase());
statement.setString(1, getAddress());
}
});
}

View File

@ -24,6 +24,7 @@ import com.djrapitops.plan.storage.database.sql.tables.JoinAddressTable;
import com.djrapitops.plan.storage.database.sql.tables.SessionsTable;
import com.djrapitops.plan.storage.database.sql.tables.UserInfoTable;
import com.djrapitops.plan.storage.database.transactions.ExecBatchStatement;
import org.apache.commons.lang3.StringUtils;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -98,7 +99,7 @@ public class SessionJoinAddressPatch extends Patch {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (String joinAddress : joinAddresses) {
statement.setString(1, joinAddress.toLowerCase());
statement.setString(1, StringUtils.truncate(joinAddress.toLowerCase(), JoinAddressTable.JOIN_ADDRESS_MAX_LENGTH));
statement.addBatch();
}
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.storage.database.transactions.patches;
import com.djrapitops.plan.storage.database.sql.building.Sql;
import com.djrapitops.plan.storage.database.sql.tables.JoinAddressTable;
import com.djrapitops.plan.storage.database.sql.tables.UserInfoTable;
/**
@ -34,6 +35,6 @@ public class UserInfoHostnamePatch extends Patch {
@Override
protected void applyPatch() {
addColumn(UserInfoTable.TABLE_NAME, UserInfoTable.JOIN_ADDRESS + ' '
+ Sql.varchar(255));
+ Sql.varchar(JoinAddressTable.JOIN_ADDRESS_MAX_LENGTH));
}
}