Attempt to fix issue #2158

This commit is contained in:
Gabriele C 2020-07-17 18:07:42 +02:00
parent a1021f9dc3
commit e291a0415b
5 changed files with 26 additions and 38 deletions

View File

@ -268,9 +268,9 @@ public class MySQL extends AbstractSqlDataSource {
if (isColumnMissing(md, col.TOTP_KEY)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
} else if (isColumnSizeIncorrect(md, col.TOTP_KEY, 32)) {
} else if (SqlDataSourceUtils.getColumnSize(md, tableName, col.TOTP_KEY) != 32) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ALTER COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
+ " MODIFY " + col.TOTP_KEY + " VARCHAR(32);");
}
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
@ -287,16 +287,6 @@ 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

@ -243,9 +243,9 @@ public class PostgreSqlDataSource extends AbstractSqlDataSource {
if (isColumnMissing(md, col.TOTP_KEY)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
} else if (isColumnSizeIncorrect(md, col.TOTP_KEY, 32)) {
} else if (SqlDataSourceUtils.getColumnSize(md, tableName, col.TOTP_KEY) != 32) {
st.executeUpdate("ALTER TABLE " + tableName
+ " ALTER COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
+ " ALTER COLUMN " + col.TOTP_KEY + " TYPE VARCHAR(32);");
}
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
@ -262,16 +262,6 @@ 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

@ -184,9 +184,6 @@ public class SQLite extends AbstractSqlDataSource {
if (isColumnMissing(md, col.TOTP_KEY)) {
st.executeUpdate("ALTER TABLE " + tableName
+ " 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)) {
@ -217,16 +214,6 @@ 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);

View File

@ -46,7 +46,8 @@ class SqLiteMigrater {
*/
static boolean isMigrationRequired(DatabaseMetaData metaData, String tableName, Columns col) throws SQLException {
return SqlDataSourceUtils.isNotNullColumn(metaData, tableName, col.LAST_IP)
&& SqlDataSourceUtils.getColumnDefaultValue(metaData, tableName, col.LAST_IP) == null;
&& SqlDataSourceUtils.getColumnDefaultValue(metaData, tableName, col.LAST_IP) == null
&& SqlDataSourceUtils.getColumnSize(metaData, tableName, col.TOTP_KEY) != 32;
}
/**

View File

@ -86,4 +86,24 @@ public final class SqlDataSourceUtils {
return rs.getObject("COLUMN_DEF");
}
}
/**
* Returns the size of a column (as per its SQL definition).
*
* @param metaData the database meta data
* @param tableName the name of the table in which the column is
* @param columnName the name of the column to check
* @return the size of the column
* @throws SQLException :)
*/
public static int getColumnSize(DatabaseMetaData metaData, String tableName,
String columnName) throws SQLException {
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
if (!rs.next()) {
throw new IllegalStateException("Did not find meta data for column '"
+ columnName + "' while checking its size");
}
return rs.getInt("COLUMN_SIZE");
}
}
}