diff --git a/data-otservbr-global/migrations/46.lua b/data-otservbr-global/migrations/46.lua index 86a6d8ffec1..bdd0c851987 100644 --- a/data-otservbr-global/migrations/46.lua +++ b/data-otservbr-global/migrations/46.lua @@ -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 diff --git a/data-otservbr-global/migrations/47.lua b/data-otservbr-global/migrations/47.lua new file mode 100644 index 00000000000..86a6d8ffec1 --- /dev/null +++ b/data-otservbr-global/migrations/47.lua @@ -0,0 +1,3 @@ +function onUpdateDatabase() + return false -- true = There are others migrations file | false = this is the last migration file +end diff --git a/schema.sql b/schema.sql index d81c2914156..43180363baf 100644 --- a/schema.sql +++ b/schema.sql @@ -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, @@ -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;