mod_storage_sql2: Expose the unique key argument, allowing arbitrary ids. Conflicting items are removed.
authorKim Alvefur <zash@zash.se>
Wed, 25 Dec 2013 22:37:52 +0100
changeset 5969 0a3b8003ac9c
parent 5968 e1e9f1411442
child 5970 6a2c3293d4d7
mod_storage_sql2: Expose the unique key argument, allowing arbitrary ids. Conflicting items are removed.
plugins/mod_storage_sql2.lua
--- a/plugins/mod_storage_sql2.lua	Wed Dec 25 15:28:55 2013 +0100
+++ b/plugins/mod_storage_sql2.lua	Wed Dec 25 22:37:52 2013 +0100
@@ -216,11 +216,15 @@
 
 local archive_store = {}
 archive_store.__index = archive_store
-function archive_store:append(username, when, with, value)
+function archive_store:append(username, key, when, with, value)
+	if value == nil then -- COMPAT early versions
+		when, with, value, key = key, when, with, value
+	end
 	local user,store = username,self.store;
 	return engine:transaction(function()
-		local key = uuid.generate();
+		local key = key or uuid.generate();
 		local t, value = serialize(value);
+		engine:delete("DELETE FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=? AND KEY=?", host, user or "", store, key);
 		engine:insert("INSERT INTO `prosodyarchive` (`host`, `user`, `store`, `when`, `with`, `key`, `type`, `value`) VALUES (?,?,?,?,?,?,?,?)", host, user or "", store, when, with, key, t, value);
 		return key;
 	end);