Software / code / prosody
Comparison
plugins/mod_storage_sql2.lua @ 5765:d854c17a45fd
mod_storage_sql2: Do an early return and drop an indentation level
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 02 Aug 2013 15:40:21 +0200 |
| parent | 5741:c7a664e496b3 |
| child | 5776:bd0ff8ae98a8 |
comparison
equal
deleted
inserted
replaced
| 5764:969e0a054795 | 5765:d854c17a45fd |
|---|---|
| 98 engine:transaction(function() | 98 engine:transaction(function() |
| 99 ProsodyArchiveTable:create(engine); | 99 ProsodyArchiveTable:create(engine); |
| 100 end); | 100 end); |
| 101 end | 101 end |
| 102 local function set_encoding() | 102 local function set_encoding() |
| 103 if params.driver ~= "SQLite3" then | 103 if params.driver == "SQLite3" then return end |
| 104 local set_names_query = "SET NAMES 'utf8';"; | 104 local set_names_query = "SET NAMES 'utf8';"; |
| 105 if params.driver == "MySQL" then | 105 if params.driver == "MySQL" then |
| 106 set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';"); | 106 set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';"); |
| 107 end | 107 end |
| 108 local success,err = engine:transaction(function() return engine:execute(set_names_query); end); | 108 local success,err = engine:transaction(function() return engine:execute(set_names_query); end); |
| 109 if not success then | |
| 110 module:log("error", "Failed to set database connection encoding to UTF8: %s", err); | |
| 111 return; | |
| 112 end | |
| 113 if params.driver == "MySQL" then | |
| 114 -- COMPAT w/pre-0.9: Upgrade tables to UTF-8 if not already | |
| 115 local check_encoding_query = "SELECT `COLUMN_NAME`,`COLUMN_TYPE` FROM `information_schema`.`columns` WHERE `TABLE_NAME`='prosody' AND ( `CHARACTER_SET_NAME`!='utf8' OR `COLLATION_NAME`!='utf8_bin' );"; | |
| 116 local success,err = engine:transaction(function() | |
| 117 local result = engine:execute(check_encoding_query); | |
| 118 local n_bad_columns = result:rowcount(); | |
| 119 if n_bad_columns > 0 then | |
| 120 module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns); | |
| 121 local fix_column_query1 = "ALTER TABLE `prosody` CHANGE `%s` `%s` BLOB;"; | |
| 122 local fix_column_query2 = "ALTER TABLE `prosody` CHANGE `%s` `%s` %s CHARACTER SET 'utf8' COLLATE 'utf8_bin';"; | |
| 123 for row in result:rows() do | |
| 124 local column_name, column_type = unpack(row); | |
| 125 engine:execute(fix_column_query1:format(column_name, column_name)); | |
| 126 engine:execute(fix_column_query2:format(column_name, column_name, column_type)); | |
| 127 end | |
| 128 module:log("info", "Database encoding upgrade complete!"); | |
| 129 end | |
| 130 end); | |
| 131 local success,err = engine:transaction(function() return engine:execute(check_encoding_query); end); | |
| 109 if not success then | 132 if not success then |
| 110 module:log("error", "Failed to set database connection encoding to UTF8: %s", err); | 133 module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error"); |
| 111 return; | |
| 112 end | |
| 113 if params.driver == "MySQL" then | |
| 114 -- COMPAT w/pre-0.9: Upgrade tables to UTF-8 if not already | |
| 115 local check_encoding_query = "SELECT `COLUMN_NAME`,`COLUMN_TYPE` FROM `information_schema`.`columns` WHERE `TABLE_NAME`='prosody' AND ( `CHARACTER_SET_NAME`!='utf8' OR `COLLATION_NAME`!='utf8_bin' );"; | |
| 116 local success,err = engine:transaction(function() | |
| 117 local result = engine:execute(check_encoding_query); | |
| 118 local n_bad_columns = result:rowcount(); | |
| 119 if n_bad_columns > 0 then | |
| 120 module:log("warn", "Found %d columns in prosody table requiring encoding change, updating now...", n_bad_columns); | |
| 121 local fix_column_query1 = "ALTER TABLE `prosody` CHANGE `%s` `%s` BLOB;"; | |
| 122 local fix_column_query2 = "ALTER TABLE `prosody` CHANGE `%s` `%s` %s CHARACTER SET 'utf8' COLLATE 'utf8_bin';"; | |
| 123 for row in result:rows() do | |
| 124 local column_name, column_type = unpack(row); | |
| 125 engine:execute(fix_column_query1:format(column_name, column_name)); | |
| 126 engine:execute(fix_column_query2:format(column_name, column_name, column_type)); | |
| 127 end | |
| 128 module:log("info", "Database encoding upgrade complete!"); | |
| 129 end | |
| 130 end); | |
| 131 local success,err = engine:transaction(function() return engine:execute(check_encoding_query); end); | |
| 132 if not success then | |
| 133 module:log("error", "Failed to check/upgrade database encoding: %s", err or "unknown error"); | |
| 134 end | |
| 135 end | 134 end |
| 136 end | 135 end |
| 137 end | 136 end |
| 138 | 137 |
| 139 do -- process options to get a db connection | 138 do -- process options to get a db connection |