upgrade_20221218.sql 24 KB

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