Merge 0.9->0.10
[prosody.git] / tools / migration / migrator / prosody_sql.lua
index b1f836beaed1d78178e2f9e1128cfba93c660b53..180ae910725cf38d32852310bdd8875563da3afd 100644 (file)
@@ -21,8 +21,10 @@ local function create_table(connection, params)
        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);
        if stmt then
                local ok = stmt:execute();
@@ -40,6 +42,22 @@ local function create_table(connection, params)
                                ok, err = assert(stmt:execute());
                                commit_ok, commit_err = assert(connection:commit());
                        end
+               elseif params.driver == "MySQL" then -- 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
@@ -149,7 +167,11 @@ function writer(output, iter)
        assert(stmt:execute());
        local stmt = assert(dbh:prepare("DELETE FROM prosody"));
        assert(stmt:execute());
-       local insert = assert(dbh:prepare("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)"));
+       local insert_sql = "INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)";
+       if output.driver == "PostgreSQL" then
+               insert_sql = insert_sql:gsub("`", "\"");
+       end
+       local insert = assert(dbh:prepare(insert_sql));
 
        return function(item)
                if not item then assert(dbh:commit()) return dbh:close(); end -- end of input