Skip to content

Commit

Permalink
improve: change forge_history for set "unique key" done_at
Browse files Browse the repository at this point in the history
Converted to miliseconds and set the "done_at" to unique key.
  • Loading branch information
dudantas committed Nov 2, 2024
1 parent 2cef00f commit 67c0784
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
30 changes: 29 additions & 1 deletion data-otservbr-global/migrations/46.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
function onUpdateDatabase()
return false -- true = There are others migrations file | false = this is the last migration file
logger.info("Updating database to version 52 (player forge history unique done_at)")

db.query([[
UPDATE forge_history
SET done_at = done_at * 1000
WHERE done_at < 1000000000000;
]])

db.query([[
UPDATE forge_history AS f1
JOIN (
SELECT
id,
done_at,
ROW_NUMBER() OVER (PARTITION BY done_at ORDER BY id) AS row_num
FROM forge_history
) AS duplicates ON f1.id = duplicates.id
SET f1.done_at = f1.done_at + (duplicates.row_num * 1)
WHERE duplicates.row_num > 1;
]])

local success = db.query("ALTER TABLE forge_history ADD UNIQUE KEY unique_done_at (done_at);")

if not success then
logger.error("Failed to add unique key to 'done_at'.")
return false
end

return true
end
3 changes: 3 additions & 0 deletions data-otservbr-global/migrations/47.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function onUpdateDatabase()
return false -- true = There are others migrations file | false = this is the last migration file
end
5 changes: 3 additions & 2 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ CREATE TABLE IF NOT EXISTS `daily_reward_history` (
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Tabble Structure `forge_history`
-- Table Structure `forge_history`
CREATE TABLE IF NOT EXISTS `forge_history` (
`id` int NOT NULL AUTO_INCREMENT,
`player_id` int NOT NULL,
Expand All @@ -314,7 +314,8 @@ CREATE TABLE IF NOT EXISTS `forge_history` (
`done_at_date` datetime DEFAULT NOW(),
`cost` bigint UNSIGNED NOT NULL DEFAULT '0',
`gained` bigint UNSIGNED NOT NULL DEFAULT '0',
CONSTRAINT `forge_history_pk` PRIMARY KEY (`id`),
PRIMARY KEY (`id`),
UNIQUE KEY `unique_done_at` (`done_at`),
FOREIGN KEY (`player_id`) REFERENCES `players` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down

0 comments on commit 67c0784

Please sign in to comment.