diff --git a/database.js b/database.js index d2e1844..afc2617 100644 --- a/database.js +++ b/database.js @@ -43,24 +43,26 @@ const initDatabase = async () => { player_id VARCHAR(50), palworld_username VARCHAR(100) NOT NULL, linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - lastConnection TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_discord_id (discord_id), INDEX idx_steam_id (steam_id), INDEX idx_player_id (player_id) ) `); - // Migration: ajouter la colonne lastConnection si elle n'existe pas - await connection.execute(` - PRAGMA table_info(user_links) - `); - - const [columns] = await connection.execute(`PRAGMA table_info(user_links)`); + // Migration: ajouter la colonne lastConnection si elle n'existe pas (syntaxe MySQL) + const [columns] = await connection.execute(` + SELECT COLUMN_NAME + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = ? + AND TABLE_NAME = 'user_links' + AND COLUMN_NAME = 'lastConnection' + `, [process.env.DB_NAME]); - const hasLastConnection = columns.some(col => col.name === 'lastConnection'); - - if (!hasLastConnection) { - await connection.execute(`ALTER TABLE user_links ADD COLUMN lastConnection TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`); + if (columns.length === 0) { + await connection.execute(` + ALTER TABLE user_links + ADD COLUMN lastConnection TIMESTAMP NULL DEFAULT NULL + `); console.log('✅ Colonne lastConnection ajoutée'); } @@ -105,7 +107,7 @@ const createTables = async () => { player_id VARCHAR(50), palworld_username VARCHAR(100) NOT NULL, linked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - lastConnection TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + lastConnection TIMESTAMP NULL DEFAULT NULL, INDEX idx_discord_id (discord_id), INDEX idx_steam_id (steam_id), INDEX idx_player_id (player_id) @@ -214,12 +216,19 @@ const cleanExpiredCodes = async () => { ); }; -const updateLastConnection = async (steamId) => { +const updateLastConnection = async (userId) => { const connection = getConnection(); - await connection.execute( + + // Le userId est au format gdk_STEAMID, on extrait le Steam ID + const steamIdMatch = userId.match(/gdk_(\d+)/); + const steamId = steamIdMatch ? steamIdMatch[1] : userId; + + const [result] = await connection.execute( 'UPDATE user_links SET lastConnection = NOW() WHERE steam_id = ?', [steamId] ); + + return { success: true, changes: result.affectedRows }; }; module.exports = {