API сессий: добавить sessionType и clientPlatform

This commit is contained in:
AidarKC
2026-06-13 14:15:42 +04:00
parent 3b8ea70d3c
commit 919387f581
21 changed files with 446 additions and 15 deletions
@@ -178,6 +178,8 @@ public final class DatabaseInitializer {
client_ip TEXT,
client_info_from_client TEXT,
client_info_from_request TEXT,
session_type INTEGER NOT NULL DEFAULT 1,
client_platform TEXT NOT NULL DEFAULT '',
user_language TEXT,
FOREIGN KEY (login) REFERENCES solana_users(login)
);
@@ -14,7 +14,7 @@ import java.sql.Statement;
public final class SqliteDbController {
private static volatile SqliteDbController instance;
private static final int LATEST_SCHEMA_VERSION = 3;
private static final int LATEST_SCHEMA_VERSION = 4;
private final String jdbcUrl;
@@ -86,6 +86,7 @@ public final class SqliteDbController {
case 1 -> migrateToV1();
case 2 -> migrateToV2();
case 3 -> migrateToV3();
case 4 -> migrateToV4();
default -> throw new RuntimeException("Unknown DB migration target version: " + targetVersion);
}
}
@@ -168,6 +169,26 @@ public final class SqliteDbController {
}
}
private void migrateToV4() {
try (Connection c = DriverManager.getConnection(jdbcUrl);
Statement st = c.createStatement()) {
c.setAutoCommit(false);
try {
ensureActiveSessionsSessionTypeColumn(c, st);
ensureActiveSessionsClientPlatformColumn(c, st);
setSchemaVersion(c, 4);
c.commit();
} catch (Exception e) {
try { c.rollback(); } catch (Exception ignored) {}
throw new RuntimeException("DB migration to v4 failed", e);
} finally {
try { c.setAutoCommit(true); } catch (Exception ignored) {}
}
} catch (SQLException e) {
throw new RuntimeException("DB migration to v4 failed", e);
}
}
private static void ensureChat200StateTables(Statement st) throws SQLException {
st.executeUpdate("""
CREATE TABLE IF NOT EXISTS chat200_state (
@@ -235,6 +256,28 @@ public final class SqliteDbController {
}
}
private static void ensureActiveSessionsSessionTypeColumn(Connection c, Statement st) throws SQLException {
if (columnExists(c, "active_sessions", "session_type")) return;
st.executeUpdate("ALTER TABLE active_sessions ADD COLUMN session_type INTEGER NOT NULL DEFAULT 1");
}
private static void ensureActiveSessionsClientPlatformColumn(Connection c, Statement st) throws SQLException {
if (columnExists(c, "active_sessions", "client_platform")) return;
st.executeUpdate("ALTER TABLE active_sessions ADD COLUMN client_platform TEXT NOT NULL DEFAULT ''");
}
private static boolean columnExists(Connection c, String tableName, String columnName) throws SQLException {
try (Statement probe = c.createStatement();
ResultSet rs = probe.executeQuery("PRAGMA table_info(" + tableName + ")")) {
while (rs.next()) {
if (columnName.equalsIgnoreCase(rs.getString("name"))) {
return true;
}
}
return false;
}
}
private static void setSchemaVersion(Connection c, int version) throws SQLException {
try (var ps = c.prepareStatement("""
INSERT INTO db_schema_version (id, schema_version, updated_at_ms)
@@ -47,8 +47,10 @@ public final class ActiveSessionsDAO {
client_ip,
client_info_from_client,
client_info_from_request,
session_type,
client_platform,
user_language
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""";
try (PreparedStatement ps = c.prepareStatement(sql)) {
@@ -64,7 +66,9 @@ public final class ActiveSessionsDAO {
ps.setString(10, session.getClientIp());
ps.setString(11, session.getClientInfoFromClient());
ps.setString(12, session.getClientInfoFromRequest());
ps.setString(13, session.getUserLanguage());
ps.setInt(13, session.getSessionType());
ps.setString(14, session.getClientPlatform());
ps.setString(15, session.getUserLanguage());
ps.executeUpdate();
}
}
@@ -92,6 +96,8 @@ public final class ActiveSessionsDAO {
client_ip,
client_info_from_client,
client_info_from_request,
session_type,
client_platform,
user_language
FROM active_sessions
WHERE session_id = ?
@@ -127,6 +133,8 @@ public final class ActiveSessionsDAO {
client_ip,
client_info_from_client,
client_info_from_request,
session_type,
client_platform,
user_language
FROM active_sessions
WHERE login = ? COLLATE NOCASE
@@ -179,6 +187,8 @@ public final class ActiveSessionsDAO {
String clientIp,
String clientInfoFromClient,
String clientInfoFromRequest,
int sessionType,
String clientPlatform,
String userLanguage
) throws SQLException {
@@ -189,6 +199,8 @@ public final class ActiveSessionsDAO {
client_ip = ?,
client_info_from_client = ?,
client_info_from_request = ?,
session_type = ?,
client_platform = ?,
user_language = ?
WHERE session_id = ?
""";
@@ -198,8 +210,10 @@ public final class ActiveSessionsDAO {
ps.setString(2, clientIp);
ps.setString(3, clientInfoFromClient);
ps.setString(4, clientInfoFromRequest);
ps.setString(5, userLanguage);
ps.setString(6, sessionId);
ps.setInt(5, sessionType);
ps.setString(6, clientPlatform);
ps.setString(7, userLanguage);
ps.setString(8, sessionId);
ps.executeUpdate();
}
}
@@ -210,10 +224,12 @@ public final class ActiveSessionsDAO {
String clientIp,
String clientInfoFromClient,
String clientInfoFromRequest,
int sessionType,
String clientPlatform,
String userLanguage
) throws SQLException {
try (Connection c = db.getConnection()) {
updateOnRefresh(c, sessionId, lastAuthMs, clientIp, clientInfoFromClient, clientInfoFromRequest, userLanguage);
updateOnRefresh(c, sessionId, lastAuthMs, clientIp, clientInfoFromClient, clientInfoFromRequest, sessionType, clientPlatform, userLanguage);
}
}
@@ -268,6 +284,8 @@ public final class ActiveSessionsDAO {
String clientIp = rs.getString("client_ip");
String clientInfoFromClient = rs.getString("client_info_from_client");
String clientInfoFromRequest = rs.getString("client_info_from_request");
int sessionType = rs.getInt("session_type");
String clientPlatform = rs.getString("client_platform");
String userLanguage = rs.getString("user_language");
return new ActiveSessionEntry(
@@ -283,6 +301,8 @@ public final class ActiveSessionsDAO {
clientIp,
clientInfoFromClient,
clientInfoFromRequest,
sessionType,
clientPlatform,
userLanguage
);
}
@@ -22,6 +22,8 @@ public class ActiveSessionEntry {
private String clientIp;
private String clientInfoFromClient;
private String clientInfoFromRequest;
private int sessionType;
private String clientPlatform;
private String userLanguage;
public ActiveSessionEntry() { }
@@ -38,6 +40,8 @@ public class ActiveSessionEntry {
String clientIp,
String clientInfoFromClient,
String clientInfoFromRequest,
int sessionType,
String clientPlatform,
String userLanguage) {
this.sessionId = sessionId;
this.login = login;
@@ -51,6 +55,8 @@ public class ActiveSessionEntry {
this.clientIp = clientIp;
this.clientInfoFromClient = clientInfoFromClient;
this.clientInfoFromRequest = clientInfoFromRequest;
this.sessionType = sessionType;
this.clientPlatform = clientPlatform;
this.userLanguage = userLanguage;
}
@@ -90,6 +96,12 @@ public class ActiveSessionEntry {
public String getClientInfoFromRequest() { return clientInfoFromRequest; }
public void setClientInfoFromRequest(String clientInfoFromRequest) { this.clientInfoFromRequest = clientInfoFromRequest; }
public int getSessionType() { return sessionType; }
public void setSessionType(int sessionType) { this.sessionType = sessionType; }
public String getClientPlatform() { return clientPlatform; }
public void setClientPlatform(String clientPlatform) { this.clientPlatform = clientPlatform; }
public String getUserLanguage() { return userLanguage; }
public void setUserLanguage(String userLanguage) { this.userLanguage = userLanguage; }
}