000000021.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package migrate
  2. import (
  3. "context"
  4. "golang-fave/engine/sqlw"
  5. )
  6. func Migrate_000000021(ctx context.Context, db *sqlw.DB, host string) error {
  7. // Drop foreign keys
  8. if _, err := db.Exec(ctx, `ALTER TABLE blog_cat_post_rel DROP FOREIGN KEY FK_blog_cat_post_rel_post_id;`); err != nil {
  9. return err
  10. }
  11. if _, err := db.Exec(ctx, `ALTER TABLE blog_cat_post_rel DROP FOREIGN KEY FK_blog_cat_post_rel_category_id;`); err != nil {
  12. return err
  13. }
  14. if _, err := db.Exec(ctx, `ALTER TABLE blog_cats DROP FOREIGN KEY FK_blog_cats_user;`); err != nil {
  15. return err
  16. }
  17. if _, err := db.Exec(ctx, `ALTER TABLE blog_posts DROP FOREIGN KEY FK_blog_posts_user;`); err != nil {
  18. return err
  19. }
  20. if _, err := db.Exec(ctx, `ALTER TABLE blog_posts DROP FOREIGN KEY FK_blog_posts_category;`); err != nil {
  21. return err
  22. }
  23. if _, err := db.Exec(ctx, `ALTER TABLE pages DROP FOREIGN KEY FK_pages_user;`); err != nil {
  24. return err
  25. }
  26. if _, err := db.Exec(ctx, `ALTER TABLE shop_cat_product_rel DROP FOREIGN KEY FK_shop_cat_product_rel_product_id;`); err != nil {
  27. return err
  28. }
  29. if _, err := db.Exec(ctx, `ALTER TABLE shop_cat_product_rel DROP FOREIGN KEY FK_shop_cat_product_rel_category_id;`); err != nil {
  30. return err
  31. }
  32. if _, err := db.Exec(ctx, `ALTER TABLE shop_cats DROP FOREIGN KEY FK_shop_cats_user;`); err != nil {
  33. return err
  34. }
  35. if _, err := db.Exec(ctx, `ALTER TABLE shop_filter_product_values DROP FOREIGN KEY FK_shop_filter_product_values_product_id;`); err != nil {
  36. return err
  37. }
  38. if _, err := db.Exec(ctx, `ALTER TABLE shop_filter_product_values DROP FOREIGN KEY FK_shop_filter_product_values_filter_value_id;`); err != nil {
  39. return err
  40. }
  41. if _, err := db.Exec(ctx, `ALTER TABLE shop_filters_values DROP FOREIGN KEY FK_shop_filters_values_filter_id;`); err != nil {
  42. return err
  43. }
  44. if _, err := db.Exec(ctx, `ALTER TABLE shop_orders DROP FOREIGN KEY FK_shop_orders_currency_id;`); err != nil {
  45. return err
  46. }
  47. if _, err := db.Exec(ctx, `ALTER TABLE shop_order_products DROP FOREIGN KEY FK_shop_order_products_order_id;`); err != nil {
  48. return err
  49. }
  50. if _, err := db.Exec(ctx, `ALTER TABLE shop_order_products DROP FOREIGN KEY FK_shop_order_products_product_id;`); err != nil {
  51. return err
  52. }
  53. if _, err := db.Exec(ctx, `ALTER TABLE shop_product_images DROP FOREIGN KEY FK_shop_product_images_product_id;`); err != nil {
  54. return err
  55. }
  56. if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_user;`); err != nil {
  57. return err
  58. }
  59. if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_currency;`); err != nil {
  60. return err
  61. }
  62. if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_category;`); err != nil {
  63. return err
  64. }
  65. if _, err := db.Exec(ctx, `ALTER TABLE shop_products DROP FOREIGN KEY FK_shop_products_parent_id;`); err != nil {
  66. return err
  67. }
  68. // Rename tables
  69. if _, err := db.Exec(ctx, `RENAME TABLE blog_cat_post_rel TO fave_blog_cat_post_rel;`); err != nil {
  70. return err
  71. }
  72. if _, err := db.Exec(ctx, `RENAME TABLE blog_cats TO fave_blog_cats;`); err != nil {
  73. return err
  74. }
  75. if _, err := db.Exec(ctx, `RENAME TABLE blog_posts TO fave_blog_posts;`); err != nil {
  76. return err
  77. }
  78. if _, err := db.Exec(ctx, `RENAME TABLE notify_mail TO fave_notify_mail;`); err != nil {
  79. return err
  80. }
  81. if _, err := db.Exec(ctx, `RENAME TABLE pages TO fave_pages;`); err != nil {
  82. return err
  83. }
  84. if _, err := db.Exec(ctx, `RENAME TABLE shop_cat_product_rel TO fave_shop_cat_product_rel;`); err != nil {
  85. return err
  86. }
  87. if _, err := db.Exec(ctx, `RENAME TABLE shop_cats TO fave_shop_cats;`); err != nil {
  88. return err
  89. }
  90. if _, err := db.Exec(ctx, `RENAME TABLE shop_currencies TO fave_shop_currencies;`); err != nil {
  91. return err
  92. }
  93. if _, err := db.Exec(ctx, `RENAME TABLE shop_filter_product_values TO fave_shop_filter_product_values;`); err != nil {
  94. return err
  95. }
  96. if _, err := db.Exec(ctx, `RENAME TABLE shop_filters_values TO fave_shop_filters_values;`); err != nil {
  97. return err
  98. }
  99. if _, err := db.Exec(ctx, `RENAME TABLE shop_filters TO fave_shop_filters;`); err != nil {
  100. return err
  101. }
  102. if _, err := db.Exec(ctx, `RENAME TABLE shop_order_products TO fave_shop_order_products;`); err != nil {
  103. return err
  104. }
  105. if _, err := db.Exec(ctx, `RENAME TABLE shop_orders TO fave_shop_orders;`); err != nil {
  106. return err
  107. }
  108. if _, err := db.Exec(ctx, `RENAME TABLE shop_product_images TO fave_shop_product_images;`); err != nil {
  109. return err
  110. }
  111. if _, err := db.Exec(ctx, `RENAME TABLE shop_products TO fave_shop_products;`); err != nil {
  112. return err
  113. }
  114. if _, err := db.Exec(ctx, `RENAME TABLE users TO fave_users;`); err != nil {
  115. return err
  116. }
  117. // Add constraints
  118. if _, err := db.Exec(
  119. ctx,
  120. `ALTER TABLE fave_blog_cat_post_rel ADD CONSTRAINT FK_blog_cat_post_rel_post_id
  121. FOREIGN KEY (post_id) REFERENCES fave_blog_posts (id) ON DELETE RESTRICT;`,
  122. ); err != nil {
  123. return err
  124. }
  125. if _, err := db.Exec(
  126. ctx,
  127. `ALTER TABLE fave_blog_cat_post_rel ADD CONSTRAINT FK_blog_cat_post_rel_category_id
  128. FOREIGN KEY (category_id) REFERENCES fave_blog_cats (id) ON DELETE RESTRICT;`,
  129. ); err != nil {
  130. return err
  131. }
  132. if _, err := db.Exec(
  133. ctx,
  134. `ALTER TABLE fave_blog_cats ADD CONSTRAINT FK_blog_cats_user
  135. FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
  136. ); err != nil {
  137. return err
  138. }
  139. if _, err := db.Exec(
  140. ctx,
  141. `ALTER TABLE fave_blog_posts ADD CONSTRAINT FK_blog_posts_user
  142. FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
  143. ); err != nil {
  144. return err
  145. }
  146. if _, err := db.Exec(
  147. ctx,
  148. `ALTER TABLE fave_blog_posts ADD CONSTRAINT FK_blog_posts_category
  149. FOREIGN KEY (category) REFERENCES fave_blog_cats (id) ON DELETE RESTRICT;`,
  150. ); err != nil {
  151. return err
  152. }
  153. if _, err := db.Exec(
  154. ctx,
  155. `ALTER TABLE fave_pages ADD CONSTRAINT FK_pages_user
  156. FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
  157. ); err != nil {
  158. return err
  159. }
  160. if _, err := db.Exec(
  161. ctx,
  162. `ALTER TABLE fave_shop_cat_product_rel ADD CONSTRAINT FK_shop_cat_product_rel_product_id
  163. FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
  164. ); err != nil {
  165. return err
  166. }
  167. if _, err := db.Exec(
  168. ctx,
  169. `ALTER TABLE fave_shop_cat_product_rel ADD CONSTRAINT FK_shop_cat_product_rel_category_id
  170. FOREIGN KEY (category_id) REFERENCES fave_shop_cats (id) ON DELETE RESTRICT;`,
  171. ); err != nil {
  172. return err
  173. }
  174. if _, err := db.Exec(
  175. ctx,
  176. `ALTER TABLE fave_shop_cats ADD CONSTRAINT FK_shop_cats_user
  177. FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
  178. ); err != nil {
  179. return err
  180. }
  181. if _, err := db.Exec(
  182. ctx,
  183. `ALTER TABLE fave_shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_product_id
  184. FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
  185. ); err != nil {
  186. return err
  187. }
  188. if _, err := db.Exec(
  189. ctx,
  190. `ALTER TABLE fave_shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_filter_value_id
  191. FOREIGN KEY (filter_value_id) REFERENCES fave_shop_filters_values (id) ON DELETE RESTRICT;`,
  192. ); err != nil {
  193. return err
  194. }
  195. if _, err := db.Exec(
  196. ctx,
  197. `ALTER TABLE fave_shop_filters_values ADD CONSTRAINT FK_shop_filters_values_filter_id
  198. FOREIGN KEY (filter_id) REFERENCES fave_shop_filters (id) ON DELETE RESTRICT;`,
  199. ); err != nil {
  200. return err
  201. }
  202. if _, err := db.Exec(
  203. ctx,
  204. `ALTER TABLE fave_shop_orders ADD CONSTRAINT FK_shop_orders_currency_id
  205. FOREIGN KEY (currency_id) REFERENCES fave_shop_currencies (id) ON DELETE RESTRICT;`,
  206. ); err != nil {
  207. return err
  208. }
  209. if _, err := db.Exec(
  210. ctx,
  211. `ALTER TABLE fave_shop_order_products ADD CONSTRAINT FK_shop_order_products_order_id
  212. FOREIGN KEY (order_id) REFERENCES fave_shop_orders (id) ON DELETE RESTRICT;`,
  213. ); err != nil {
  214. return err
  215. }
  216. if _, err := db.Exec(
  217. ctx,
  218. `ALTER TABLE fave_shop_order_products ADD CONSTRAINT FK_shop_order_products_product_id
  219. FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
  220. ); err != nil {
  221. return err
  222. }
  223. if _, err := db.Exec(
  224. ctx,
  225. `ALTER TABLE fave_shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id
  226. FOREIGN KEY (product_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
  227. ); err != nil {
  228. return err
  229. }
  230. if _, err := db.Exec(
  231. ctx,
  232. `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_user
  233. FOREIGN KEY (user) REFERENCES fave_users (id) ON DELETE RESTRICT;`,
  234. ); err != nil {
  235. return err
  236. }
  237. if _, err := db.Exec(
  238. ctx,
  239. `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_currency
  240. FOREIGN KEY (currency) REFERENCES fave_shop_currencies (id) ON DELETE RESTRICT;`,
  241. ); err != nil {
  242. return err
  243. }
  244. if _, err := db.Exec(
  245. ctx,
  246. `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_category
  247. FOREIGN KEY (category) REFERENCES fave_shop_cats (id) ON DELETE RESTRICT;`,
  248. ); err != nil {
  249. return err
  250. }
  251. if _, err := db.Exec(
  252. ctx,
  253. `ALTER TABLE fave_shop_products ADD CONSTRAINT FK_shop_products_parent_id
  254. FOREIGN KEY (parent_id) REFERENCES fave_shop_products (id) ON DELETE RESTRICT;`,
  255. ); err != nil {
  256. return err
  257. }
  258. return nil
  259. }