Skip to content

Commit

Permalink
refactor(server): always create db table
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jul 14, 2023
1 parent 0c7cd35 commit fe58308
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -227,30 +227,21 @@ local function isAuthorised(playerId, door, lockpick)
return authorised
end

MySQL.query([[CREATE TABLE IF NOT EXISTS `ox_doorlock` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`data` LONGTEXT NOT NULL,
PRIMARY KEY (`id`) USING BTREE
)]])

MySQL.ready(function()
while Config.DoorList do Wait(100) end

local success, result = pcall(MySQL.query.await, 'SELECT id, name, data FROM ox_doorlock') --[[@as any]]

if not success then
-- because some people can't run sql files
success, result = pcall(MySQL.query, [[CREATE TABLE `ox_doorlock` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`data` LONGTEXT NOT NULL COLLATE 'utf8mb4_unicode_ci',
PRIMARY KEY (`id`) USING BTREE
) COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB; ]])
local response = MySQL.query.await('SELECT `id`, `name`, `data` FROM `ox_doorlock`')

if not success then
return error(result)
end

print("Created table 'ox_doorlock' in MySQL database.")
elseif result then
for i = 1, #result do
local door = result[i]
createDoor(door.id, json.decode(door.data), door.name)
end
for i = 1, #response do
local door = response[i]
createDoor(door.id, json.decode(door.data), door.name)
end

isLoaded = true
Expand Down

0 comments on commit fe58308

Please sign in to comment.