diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 501758181..a9908eb30 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -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 + "=?;"; diff --git a/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java b/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java index 566166d60..fddf3b650 100644 --- a/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/PostgreSqlDataSource.java @@ -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 + "=?;"; diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index 3ca138d59..08d6f7dbe 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -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);