migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
authorMatthew Wild <mwild1@gmail.com>
Fri, 03 Jun 2011 00:57:25 +0100
changeset 4294 d2406f0ce8a5
parent 4293 419354c47c28
child 4295 da4844ab289f
child 4297 3421dfaa8188
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
tools/migration/migrator/prosody_sql.lua
--- a/tools/migration/migrator/prosody_sql.lua	Thu Jun 02 17:18:23 2011 +0100
+++ b/tools/migration/migrator/prosody_sql.lua	Fri Jun 03 00:57:25 2011 +0100
@@ -21,6 +21,8 @@
 	local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);";
 	if params.driver == "PostgreSQL" then
 		create_sql = create_sql:gsub("`", "\"");
+	elseif params.driver == "MySQL" then
+		create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT");
 	end
 	
 	local stmt = connection:prepare(create_sql);
@@ -40,6 +42,22 @@
 				ok, err = assert(stmt:execute());
 				commit_ok, commit_err = assert(connection:commit());
 			end
+		else -- COMPAT: Upgrade tables from 0.8.0
+			-- Failed to create, but check existing MySQL table here
+			local stmt = connection:prepare("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'");
+			local ok = stmt:execute();
+			local commit_ok = connection:commit();
+			if ok and commit_ok then
+				if stmt:rowcount() > 0 then
+					local stmt = connection:prepare("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT");
+					local ok = stmt:execute();
+					local commit_ok = connection:commit();
+					if ok and commit_ok then
+						print("Database table automatically upgraded");
+					end
+				end
+				repeat until not stmt:fetch();
+			end
 		end
 	end
 end