comparison tools/migration/migrator/prosody_sql.lua @ 8064:ffb36d1ae23b

migrator/prosody_sql: Abort and demand database be upgraded if it needs to be (#635)
author Kim Alvefur <zash@zash.se>
date Fri, 07 Apr 2017 13:17:00 +0200
parents 605fa6bfafd1
children 36d9c1226fbc 5eec340c75fb
comparison
equal deleted inserted replaced
8063:605fa6bfafd1 8064:ffb36d1ae23b
82 end 82 end
83 end 83 end
84 return userdata; 84 return userdata;
85 end 85 end
86 86
87 local function needs_upgrade(engine, params)
88 if params.driver == "MySQL" then
89 local success = engine:transaction(function()
90 local result = engine:execute("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'");
91 assert(result:rowcount() == 0);
92
93 -- COMPAT w/pre-0.10: Upgrade table to UTF-8 if not already
94 local check_encoding_query = [[
95 SELECT `COLUMN_NAME`,`COLUMN_TYPE`,`TABLE_NAME`
96 FROM `information_schema`.`columns`
97 WHERE `TABLE_NAME` LIKE 'prosody%%' AND ( `CHARACTER_SET_NAME`!='%s' OR `COLLATION_NAME`!='%s_bin' );
98 ]];
99 check_encoding_query = check_encoding_query:format(engine.charset, engine.charset);
100 local result = engine:execute(check_encoding_query);
101 assert(result:rowcount() == 0)
102 end);
103 if not success then
104 -- Upgrade required
105 return true;
106 end
107 end
108 return false;
109 end
110
87 local function reader(input) 111 local function reader(input)
88 local engine = assert(sql:create_engine(input); 112 local engine = assert(sql:create_engine(input, function (engine) -- luacheck: ignore 431/engine
113 if needs_upgrade(engine, input) then
114 error("Old database format detected. Please run: prosodyctl mod_storage_sql upgrade");
115 end
116 end));
89 local keys = {"host", "user", "store", "key", "type", "value"}; 117 local keys = {"host", "user", "store", "key", "type", "value"};
90 assert(engine:connect()); 118 assert(engine:connect());
91 local f,s,val = assert(engine:select("SELECT `host`, `user`, `store`, `key`, `type`, `value` FROM `prosody`;")); 119 local f,s,val = assert(engine:select("SELECT `host`, `user`, `store`, `key`, `type`, `value` FROM `prosody`;"));
92 -- get SQL rows, sorted 120 -- get SQL rows, sorted
93 local iter = mtools.sorted { 121 local iter = mtools.sorted {
121 end; 149 end;
122 end 150 end
123 151
124 local function writer(output, iter) 152 local function writer(output, iter)
125 local engine = assert(sql:create_engine(output, function (engine) -- luacheck: ignore 431/engine 153 local engine = assert(sql:create_engine(output, function (engine) -- luacheck: ignore 431/engine
154 if needs_upgrade(engine, output) then
155 error("Old database format detected. Please run: prosodyctl mod_storage_sql upgrade");
156 end
126 create_table(engine); 157 create_table(engine);
127 end)); 158 end));
128 assert(engine:connect()); 159 assert(engine:connect());
129 assert(engine:delete("DELETE FROM prosody")); 160 assert(engine:delete("DELETE FROM prosody"));
130 local insert_sql = "INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)"; 161 local insert_sql = "INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)";