upgrade_20221218.sql 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /**
  2. ATTENTION!!!
  3. Please make sure to create a backup of your live data before running this update.
  4. This update is rather complicated and although I have tried my best I cannot guarantee that I did not make any mistake. [Lemongrass]
  5. We would also advise you to check the data in the temporary table after the insert statement and before the update statements.
  6. You could do so by running
  7. select * from `tmp_randomoptionfix` where `new_index` <> `old_index`;
  8. to see where changes happen and options will be moved.
  9. For executing this update your user needs create table and drop table permissions. Either run the update with another user [admin or root for example] or give your database user these permissions temporarily.
  10. Do not forget to remove these permissions again, as they are usually not necessary and it is rather dangerous, if someone gets access to your database user.
  11. Additionally if you have more than one storage table (see conf/inter_server.yml) make sure to also update those tables (see instructions at the bottom of this script).
  12. Comment the line below to really run the script, this is a safeguard that you confirm you have read and understood what was written above.
  13. **/
  14. cancel;
  15. -- Create a temporary table to store the data for the update
  16. create table `tmp_randomoptionfix` (
  17. `id` int(11) unsigned NOT NULL,
  18. `new_index` smallint(5) NOT NULL,
  19. `old_index` smallint(5) NOT NULL,
  20. `option_id` smallint(5) NOT NULL,
  21. `option_val` smallint(5) NOT NULL,
  22. `option_parm` tinyint(3) NOT NULL,
  23. PRIMARY KEY ( `id`, `old_index` )
  24. );
  25. -- Create auction data
  26. insert into `tmp_randomoptionfix` ( `id`, `new_index`, `old_index`, `option_id`, `option_val`, `option_parm` )
  27. select
  28. `id`,
  29. row_number() over( partition by `id` order by `old_index` asc ) - 1 as `new_index`,
  30. `old_index`,
  31. `option_id`,
  32. `option_val`,
  33. `option_parm`
  34. from (
  35. select * from (
  36. select
  37. `auction_id` as `id`,
  38. 0 as `old_index`,
  39. `option_id0` as `option_id`,
  40. `option_val0` as `option_val`,
  41. `option_parm0` as `option_parm`
  42. from `auction`
  43. where `option_id0` <> 0
  44. union
  45. select
  46. `auction_id` as `id`,
  47. 1 as `old_index`,
  48. `option_id1` as `option_id`,
  49. `option_val1` as `option_val`,
  50. `option_parm1` as `option_parm`
  51. from `auction`
  52. where `option_id1` <> 0
  53. union
  54. select
  55. `auction_id` as `id`,
  56. 2 as `old_index`,
  57. `option_id2` as `option_id`,
  58. `option_val2` as `option_val`,
  59. `option_parm2` as `option_parm`
  60. from `auction`
  61. where `option_id2` <> 0
  62. union
  63. select
  64. `auction_id` as `id`,
  65. 3 as `old_index`,
  66. `option_id3` as `option_id`,
  67. `option_val3` as `option_val`,
  68. `option_parm3` as `option_parm`
  69. from `auction`
  70. where `option_id3` <> 0
  71. union
  72. select
  73. `auction_id` as `id`,
  74. 4 as `old_index`,
  75. `option_id4` as `option_id`,
  76. `option_val4` as `option_val`,
  77. `option_parm4` as `option_parm`
  78. from `auction`
  79. where `option_id4` <> 0
  80. ) t2
  81. order by `id`, `old_index`
  82. ) t ;
  83. -- Fix option 0
  84. update `auction`
  85. left join `tmp_randomoptionfix`
  86. on `auction`.`auction_id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 0
  87. set
  88. `option_id0` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  89. `option_val0` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  90. `option_parm0` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  91. ;
  92. -- Fix option 1
  93. update `auction`
  94. left join `tmp_randomoptionfix`
  95. on `auction`.`auction_id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 1
  96. set
  97. `option_id1` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  98. `option_val1` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  99. `option_parm1` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  100. ;
  101. -- Fix option 2
  102. update `auction`
  103. left join `tmp_randomoptionfix`
  104. on `auction`.`auction_id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 2
  105. set
  106. `option_id2` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  107. `option_val2` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  108. `option_parm2` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  109. ;
  110. -- Fix option 3
  111. update `auction`
  112. left join `tmp_randomoptionfix`
  113. on `auction`.`auction_id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 3
  114. set
  115. `option_id3` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  116. `option_val3` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  117. `option_parm3` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  118. ;
  119. -- Fix option 4
  120. update `auction`
  121. left join `tmp_randomoptionfix`
  122. on `auction`.`auction_id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 4
  123. set
  124. `option_id4` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  125. `option_val4` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  126. `option_parm4` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  127. ;
  128. -- Delete the data to have a clean table for the next run
  129. delete from `tmp_randomoptionfix`;
  130. -- Create cart_inventory data
  131. insert into `tmp_randomoptionfix` ( `id`, `new_index`, `old_index`, `option_id`, `option_val`, `option_parm` )
  132. select
  133. `id`,
  134. row_number() over( partition by `id` order by `old_index` asc ) - 1 as `new_index`,
  135. `old_index`,
  136. `option_id`,
  137. `option_val`,
  138. `option_parm`
  139. from (
  140. select * from (
  141. select
  142. `id`,
  143. 0 as `old_index`,
  144. `option_id0` as `option_id`,
  145. `option_val0` as `option_val`,
  146. `option_parm0` as `option_parm`
  147. from `cart_inventory`
  148. where `option_id0` <> 0
  149. union
  150. select
  151. `id`,
  152. 1 as `old_index`,
  153. `option_id1` as `option_id`,
  154. `option_val1` as `option_val`,
  155. `option_parm1` as `option_parm`
  156. from `cart_inventory`
  157. where `option_id1` <> 0
  158. union
  159. select
  160. `id`,
  161. 2 as `old_index`,
  162. `option_id2` as `option_id`,
  163. `option_val2` as `option_val`,
  164. `option_parm2` as `option_parm`
  165. from `cart_inventory`
  166. where `option_id2` <> 0
  167. union
  168. select
  169. `id`,
  170. 3 as `old_index`,
  171. `option_id3` as `option_id`,
  172. `option_val3` as `option_val`,
  173. `option_parm3` as `option_parm`
  174. from `cart_inventory`
  175. where `option_id3` <> 0
  176. union
  177. select
  178. `id`,
  179. 4 as `old_index`,
  180. `option_id4` as `option_id`,
  181. `option_val4` as `option_val`,
  182. `option_parm4` as `option_parm`
  183. from `cart_inventory`
  184. where `option_id4` <> 0
  185. ) t2
  186. order by `id`, `old_index`
  187. ) t ;
  188. -- Fix option 0
  189. update `cart_inventory`
  190. left join `tmp_randomoptionfix`
  191. on `cart_inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 0
  192. set
  193. `option_id0` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  194. `option_val0` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  195. `option_parm0` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  196. ;
  197. -- Fix option 1
  198. update `cart_inventory`
  199. left join `tmp_randomoptionfix`
  200. on `cart_inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 1
  201. set
  202. `option_id1` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  203. `option_val1` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  204. `option_parm1` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  205. ;
  206. -- Fix option 2
  207. update `cart_inventory`
  208. left join `tmp_randomoptionfix`
  209. on `cart_inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 2
  210. set
  211. `option_id2` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  212. `option_val2` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  213. `option_parm2` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  214. ;
  215. -- Fix option 3
  216. update `cart_inventory`
  217. left join `tmp_randomoptionfix`
  218. on `cart_inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 3
  219. set
  220. `option_id3` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  221. `option_val3` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  222. `option_parm3` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  223. ;
  224. -- Fix option 4
  225. update `cart_inventory`
  226. left join `tmp_randomoptionfix`
  227. on `cart_inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 4
  228. set
  229. `option_id4` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  230. `option_val4` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  231. `option_parm4` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  232. ;
  233. -- Delete the data to have a clean table for the next run
  234. delete from `tmp_randomoptionfix`;
  235. -- Create guild_storage data
  236. insert into `tmp_randomoptionfix` ( `id`, `new_index`, `old_index`, `option_id`, `option_val`, `option_parm` )
  237. select
  238. `id`,
  239. row_number() over( partition by `id` order by `old_index` asc ) - 1 as `new_index`,
  240. `old_index`,
  241. `option_id`,
  242. `option_val`,
  243. `option_parm`
  244. from (
  245. select * from (
  246. select
  247. `id`,
  248. 0 as `old_index`,
  249. `option_id0` as `option_id`,
  250. `option_val0` as `option_val`,
  251. `option_parm0` as `option_parm`
  252. from `guild_storage`
  253. where `option_id0` <> 0
  254. union
  255. select
  256. `id`,
  257. 1 as `old_index`,
  258. `option_id1` as `option_id`,
  259. `option_val1` as `option_val`,
  260. `option_parm1` as `option_parm`
  261. from `guild_storage`
  262. where `option_id1` <> 0
  263. union
  264. select
  265. `id`,
  266. 2 as `old_index`,
  267. `option_id2` as `option_id`,
  268. `option_val2` as `option_val`,
  269. `option_parm2` as `option_parm`
  270. from `guild_storage`
  271. where `option_id2` <> 0
  272. union
  273. select
  274. `id`,
  275. 3 as `old_index`,
  276. `option_id3` as `option_id`,
  277. `option_val3` as `option_val`,
  278. `option_parm3` as `option_parm`
  279. from `guild_storage`
  280. where `option_id3` <> 0
  281. union
  282. select
  283. `id`,
  284. 4 as `old_index`,
  285. `option_id4` as `option_id`,
  286. `option_val4` as `option_val`,
  287. `option_parm4` as `option_parm`
  288. from `guild_storage`
  289. where `option_id4` <> 0
  290. ) t2
  291. order by `id`, `old_index`
  292. ) t ;
  293. -- Fix option 0
  294. update `guild_storage`
  295. left join `tmp_randomoptionfix`
  296. on `guild_storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 0
  297. set
  298. `option_id0` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  299. `option_val0` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  300. `option_parm0` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  301. ;
  302. -- Fix option 1
  303. update `guild_storage`
  304. left join `tmp_randomoptionfix`
  305. on `guild_storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 1
  306. set
  307. `option_id1` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  308. `option_val1` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  309. `option_parm1` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  310. ;
  311. -- Fix option 2
  312. update `guild_storage`
  313. left join `tmp_randomoptionfix`
  314. on `guild_storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 2
  315. set
  316. `option_id2` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  317. `option_val2` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  318. `option_parm2` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  319. ;
  320. -- Fix option 3
  321. update `guild_storage`
  322. left join `tmp_randomoptionfix`
  323. on `guild_storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 3
  324. set
  325. `option_id3` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  326. `option_val3` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  327. `option_parm3` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  328. ;
  329. -- Fix option 4
  330. update `guild_storage`
  331. left join `tmp_randomoptionfix`
  332. on `guild_storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 4
  333. set
  334. `option_id4` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  335. `option_val4` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  336. `option_parm4` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  337. ;
  338. -- Delete the data to have a clean table for the next run
  339. delete from `tmp_randomoptionfix`;
  340. -- Create inventory data
  341. insert into `tmp_randomoptionfix` ( `id`, `new_index`, `old_index`, `option_id`, `option_val`, `option_parm` )
  342. select
  343. `id`,
  344. row_number() over( partition by `id` order by `old_index` asc ) - 1 as `new_index`,
  345. `old_index`,
  346. `option_id`,
  347. `option_val`,
  348. `option_parm`
  349. from (
  350. select * from (
  351. select
  352. `id`,
  353. 0 as `old_index`,
  354. `option_id0` as `option_id`,
  355. `option_val0` as `option_val`,
  356. `option_parm0` as `option_parm`
  357. from `inventory`
  358. where `option_id0` <> 0
  359. union
  360. select
  361. `id`,
  362. 1 as `old_index`,
  363. `option_id1` as `option_id`,
  364. `option_val1` as `option_val`,
  365. `option_parm1` as `option_parm`
  366. from `inventory`
  367. where `option_id1` <> 0
  368. union
  369. select
  370. `id`,
  371. 2 as `old_index`,
  372. `option_id2` as `option_id`,
  373. `option_val2` as `option_val`,
  374. `option_parm2` as `option_parm`
  375. from `inventory`
  376. where `option_id2` <> 0
  377. union
  378. select
  379. `id`,
  380. 3 as `old_index`,
  381. `option_id3` as `option_id`,
  382. `option_val3` as `option_val`,
  383. `option_parm3` as `option_parm`
  384. from `inventory`
  385. where `option_id3` <> 0
  386. union
  387. select
  388. `id`,
  389. 4 as `old_index`,
  390. `option_id4` as `option_id`,
  391. `option_val4` as `option_val`,
  392. `option_parm4` as `option_parm`
  393. from `inventory`
  394. where `option_id4` <> 0
  395. ) t2
  396. order by `id`, `old_index`
  397. ) t ;
  398. -- Fix option 0
  399. update `inventory`
  400. left join `tmp_randomoptionfix`
  401. on `inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 0
  402. set
  403. `option_id0` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  404. `option_val0` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  405. `option_parm0` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  406. ;
  407. -- Fix option 1
  408. update `inventory`
  409. left join `tmp_randomoptionfix`
  410. on `inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 1
  411. set
  412. `option_id1` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  413. `option_val1` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  414. `option_parm1` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  415. ;
  416. -- Fix option 2
  417. update `inventory`
  418. left join `tmp_randomoptionfix`
  419. on `inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 2
  420. set
  421. `option_id2` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  422. `option_val2` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  423. `option_parm2` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  424. ;
  425. -- Fix option 3
  426. update `inventory`
  427. left join `tmp_randomoptionfix`
  428. on `inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 3
  429. set
  430. `option_id3` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  431. `option_val3` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  432. `option_parm3` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  433. ;
  434. -- Fix option 4
  435. update `inventory`
  436. left join `tmp_randomoptionfix`
  437. on `inventory`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 4
  438. set
  439. `option_id4` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  440. `option_val4` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  441. `option_parm4` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  442. ;
  443. -- Delete the data to have a clean table for the next run
  444. delete from `tmp_randomoptionfix`;
  445. -- Create mail_attachments data
  446. insert into `tmp_randomoptionfix` ( `id`, `new_index`, `old_index`, `option_id`, `option_val`, `option_parm` )
  447. select
  448. `id`,
  449. row_number() over( partition by `id` order by `old_index` asc ) - 1 as `new_index`,
  450. `old_index`,
  451. `option_id`,
  452. `option_val`,
  453. `option_parm`
  454. from (
  455. select * from (
  456. select
  457. `id`,
  458. 0 as `old_index`,
  459. `option_id0` as `option_id`,
  460. `option_val0` as `option_val`,
  461. `option_parm0` as `option_parm`
  462. from `mail_attachments`
  463. where `option_id0` <> 0
  464. union
  465. select
  466. `id`,
  467. 1 as `old_index`,
  468. `option_id1` as `option_id`,
  469. `option_val1` as `option_val`,
  470. `option_parm1` as `option_parm`
  471. from `mail_attachments`
  472. where `option_id1` <> 0
  473. union
  474. select
  475. `id`,
  476. 2 as `old_index`,
  477. `option_id2` as `option_id`,
  478. `option_val2` as `option_val`,
  479. `option_parm2` as `option_parm`
  480. from `mail_attachments`
  481. where `option_id2` <> 0
  482. union
  483. select
  484. `id`,
  485. 3 as `old_index`,
  486. `option_id3` as `option_id`,
  487. `option_val3` as `option_val`,
  488. `option_parm3` as `option_parm`
  489. from `mail_attachments`
  490. where `option_id3` <> 0
  491. union
  492. select
  493. `id`,
  494. 4 as `old_index`,
  495. `option_id4` as `option_id`,
  496. `option_val4` as `option_val`,
  497. `option_parm4` as `option_parm`
  498. from `mail_attachments`
  499. where `option_id4` <> 0
  500. ) t2
  501. order by `id`, `old_index`
  502. ) t ;
  503. -- Fix option 0
  504. update `mail_attachments`
  505. left join `tmp_randomoptionfix`
  506. on `mail_attachments`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 0
  507. set
  508. `option_id0` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  509. `option_val0` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  510. `option_parm0` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  511. ;
  512. -- Fix option 1
  513. update `mail_attachments`
  514. left join `tmp_randomoptionfix`
  515. on `mail_attachments`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 1
  516. set
  517. `option_id1` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  518. `option_val1` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  519. `option_parm1` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  520. ;
  521. -- Fix option 2
  522. update `mail_attachments`
  523. left join `tmp_randomoptionfix`
  524. on `mail_attachments`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 2
  525. set
  526. `option_id2` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  527. `option_val2` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  528. `option_parm2` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  529. ;
  530. -- Fix option 3
  531. update `mail_attachments`
  532. left join `tmp_randomoptionfix`
  533. on `mail_attachments`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 3
  534. set
  535. `option_id3` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  536. `option_val3` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  537. `option_parm3` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  538. ;
  539. -- Fix option 4
  540. update `mail_attachments`
  541. left join `tmp_randomoptionfix`
  542. on `mail_attachments`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 4
  543. set
  544. `option_id4` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  545. `option_val4` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  546. `option_parm4` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  547. ;
  548. -- Delete the data to have a clean table for the next run
  549. delete from `tmp_randomoptionfix`;
  550. -- Create storage data
  551. insert into `tmp_randomoptionfix` ( `id`, `new_index`, `old_index`, `option_id`, `option_val`, `option_parm` )
  552. select
  553. `id`,
  554. row_number() over( partition by `id` order by `old_index` asc ) - 1 as `new_index`,
  555. `old_index`,
  556. `option_id`,
  557. `option_val`,
  558. `option_parm`
  559. from (
  560. select * from (
  561. select
  562. `id`,
  563. 0 as `old_index`,
  564. `option_id0` as `option_id`,
  565. `option_val0` as `option_val`,
  566. `option_parm0` as `option_parm`
  567. from `storage`
  568. where `option_id0` <> 0
  569. union
  570. select
  571. `id`,
  572. 1 as `old_index`,
  573. `option_id1` as `option_id`,
  574. `option_val1` as `option_val`,
  575. `option_parm1` as `option_parm`
  576. from `storage`
  577. where `option_id1` <> 0
  578. union
  579. select
  580. `id`,
  581. 2 as `old_index`,
  582. `option_id2` as `option_id`,
  583. `option_val2` as `option_val`,
  584. `option_parm2` as `option_parm`
  585. from `storage`
  586. where `option_id2` <> 0
  587. union
  588. select
  589. `id`,
  590. 3 as `old_index`,
  591. `option_id3` as `option_id`,
  592. `option_val3` as `option_val`,
  593. `option_parm3` as `option_parm`
  594. from `storage`
  595. where `option_id3` <> 0
  596. union
  597. select
  598. `id`,
  599. 4 as `old_index`,
  600. `option_id4` as `option_id`,
  601. `option_val4` as `option_val`,
  602. `option_parm4` as `option_parm`
  603. from `storage`
  604. where `option_id4` <> 0
  605. ) t2
  606. order by `id`, `old_index`
  607. ) t ;
  608. -- Fix option 0
  609. update `storage`
  610. left join `tmp_randomoptionfix`
  611. on `storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 0
  612. set
  613. `option_id0` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  614. `option_val0` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  615. `option_parm0` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  616. ;
  617. -- Fix option 1
  618. update `storage`
  619. left join `tmp_randomoptionfix`
  620. on `storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 1
  621. set
  622. `option_id1` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  623. `option_val1` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  624. `option_parm1` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  625. ;
  626. -- Fix option 2
  627. update `storage`
  628. left join `tmp_randomoptionfix`
  629. on `storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 2
  630. set
  631. `option_id2` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  632. `option_val2` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  633. `option_parm2` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  634. ;
  635. -- Fix option 3
  636. update `storage`
  637. left join `tmp_randomoptionfix`
  638. on `storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 3
  639. set
  640. `option_id3` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  641. `option_val3` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  642. `option_parm3` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  643. ;
  644. -- Fix option 4
  645. update `storage`
  646. left join `tmp_randomoptionfix`
  647. on `storage`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 4
  648. set
  649. `option_id4` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  650. `option_val4` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  651. `option_parm4` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  652. ;
  653. -- Delete the data to have a clean table for the next run
  654. delete from `tmp_randomoptionfix`;
  655. -- Repeat this for any other storage tables that you have created (for example VIP)
  656. -- To do this copy paste the code below and replace ${other_storage} with the name of your table
  657. /**
  658. -- Create ${other_storage} data
  659. insert into `tmp_randomoptionfix` ( `id`, `new_index`, `old_index`, `option_id`, `option_val`, `option_parm` )
  660. select
  661. `id`,
  662. row_number() over( partition by `id` order by `old_index` asc ) - 1 as `new_index`,
  663. `old_index`,
  664. `option_id`,
  665. `option_val`,
  666. `option_parm`
  667. from (
  668. select * from (
  669. select
  670. `id`,
  671. 0 as `old_index`,
  672. `option_id0` as `option_id`,
  673. `option_val0` as `option_val`,
  674. `option_parm0` as `option_parm`
  675. from `${other_storage}`
  676. where `option_id0` <> 0
  677. union
  678. select
  679. `id`,
  680. 1 as `old_index`,
  681. `option_id1` as `option_id`,
  682. `option_val1` as `option_val`,
  683. `option_parm1` as `option_parm`
  684. from `${other_storage}`
  685. where `option_id1` <> 0
  686. union
  687. select
  688. `id`,
  689. 2 as `old_index`,
  690. `option_id2` as `option_id`,
  691. `option_val2` as `option_val`,
  692. `option_parm2` as `option_parm`
  693. from `${other_storage}`
  694. where `option_id2` <> 0
  695. union
  696. select
  697. `id`,
  698. 3 as `old_index`,
  699. `option_id3` as `option_id`,
  700. `option_val3` as `option_val`,
  701. `option_parm3` as `option_parm`
  702. from `${other_storage}`
  703. where `option_id3` <> 0
  704. union
  705. select
  706. `id`,
  707. 4 as `old_index`,
  708. `option_id4` as `option_id`,
  709. `option_val4` as `option_val`,
  710. `option_parm4` as `option_parm`
  711. from `${other_storage}`
  712. where `option_id4` <> 0
  713. ) t2
  714. order by `id`, `old_index`
  715. ) t ;
  716. -- Fix option 0
  717. update `${other_storage}`
  718. left join `tmp_randomoptionfix`
  719. on `${other_storage}`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 0
  720. set
  721. `option_id0` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  722. `option_val0` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  723. `option_parm0` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  724. ;
  725. -- Fix option 1
  726. update `${other_storage}`
  727. left join `tmp_randomoptionfix`
  728. on `${other_storage}`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 1
  729. set
  730. `option_id1` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  731. `option_val1` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  732. `option_parm1` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  733. ;
  734. -- Fix option 2
  735. update `${other_storage}`
  736. left join `tmp_randomoptionfix`
  737. on `${other_storage}`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 2
  738. set
  739. `option_id2` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  740. `option_val2` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  741. `option_parm2` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  742. ;
  743. -- Fix option 3
  744. update `${other_storage}`
  745. left join `tmp_randomoptionfix`
  746. on `${other_storage}`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 3
  747. set
  748. `option_id3` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  749. `option_val3` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  750. `option_parm3` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  751. ;
  752. -- Fix option 4
  753. update `${other_storage}`
  754. left join `tmp_randomoptionfix`
  755. on `${other_storage}`.`id` = `tmp_randomoptionfix`.`id` and `tmp_randomoptionfix`.`new_index` = 4
  756. set
  757. `option_id4` = coalesce( `tmp_randomoptionfix`.`option_id`, 0 ),
  758. `option_val4` = coalesce( `tmp_randomoptionfix`.`option_val`, 0 ),
  759. `option_parm4` = coalesce( `tmp_randomoptionfix`.`option_parm`, 0 )
  760. ;
  761. -- Delete the data to have a clean table for the next run
  762. delete from `tmp_randomoptionfix`;
  763. **/
  764. drop table `tmp_randomoptionfix`;