Merge pull request #57 from Valorith/copilot/sub-pr-55-again

Wrap SaveBuffs delete+replace in a transaction to prevent buff wipe on insert failure
This commit is contained in:
Vayle 2026-03-08 19:34:34 -04:00 committed by GitHub
commit 099da0e031
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2877,14 +2877,6 @@ void ZoneDatabase::UpdateAltCurrencyValue(uint32 char_id, uint32 currency_id, ui
void ZoneDatabase::SaveBuffs(Client *client)
{
CharacterBuffsRepository::DeleteWhere(
database,
fmt::format(
"`character_id` = {}",
client->CharacterID()
)
);
auto buffs = client->GetBuffs();
const int max_buff_slots = client->GetMaxBuffSlots();
@ -2933,9 +2925,20 @@ void ZoneDatabase::SaveBuffs(Client *client)
v.emplace_back(e);
}
database.TransactionBegin();
CharacterBuffsRepository::DeleteWhere(
database,
fmt::format(
"`character_id` = {}",
client->CharacterID()
)
);
if (!v.empty()) {
const auto saved_count = CharacterBuffsRepository::ReplaceMany(database, v);
if (saved_count < static_cast<int>(v.size())) {
database.TransactionRollback();
LogError(
"Failed to save all buffs for character [{}] [{}]. Expected at least [{}] rows saved, got [{}]. Verify the `character_buffs` schema is up to date.",
client->GetCleanName(),
@ -2943,8 +2946,20 @@ void ZoneDatabase::SaveBuffs(Client *client)
v.size(),
saved_count
);
return;
}
}
const auto commit_result = database.TransactionCommit();
if (!commit_result.Success()) {
database.TransactionRollback();
LogError(
"Failed to commit buff save transaction for character [{}] [{}]: {}",
client->GetCleanName(),
client->CharacterID(),
commit_result.ErrorMessage()
);
}
}
void ZoneDatabase::LoadBuffs(Client *client)