mod_storage_sql: Switch to MEDIUMTEXT for the 'value' column when using MySQL, as...
authorMatthew Wild <mwild1@gmail.com>
Thu, 2 Jun 2011 01:30:26 +0000 (02:30 +0100)
committerMatthew Wild <mwild1@gmail.com>
Thu, 2 Jun 2011 01:30:26 +0000 (02:30 +0100)
plugins/mod_storage_sql.lua

index b88efb6423b31ee274ab35efc09d4de4b72d9ac8..dd1487044113d11ae965759f1a9192d58a7f265a 100644 (file)
@@ -68,6 +68,8 @@ local function create_table()
        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);
@@ -91,6 +93,22 @@ local function create_table()
                        if not(ok and commit_ok) then
                                module:log("warn", "Failed to create index (%s), lookups may not be optimised", err or commit_err);
                        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
+                                               module:log("info", "Database table automatically upgraded");
+                                       end
+                               end
+                               repeat until not stmt:fetch();
+                       end
                end
        end
 end