Fix totp column size #2154

This commit is contained in:
Gabriele C 2020-07-16 00:18:51 +02:00
parent 790b959054
commit a1021f9dc3
3 changed files with 42 additions and 3 deletions

View File

@ -267,7 +267,10 @@ public class MySQL extends AbstractSqlDataSource {
if (isColumnMissing(md, col.TOTP_KEY)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(16);");
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
} else if (isColumnSizeIncorrect(md, col.TOTP_KEY, 32)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ALTER COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
}
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
@ -284,6 +287,16 @@ public class MySQL extends AbstractSqlDataSource {
}
}
private boolean isColumnSizeIncorrect(DatabaseMetaData metaData, String columnName, int size) throws SQLException {
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
if (!rs.next()) {
throw new RuntimeException("Column " + columnName + " doesn't exist!");
}
int currentSize = rs.getInt("COLUMN_SIZE");
return size != currentSize;
}
}
@Override
public PlayerAuth getAuth(String user) {
String sql = "SELECT * FROM " + tableName + " WHERE " + col.NAME + "=?;";

View File

@ -242,7 +242,10 @@ public class PostgreSqlDataSource extends AbstractSqlDataSource {
if (isColumnMissing(md, col.TOTP_KEY)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(16);");
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
} else if (isColumnSizeIncorrect(md, col.TOTP_KEY, 32)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ALTER COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
}
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
@ -259,6 +262,16 @@ public class PostgreSqlDataSource extends AbstractSqlDataSource {
}
}
private boolean isColumnSizeIncorrect(DatabaseMetaData metaData, String columnName, int size) throws SQLException {
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
if (!rs.next()) {
throw new RuntimeException("Column " + columnName + " doesn't exist!");
}
int currentSize = rs.getInt("COLUMN_SIZE");
return size != currentSize;
}
}
@Override
public PlayerAuth getAuth(String user) {
String sql = "SELECT * FROM " + tableName + " WHERE " + col.NAME + "=?;";

View File

@ -183,7 +183,10 @@ public class SQLite extends AbstractSqlDataSource {
if (isColumnMissing(md, col.TOTP_KEY)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(16);");
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
} else if (isColumnSizeIncorrect(md, col.TOTP_KEY, 32)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ALTER COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
}
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
@ -214,6 +217,16 @@ public class SQLite extends AbstractSqlDataSource {
}
}
private boolean isColumnSizeIncorrect(DatabaseMetaData metaData, String columnName, int size) throws SQLException {
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
if (!rs.next()) {
throw new RuntimeException("Column " + columnName + " doesn't exist!");
}
int currentSize = rs.getInt("COLUMN_SIZE");
return size != currentSize;
}
}
@Override
public void reload() {
close(con);