Skip to content

Commit

Permalink
SQL: Add migration to clear up old triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Jan 30, 2025
1 parent 62bb678 commit 04e43b7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 59 deletions.
10 changes: 9 additions & 1 deletion src/common/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,10 @@ namespace db
auto& lazyPreparedStatements = state.lazyPreparedStatements;

// If we don't have it, lazily make it
// cppcheck-suppress stlFindInsert
if (lazyPreparedStatements.find(rawQuery) == lazyPreparedStatements.end())
{
// cppcheck-suppress stlFindInsert
lazyPreparedStatements[rawQuery] = std::unique_ptr<sql::PreparedStatement>(state.connection->prepareStatement(rawQuery.c_str()));
}

Expand Down Expand Up @@ -583,10 +585,12 @@ namespace db
auto& lazyPreparedStatements = state.lazyPreparedStatements;

// If we don't have it, lazily make it
// cppcheck-suppress stlFindInsert
if (lazyPreparedStatements.find(rawQuery) == lazyPreparedStatements.end())
{
try
{
// cppcheck-suppress stlFindInsert
lazyPreparedStatements[rawQuery] = std::unique_ptr<sql::PreparedStatement>(state.connection->prepareStatement(rawQuery.c_str()));
}
catch (const std::exception& e)
Expand All @@ -597,9 +601,13 @@ namespace db
}
}

auto rowCountQuery = "SELECT ROW_COUNT() AS count";
const auto rowCountQuery = "SELECT ROW_COUNT() AS count";

// If we don't have it, lazily make it
// cppcheck-suppress stlFindInsert
if (lazyPreparedStatements.find(rowCountQuery) == lazyPreparedStatements.end())
{
// cppcheck-suppress stlFindInsert
lazyPreparedStatements[rowCountQuery] = std::unique_ptr<sql::PreparedStatement>(state.connection->prepareStatement(rowCountQuery));
}

Expand Down
76 changes: 18 additions & 58 deletions src/map/packet_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,6 @@ void PrintPacket(CBasicPacket& packet)
}
}

struct CharInfo
{
uint32 charId;
uint32 accId;
};

auto getCharInfoFromCharName(const std::string& name) -> xi::optional<CharInfo>
{
// NOTE: This is NOT case-sensitive.
const auto rset = db::preparedStmt("SELECT charid, accid FROM chars WHERE charname = ? LIMIT 1", name);
if (rset && rset->rowsCount() != 0 && rset->next())
{
return CharInfo{
.charId = rset->get<uint32>("charid"),
.accId = rset->get<uint32>("accid"),
};
}

return std::nullopt;
}

auto escapeString(const std::string& str) -> std::string
{
// TODO: Replace with db::escapeString
Expand Down Expand Up @@ -2208,69 +2187,50 @@ void SmallPacket0x03D(map_session_data_t* const PSession, CCharEntity* const PCh
{
TracyZoneScoped;

const auto blacklistedName = escapeString(asUntrustedCharacterName(data[0x08]));
const auto maybeCharInfo = getCharInfoFromCharName(blacklistedName);
const auto name = escapeString(asUntrustedCharacterName(data[0x08]));
const auto cmd = data.ref<uint8>(0x18);

// Could not look up potential blacklisted character info
if (!maybeCharInfo)
const auto sendFailPacket = [&]()
{
// Send failed
PChar->pushPacket<CBlacklistEditResponsePacket>(0, "", 0x02);
};

// Attempt to locate the character by their name
const auto rset = db::preparedStmt("SELECT charid, accid FROM chars WHERE charname = ? LIMIT 1", name);
if (!rset || rset->rowsCount() != 1 || !rset->next())
{
sendFailPacket();
return;
}
const auto info = *maybeCharInfo;

// NOTE: It is allowed on retail to blacklist yourself!
// : This will block you from sending /tell's to yourself...

const auto cmd = data.ref<uint8>(0x18);
uint32 charid = rset->get<uint32>("charid");
uint32 accid = rset->get<uint32>("accid");

// User is trying to add someone to their blacklist
if (cmd == 0x00)
{
if (blacklistutils::IsBlacklisted(PChar->id, info.charId))
{
// We cannot readd this person, fail to add
PChar->pushPacket<CBlacklistEditResponsePacket>(0, "", 0x02);
return;
}

// Attempt to add this person
if (blacklistutils::AddBlacklisted(PChar->id, info.charId))
if (blacklistutils::AddBlacklisted(PChar->id, charid))
{
PChar->pushPacket<CBlacklistEditResponsePacket>(info.accId, blacklistedName, cmd);
PChar->pushPacket<CBlacklistEditResponsePacket>(accid, name, cmd);
}
else
{
// Send failed
PChar->pushPacket<CBlacklistEditResponsePacket>(0, "", 0x02);
sendFailPacket();
}
}
else if (cmd == 0x01) // User is trying to remove someone from their blacklist
{
if (!blacklistutils::IsBlacklisted(PChar->id, info.charId))
{
// We cannot remove this person, fail to remove
PChar->pushPacket<CBlacklistEditResponsePacket>(0, "", 0x02);
return;
}

// Attempt to remove this person
if (blacklistutils::DeleteBlacklisted(PChar->id, info.charId))
if (blacklistutils::DeleteBlacklisted(PChar->id, charid))
{
PChar->pushPacket<CBlacklistEditResponsePacket>(info.accId, blacklistedName, cmd);
PChar->pushPacket<CBlacklistEditResponsePacket>(accid, name, cmd);
}
else
{
// Send failed
PChar->pushPacket<CBlacklistEditResponsePacket>(0, "", 0x02);
sendFailPacket();
}
}
else
{
// Send failed
PChar->pushPacket<CBlacklistEditResponsePacket>(0, "", 0x02);
}
}

/************************************************************************
Expand Down
24 changes: 24 additions & 0 deletions tools/migrations/045_drop_old_triggers_after_rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mariadb


def migration_name():
return "Dropping old ensure_ingredients_are_ordered triggers (new ones were added)"


def check_preconditions(cur):
return


def needs_to_run(cur):
cur.execute("SHOW TRIGGERS WHERE `Trigger` LIKE 'ensure_ingredients_are_ordered'")
if cur.fetchone():
return True
return False


def migrate(cur, db):
try:
cur.execute("DROP TRIGGER IF EXISTS ensure_ingredients_are_ordered;")
db.commit()
except mariadb.Error as err:
print("Something went wrong: {}".format(err))

0 comments on commit 04e43b7

Please sign in to comment.