Comparison

plugins/mod_storage_sql.lua @ 4285:c806a599224a

mod_storage_sql: Switch to MEDIUMTEXT for the 'value' column when using MySQL, as it imposes a 64K limit otherwise, potentially truncating data. Automatically upgrades existing tables.
author Matthew Wild <mwild1@gmail.com>
date Thu, 02 Jun 2011 02:30:26 +0100
parent 4218:8a271b3fcaa7
child 4297:3421dfaa8188
comparison
equal deleted inserted replaced
4284:20979f124ad9 4285:c806a599224a
66 66
67 local function create_table() 67 local function create_table()
68 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; 68 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);";
69 if params.driver == "PostgreSQL" then 69 if params.driver == "PostgreSQL" then
70 create_sql = create_sql:gsub("`", "\""); 70 create_sql = create_sql:gsub("`", "\"");
71 elseif params.driver == "MySQL" then
72 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT");
71 end 73 end
72 74
73 local stmt = connection:prepare(create_sql); 75 local stmt = connection:prepare(create_sql);
74 if stmt then 76 if stmt then
75 local ok = stmt:execute(); 77 local ok = stmt:execute();
89 commit_ok, commit_err = connection:commit(); 91 commit_ok, commit_err = connection:commit();
90 end 92 end
91 if not(ok and commit_ok) then 93 if not(ok and commit_ok) then
92 module:log("warn", "Failed to create index (%s), lookups may not be optimised", err or commit_err); 94 module:log("warn", "Failed to create index (%s), lookups may not be optimised", err or commit_err);
93 end 95 end
96 else -- COMPAT: Upgrade tables from 0.8.0
97 -- Failed to create, but check existing MySQL table here
98 local stmt = connection:prepare("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'");
99 local ok = stmt:execute();
100 local commit_ok = connection:commit();
101 if ok and commit_ok then
102 if stmt:rowcount() > 0 then
103 local stmt = connection:prepare("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT");
104 local ok = stmt:execute();
105 local commit_ok = connection:commit();
106 if ok and commit_ok then
107 module:log("info", "Database table automatically upgraded");
108 end
109 end
110 repeat until not stmt:fetch();
111 end
94 end 112 end
95 end 113 end
96 end 114 end
97 115
98 do -- process options to get a db connection 116 do -- process options to get a db connection