Mercurial > prosody-hg
comparison plugins/mod_storage_sql.lua @ 4319:b46b766ce0af
Merge 0.8->trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 11 Jun 2011 02:17:11 +0100 |
| parents | 44b131d7041b |
| children | 2dbb3bf74090 |
comparison
equal
deleted
inserted
replaced
| 4316:2478698bdc52 | 4319:b46b766ce0af |
|---|---|
| 63 return connection; | 63 return connection; |
| 64 end | 64 end |
| 65 end | 65 end |
| 66 | 66 |
| 67 local function create_table() | 67 local function create_table() |
| 68 if not module:get_option("sql_manage_tables", true) then | |
| 69 return; | |
| 70 end | |
| 68 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; | 71 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; |
| 69 if params.driver == "PostgreSQL" then | 72 if params.driver == "PostgreSQL" then |
| 70 create_sql = create_sql:gsub("`", "\""); | 73 create_sql = create_sql:gsub("`", "\""); |
| 71 elseif params.driver == "MySQL" then | 74 elseif params.driver == "MySQL" then |
| 72 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT"); | 75 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT"); |
| 73 end | 76 end |
| 74 | 77 |
| 75 local stmt = connection:prepare(create_sql); | 78 local stmt, err = connection:prepare(create_sql); |
| 76 if stmt then | 79 if stmt then |
| 77 local ok = stmt:execute(); | 80 local ok = stmt:execute(); |
| 78 local commit_ok = connection:commit(); | 81 local commit_ok = connection:commit(); |
| 79 if ok and commit_ok then | 82 if ok and commit_ok then |
| 80 module:log("info", "Initialized new %s database with prosody table", params.driver); | 83 module:log("info", "Initialized new %s database with prosody table", params.driver); |
| 98 local stmt = connection:prepare("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'"); | 101 local stmt = connection:prepare("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'"); |
| 99 local ok = stmt:execute(); | 102 local ok = stmt:execute(); |
| 100 local commit_ok = connection:commit(); | 103 local commit_ok = connection:commit(); |
| 101 if ok and commit_ok then | 104 if ok and commit_ok then |
| 102 if stmt:rowcount() > 0 then | 105 if stmt:rowcount() > 0 then |
| 106 module:log("info", "Upgrading database schema..."); | |
| 103 local stmt = connection:prepare("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT"); | 107 local stmt = connection:prepare("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT"); |
| 104 local ok = stmt:execute(); | 108 local ok, err = stmt:execute(); |
| 105 local commit_ok = connection:commit(); | 109 local commit_ok = connection:commit(); |
| 106 if ok and commit_ok then | 110 if ok and commit_ok then |
| 107 module:log("info", "Database table automatically upgraded"); | 111 module:log("info", "Database table automatically upgraded"); |
| 112 else | |
| 113 module:log("error", "Failed to upgrade database schema (%s), please see " | |
| 114 .."http://prosody.im/doc/mysql for help", | |
| 115 err or "unknown error"); | |
| 108 end | 116 end |
| 109 end | 117 end |
| 110 repeat until not stmt:fetch(); | 118 repeat until not stmt:fetch(); |
| 111 else | 119 end |
| 112 module:log("error", "Failed to upgrade database schema, please see http://prosody.im/doc/mysql for help"); | 120 end |
| 113 end | 121 elseif params.driver ~= "SQLite3" then -- SQLite normally fails to prepare for existing table |
| 114 end | 122 module:log("warn", "Prosody was not able to automatically check/create the database table (%s), " |
| 123 .."see http://prosody.im/doc/modules/mod_storage_sql#table_management for help.", | |
| 124 err or "unknown error"); | |
| 115 end | 125 end |
| 116 end | 126 end |
| 117 | 127 |
| 118 do -- process options to get a db connection | 128 do -- process options to get a db connection |
| 119 local ok; | 129 local ok; |
