From a2310b6d5e4e67e6b93aa30e3a9d54f246d87a04 Mon Sep 17 00:00:00 2001 From: Mitch Freeman <65987027+neckkola@users.noreply.github.com> Date: Mon, 11 Aug 2025 20:06:33 -0700 Subject: [PATCH] Updated inventory_snapshots to correct a test case. Working as intended now. Added sharedbank conversion to create unique item ids on world load. --- common/database.cpp | 35 +++++++++++++++++++ common/database.h | 1 + common/database/database_update_manifest.cpp | 4 ++- .../base_inventory_snapshots_repository.h | 12 ------- .../inventory_snapshots_repository.h | 6 ++-- world/world_boot.cpp | 1 + 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/common/database.cpp b/common/database.cpp index d60f3ce60..7e909ffa8 100644 --- a/common/database.cpp +++ b/common/database.cpp @@ -52,6 +52,7 @@ #include "../common/repositories/variables_repository.h" #include "../common/repositories/character_pet_name_repository.h" #include "../common/events/player_event_logs.h" +#include "../common/repositories/sharedbank_repository.h" // Disgrace: for windows compile #ifdef _WINDOWS @@ -2327,3 +2328,37 @@ void Database::ConvertInventoryToNewUniqueId() TransactionCommit(); LogInfo("Converted {} records", results.size()); } + +void Database::ConvertSharedbankToNewUniqueId() +{ + LogInfo("Converting shared bank entries with NULL item_unique_id"); + auto results = SharedbankRepository::GetWhere(*this, "`item_unique_id` IS NULL"); + + if (results.empty()) { + return; + } + + TransactionBegin(); + uint32 index = 0; + const uint32 batch_size = 1000; + std::vector queue{}; + queue.reserve(batch_size); + + for (auto &r: results) { + r.item_unique_id = EQ::UniqueHashGenerator::generate(); + queue.push_back(r); + index++; + if (index >= batch_size) { + SharedbankRepository::ReplaceMany(*this, queue); + index = 0; + queue.clear(); + } + } + + if (!queue.empty()) { + SharedbankRepository::ReplaceMany(*this, queue); + } + + TransactionCommit(); + LogInfo("Converted {} records", results.size()); +} \ No newline at end of file diff --git a/common/database.h b/common/database.h index accbb55ff..ee5e2c8a7 100644 --- a/common/database.h +++ b/common/database.h @@ -279,6 +279,7 @@ public: uint64_t GetNextTableId(const std::string& table_name); void ConvertInventoryToNewUniqueId(); + void ConvertSharedbankToNewUniqueId(); private: Mutex Mvarcache; diff --git a/common/database/database_update_manifest.cpp b/common/database/database_update_manifest.cpp index 904c67052..8a6b9a20b 100644 --- a/common/database/database_update_manifest.cpp +++ b/common/database/database_update_manifest.cpp @@ -7219,7 +7219,9 @@ ALTER TABLE `inventory_snapshots` ADD COLUMN `item_unique_id` VARCHAR(16) NULL DEFAULT NULL AFTER `ornament_hero_model`; ALTER TABLE `inventory_snapshots` - DROP PRIMARY KEY; + DROP PRIMARY KEY, + DROP COLUMN `guid`, + ADD COLUMN `item_unique_id` VARCHAR(16) NULL DEFAULT NULL AFTER `ornament_hero_model`; ALTER TABLE `inventory_snapshots` ADD PRIMARY KEY (`time_index`, `character_id`, `slot_id`) USING BTREE; diff --git a/common/repositories/base/base_inventory_snapshots_repository.h b/common/repositories/base/base_inventory_snapshots_repository.h index 0088a8784..a700c39f0 100644 --- a/common/repositories/base/base_inventory_snapshots_repository.h +++ b/common/repositories/base/base_inventory_snapshots_repository.h @@ -37,7 +37,6 @@ public: uint32_t ornament_idfile; int32_t ornament_hero_model; std::string item_unique_id; - uint64_t guid; }; static std::string PrimaryKey() @@ -66,7 +65,6 @@ public: "ornament_idfile", "ornament_hero_model", "item_unique_id", - "guid", }; } @@ -91,7 +89,6 @@ public: "ornament_idfile", "ornament_hero_model", "item_unique_id", - "guid", }; } @@ -150,7 +147,6 @@ public: e.ornament_idfile = 0; e.ornament_hero_model = 0; e.item_unique_id = ""; - e.guid = 0; return e; } @@ -205,7 +201,6 @@ public: e.ornament_idfile = row[15] ? static_cast(strtoul(row[15], nullptr, 10)) : 0; e.ornament_hero_model = row[16] ? static_cast(atoi(row[16])) : 0; e.item_unique_id = row[17] ? row[17] : ""; - e.guid = row[18] ? strtoull(row[18], nullptr, 10) : 0; return e; } @@ -257,7 +252,6 @@ public: v.push_back(columns[15] + " = " + std::to_string(e.ornament_idfile)); v.push_back(columns[16] + " = " + std::to_string(e.ornament_hero_model)); v.push_back(columns[17] + " = '" + Strings::Escape(e.item_unique_id) + "'"); - v.push_back(columns[18] + " = " + std::to_string(e.guid)); auto results = db.QueryDatabase( fmt::format( @@ -297,7 +291,6 @@ public: v.push_back(std::to_string(e.ornament_idfile)); v.push_back(std::to_string(e.ornament_hero_model)); v.push_back("'" + Strings::Escape(e.item_unique_id) + "'"); - v.push_back(std::to_string(e.guid)); auto results = db.QueryDatabase( fmt::format( @@ -345,7 +338,6 @@ public: v.push_back(std::to_string(e.ornament_idfile)); v.push_back(std::to_string(e.ornament_hero_model)); v.push_back("'" + Strings::Escape(e.item_unique_id) + "'"); - v.push_back(std::to_string(e.guid)); insert_chunks.push_back("(" + Strings::Implode(",", v) + ")"); } @@ -397,7 +389,6 @@ public: e.ornament_idfile = row[15] ? static_cast(strtoul(row[15], nullptr, 10)) : 0; e.ornament_hero_model = row[16] ? static_cast(atoi(row[16])) : 0; e.item_unique_id = row[17] ? row[17] : ""; - e.guid = row[18] ? strtoull(row[18], nullptr, 10) : 0; all_entries.push_back(e); } @@ -440,7 +431,6 @@ public: e.ornament_idfile = row[15] ? static_cast(strtoul(row[15], nullptr, 10)) : 0; e.ornament_hero_model = row[16] ? static_cast(atoi(row[16])) : 0; e.item_unique_id = row[17] ? row[17] : ""; - e.guid = row[18] ? strtoull(row[18], nullptr, 10) : 0; all_entries.push_back(e); } @@ -533,7 +523,6 @@ public: v.push_back(std::to_string(e.ornament_idfile)); v.push_back(std::to_string(e.ornament_hero_model)); v.push_back("'" + Strings::Escape(e.item_unique_id) + "'"); - v.push_back(std::to_string(e.guid)); auto results = db.QueryDatabase( fmt::format( @@ -574,7 +563,6 @@ public: v.push_back(std::to_string(e.ornament_idfile)); v.push_back(std::to_string(e.ornament_hero_model)); v.push_back("'" + Strings::Escape(e.item_unique_id) + "'"); - v.push_back(std::to_string(e.guid)); insert_chunks.push_back("(" + Strings::Implode(",", v) + ")"); } diff --git a/common/repositories/inventory_snapshots_repository.h b/common/repositories/inventory_snapshots_repository.h index 618ffd2fa..c05ac98bc 100644 --- a/common/repositories/inventory_snapshots_repository.h +++ b/common/repositories/inventory_snapshots_repository.h @@ -245,6 +245,7 @@ public: s.ornament_idfile = i.ornament_idfile; s.slot_id = i.slot_id; s.time_index = time_index; + s.item_unique_id = i.item_unique_id; queue.push_back(s); } @@ -264,8 +265,6 @@ public: static bool RestoreCharacterInvSnapshot(Database &db, uint32 character_id, uint32 timestamp) { - InventoryRepository::DeleteWhere(db, fmt::format("`character_id` = {}", character_id)); - auto snapshot = GetWhere(db, fmt::format("`character_id` = {} AND `time_index` = {}", character_id, timestamp)); if (snapshot.empty()) { LogError("The snapshot requested could not be found. Restore failed for character id [{}] @ [{}] failed", @@ -295,6 +294,7 @@ public: e.ornament_icon = i.ornament_icon; e.ornament_idfile = i.ornament_idfile; e.slot_id = i.slot_id; + e.item_unique_id = i.item_unique_id; queue.push_back(e); } @@ -303,6 +303,8 @@ public: return false; } + InventoryRepository::DeleteWhere(db, fmt::format("`character_id` = {}", character_id)); + if (!InventoryRepository::InsertMany(db, queue)) { LogError("A database error occurred. Restore failed for character id [{}] @ [{}] failed", character_id, timestamp); return false; diff --git a/world/world_boot.cpp b/world/world_boot.cpp index ee13ab7a3..3fc5c1ea7 100644 --- a/world/world_boot.cpp +++ b/world/world_boot.cpp @@ -296,6 +296,7 @@ bool WorldBoot::DatabaseLoadRoutines(int argc, char **argv) LogInfo("Clearing buyer table details"); database.ConvertInventoryToNewUniqueId(); + database.ConvertSharedbankToNewUniqueId(); if (RuleB(Bots, Enabled)) { LogInfo("Clearing [bot_pet_buffs] table of stale entries");