diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index a9908eb30..7a8d9fa69 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -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 + "=?;"; diff --git a/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java b/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java index fddf3b650..b0186f4b7 100644 --- a/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java @@ -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 + "=?;"; diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index 08d6f7dbe..da1851bd3 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -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); diff --git a/src/main/java/fr/xephi/authme/datasource/SqLiteMigrater.java b/src/main/java/fr/xephi/authme/datasource/SqLiteMigrater.java index b4a2a577b..a9da0dd91 100644 --- a/src/main/java/fr/xephi/authme/datasource/SqLiteMigrater.java +++ b/src/main/java/fr/xephi/authme/datasource/SqLiteMigrater.java @@ -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; } /** diff --git a/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java b/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java index b115b276b..1a936f024 100644 --- a/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java +++ b/src/main/java/fr/xephi/authme/datasource/SqlDataSourceUtils.java @@ -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"); + } + } }