Skip to content

Commit

Permalink
fix(storage/players): check if table exists before deleting from
Browse files Browse the repository at this point in the history
Fixes #398
  • Loading branch information
BerkieBb committed Apr 20, 2024
1 parent 73f52fb commit b510ca2
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions server/storage/players.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ local function fetchPlayerEntity(citizenId)
} or nil
end

---Checks if a table exists in the database
---@param tableName string
---@return boolean
local function doesTableExist(tableName)
local tbl = MySQL.single.await(('SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME = \'%s\' AND TABLE_SCHEMA in (SELECT DATABASE())'):format(tableName))
return tbl['COUNT(*)'] > 0
end

---deletes character data using the characterDataTables object in the config file
---@param citizenId string
---@return boolean success if operation is successful.
Expand All @@ -234,12 +242,16 @@ local function deletePlayer(citizenId)
local queries = {}

for tableName, columnName in pairs(characterDataTables) do
queries[#queries + 1] = {
query = query:format(tableName, columnName),
values = {
citizenId,
if doesTableExist(tableName) then
queries[#queries + 1] = {
query = query:format(tableName, columnName),
values = {
citizenId,
}
}
}
else
warn(('Table %s does not exist in database, please remove it from qbx_core/config/server.lua or create the table'):format(tableName))
end
end

local success = MySQL.transaction.await(queries)
Expand Down Expand Up @@ -346,6 +358,14 @@ RegisterCommand('convertjobs', function(source)
TriggerEvent('qbx_core:server:jobsconverted')
end, true)

CreateThread(function()
for tableName in pairs(characterDataTables) do
if not doesTableExist(tableName) then
warn(('Table \'%s\' does not exist in database, please remove it from qbx_core/config/server.lua or create the table'):format(tableName))
end
end
end)

return {
insertBan = insertBan,
fetchBan = fetchBan,
Expand Down

0 comments on commit b510ca2

Please sign in to comment.