000000003.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package migrate
  2. import (
  3. "context"
  4. "golang-fave/engine/sqlw"
  5. "golang-fave/engine/utils"
  6. )
  7. func Migrate_000000003(ctx context.Context, db *sqlw.DB, host string) error {
  8. // Remove blog indexes
  9. if _, err := db.Exec(ctx, `DROP INDEX post_id ON blog_cat_post_rel`); err != nil {
  10. return err
  11. }
  12. if _, err := db.Exec(ctx, `DROP INDEX category_id ON blog_cat_post_rel`); err != nil {
  13. return err
  14. }
  15. // Table: shop_cat_product_rel
  16. if _, err := db.Exec(
  17. ctx,
  18. `CREATE TABLE shop_cat_product_rel (
  19. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  20. product_id int(11) NOT NULL COMMENT 'Product id',
  21. category_id int(11) NOT NULL COMMENT 'Category id',
  22. PRIMARY KEY (id)
  23. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  24. ); err != nil {
  25. return err
  26. }
  27. // Table: shop_cats
  28. if _, err := db.Exec(
  29. ctx,
  30. `CREATE TABLE shop_cats (
  31. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  32. user int(11) NOT NULL COMMENT 'User id',
  33. name varchar(255) NOT NULL COMMENT 'Category name',
  34. alias varchar(255) NOT NULL COMMENT 'Category alias',
  35. lft int(11) NOT NULL COMMENT 'For nested set model',
  36. rgt int(11) NOT NULL COMMENT 'For nested set model',
  37. PRIMARY KEY (id)
  38. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  39. ); err != nil {
  40. return err
  41. }
  42. // Table: shop_currencies
  43. if _, err := db.Exec(
  44. ctx,
  45. `CREATE TABLE shop_currencies (
  46. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  47. name varchar(255) NOT NULL COMMENT 'Currency name',
  48. coefficient float(8,4) NOT NULL DEFAULT '1.0000' COMMENT 'Currency coefficient',
  49. code varchar(10) NOT NULL COMMENT 'Currency code',
  50. symbol varchar(5) NOT NULL COMMENT 'Currency symbol',
  51. PRIMARY KEY (id)
  52. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  53. ); err != nil {
  54. return err
  55. }
  56. // Table: shop_filter_product_values
  57. if _, err := db.Exec(
  58. ctx,
  59. `CREATE TABLE shop_filter_product_values (
  60. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  61. product_id int(11) NOT NULL COMMENT 'Product id',
  62. filter_value_id int(11) NOT NULL COMMENT 'Filter value id',
  63. PRIMARY KEY (id)
  64. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  65. ); err != nil {
  66. return err
  67. }
  68. // Table: shop_filters
  69. if _, err := db.Exec(
  70. ctx,
  71. `CREATE TABLE shop_filters (
  72. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  73. name varchar(255) NOT NULL COMMENT 'Filter name in CP',
  74. filter varchar(255) NOT NULL COMMENT 'Filter name in site',
  75. PRIMARY KEY (id)
  76. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  77. ); err != nil {
  78. return err
  79. }
  80. // Table: shop_filters_values
  81. if _, err := db.Exec(
  82. ctx,
  83. `CREATE TABLE shop_filters_values (
  84. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  85. filter_id int(11) NOT NULL COMMENT 'Filter id',
  86. name varchar(255) NOT NULL COMMENT 'Value name',
  87. PRIMARY KEY (id)
  88. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  89. ); err != nil {
  90. return err
  91. }
  92. // Table: shop_products
  93. if _, err := db.Exec(
  94. ctx,
  95. `CREATE TABLE shop_products (
  96. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  97. user int(11) NOT NULL COMMENT 'User id',
  98. currency int(11) NOT NULL COMMENT 'Currency id',
  99. price float(8,2) NOT NULL COMMENT 'Product price',
  100. name varchar(255) NOT NULL COMMENT 'Product name',
  101. alias varchar(255) NOT NULL COMMENT 'Product alias',
  102. briefly text NOT NULL COMMENT 'Product brief content',
  103. content text NOT NULL COMMENT 'Product content',
  104. datetime datetime NOT NULL COMMENT 'Creation date/time',
  105. active int(1) NOT NULL COMMENT 'Is active product or not',
  106. PRIMARY KEY (id)
  107. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  108. ); err != nil {
  109. return err
  110. }
  111. // Demo datas
  112. if _, err := db.Exec(
  113. ctx,
  114. `INSERT INTO shop_cat_product_rel (id, product_id, category_id)
  115. VALUES
  116. (1, 1, 3);`,
  117. ); err != nil {
  118. return err
  119. }
  120. if _, err := db.Exec(
  121. ctx,
  122. `INSERT INTO shop_cats (id, user, name, alias, lft, rgt)
  123. VALUES
  124. (1, 1, 'ROOT', 'ROOT', 1, 6),
  125. (2, 1, 'Electronics', 'electronics', 2, 5),
  126. (3, 1, 'Mobile phones', 'mobile-phones', 3, 4);`,
  127. ); err != nil {
  128. return err
  129. }
  130. if _, err := db.Exec(
  131. ctx,
  132. `INSERT INTO shop_currencies (id, name, coefficient, code, symbol)
  133. VALUES
  134. (1, 'US Dollar', 1.0000, 'USD', '$');`,
  135. ); err != nil {
  136. return err
  137. }
  138. if _, err := db.Exec(
  139. ctx,
  140. `INSERT INTO shop_filter_product_values (id, product_id, filter_value_id)
  141. VALUES
  142. (1, 1, 3),
  143. (2, 1, 7),
  144. (3, 1, 9),
  145. (4, 1, 10),
  146. (5, 1, 11);`,
  147. ); err != nil {
  148. return err
  149. }
  150. if _, err := db.Exec(
  151. ctx,
  152. `INSERT INTO shop_filters (id, name, filter)
  153. VALUES
  154. (1, 'Mobile phones manufacturer', 'Manufacturer'),
  155. (2, 'Mobile phones memory', 'Memory'),
  156. (3, 'Mobile phones communication standard', 'Communication standard');`,
  157. ); err != nil {
  158. return err
  159. }
  160. if _, err := db.Exec(
  161. ctx,
  162. `INSERT INTO shop_filters_values (id, filter_id, name)
  163. VALUES
  164. (1, 1, 'Apple'),
  165. (2, 1, 'Asus'),
  166. (3, 1, 'Samsung'),
  167. (4, 2, '16 Gb'),
  168. (5, 2, '32 Gb'),
  169. (6, 2, '64 Gb'),
  170. (7, 2, '128 Gb'),
  171. (8, 2, '256 Gb'),
  172. (9, 3, '4G'),
  173. (10, 3, '2G'),
  174. (11, 3, '3G');`,
  175. ); err != nil {
  176. return err
  177. }
  178. if _, err := db.Exec(
  179. ctx,
  180. `INSERT INTO shop_products SET
  181. id = ?,
  182. user = ?,
  183. currency = ?,
  184. price = ?,
  185. name = ?,
  186. alias = ?,
  187. briefly = ?,
  188. content = ?,
  189. datetime = ?,
  190. active = ?
  191. ;`,
  192. 1,
  193. 1,
  194. 1,
  195. 1000.00,
  196. "Samsung Galaxy S10",
  197. "samsung-galaxy-s10",
  198. "<p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent.</p>",
  199. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Feugiat in ante metus dictum at tempor commodo ullamcorper a. Et malesuada fames ac turpis egestas sed tempus urna et. Euismod elementum nisi quis eleifend. Nisi porta lorem mollis aliquam ut porttitor. Ac turpis egestas maecenas pharetra convallis posuere. Nunc non blandit massa enim nec dui. Commodo elit at imperdiet dui accumsan sit amet nulla. Viverra accumsan in nisl nisi scelerisque. Dui nunc mattis enim ut tellus. Molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Faucibus ornare suspendisse sed nisi lacus. Nulla facilisi morbi tempus iaculis. Ut eu sem integer vitae justo eget magna fermentum iaculis. Ullamcorper sit amet risus nullam eget felis eget nunc. Volutpat sed cras ornare arcu dui vivamus. Eget magna fermentum iaculis eu non diam.</p><p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent. Interdum velit euismod in pellentesque massa placerat duis. Vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt. Nisl rhoncus mattis rhoncus urna neque viverra justo. Odio ut enim blandit volutpat. Ac auctor augue mauris augue neque gravida. Ut lectus arcu bibendum at varius vel. Porttitor leo a diam sollicitudin tempor id eu nisl nunc. Dolor sit amet consectetur adipiscing elit duis tristique. Semper quis lectus nulla at volutpat diam ut. Sapien eget mi proin sed.</p>",
  200. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  201. 1,
  202. ); err != nil {
  203. return err
  204. }
  205. // Indexes
  206. if _, err := db.Exec(ctx, `ALTER TABLE shop_cat_product_rel ADD UNIQUE KEY product_category (product_id,category_id) USING BTREE;`); err != nil {
  207. return err
  208. }
  209. if _, err := db.Exec(ctx, `ALTER TABLE shop_cat_product_rel ADD KEY FK_shop_cat_product_rel_product_id (product_id);`); err != nil {
  210. return err
  211. }
  212. if _, err := db.Exec(ctx, `ALTER TABLE shop_cat_product_rel ADD KEY FK_shop_cat_product_rel_category_id (category_id);`); err != nil {
  213. return err
  214. }
  215. if _, err := db.Exec(ctx, `ALTER TABLE shop_cats ADD UNIQUE KEY alias (alias);`); err != nil {
  216. return err
  217. }
  218. if _, err := db.Exec(ctx, `ALTER TABLE shop_cats ADD KEY lft (lft), ADD KEY rgt (rgt);`); err != nil {
  219. return err
  220. }
  221. if _, err := db.Exec(ctx, `ALTER TABLE shop_cats ADD KEY FK_shop_cats_user (user);`); err != nil {
  222. return err
  223. }
  224. if _, err := db.Exec(ctx, `ALTER TABLE shop_filter_product_values ADD UNIQUE KEY product_filter_value (product_id,filter_value_id) USING BTREE;`); err != nil {
  225. return err
  226. }
  227. if _, err := db.Exec(ctx, `ALTER TABLE shop_filter_product_values ADD KEY FK_shop_filter_product_values_product_id (product_id);`); err != nil {
  228. return err
  229. }
  230. if _, err := db.Exec(ctx, `ALTER TABLE shop_filter_product_values ADD KEY FK_shop_filter_product_values_filter_value_id (filter_value_id);`); err != nil {
  231. return err
  232. }
  233. if _, err := db.Exec(ctx, `ALTER TABLE shop_filters ADD KEY name (name);`); err != nil {
  234. return err
  235. }
  236. if _, err := db.Exec(ctx, `ALTER TABLE shop_filters_values ADD KEY FK_shop_filters_values_filter_id (filter_id);`); err != nil {
  237. return err
  238. }
  239. if _, err := db.Exec(ctx, `ALTER TABLE shop_filters_values ADD KEY name (name);`); err != nil {
  240. return err
  241. }
  242. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ADD UNIQUE KEY alias (alias);`); err != nil {
  243. return err
  244. }
  245. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ADD KEY FK_shop_products_user (user);`); err != nil {
  246. return err
  247. }
  248. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ADD KEY FK_shop_products_currency (currency);`); err != nil {
  249. return err
  250. }
  251. // References
  252. if _, err := db.Exec(
  253. ctx,
  254. `ALTER TABLE shop_cat_product_rel ADD CONSTRAINT FK_shop_cat_product_rel_product_id
  255. FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
  256. `); err != nil {
  257. return err
  258. }
  259. if _, err := db.Exec(
  260. ctx,
  261. `ALTER TABLE shop_cat_product_rel ADD CONSTRAINT FK_shop_cat_product_rel_category_id
  262. FOREIGN KEY (category_id) REFERENCES shop_cats (id) ON DELETE RESTRICT;
  263. `); err != nil {
  264. return err
  265. }
  266. if _, err := db.Exec(
  267. ctx,
  268. `ALTER TABLE shop_cats ADD CONSTRAINT FK_shop_cats_user
  269. FOREIGN KEY (user) REFERENCES users (id) ON DELETE RESTRICT;
  270. `); err != nil {
  271. return err
  272. }
  273. if _, err := db.Exec(
  274. ctx,
  275. `ALTER TABLE shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_product_id
  276. FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
  277. `); err != nil {
  278. return err
  279. }
  280. if _, err := db.Exec(
  281. ctx,
  282. `ALTER TABLE shop_filter_product_values ADD CONSTRAINT FK_shop_filter_product_values_filter_value_id
  283. FOREIGN KEY (filter_value_id) REFERENCES shop_filters_values (id) ON DELETE RESTRICT;
  284. `); err != nil {
  285. return err
  286. }
  287. if _, err := db.Exec(
  288. ctx,
  289. `ALTER TABLE shop_filters_values ADD CONSTRAINT FK_shop_filters_values_filter_id
  290. FOREIGN KEY (filter_id) REFERENCES shop_filters (id) ON DELETE RESTRICT;
  291. `); err != nil {
  292. return err
  293. }
  294. if _, err := db.Exec(
  295. ctx,
  296. `ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_user
  297. FOREIGN KEY (user) REFERENCES users (id) ON DELETE RESTRICT;
  298. `); err != nil {
  299. return err
  300. }
  301. if _, err := db.Exec(
  302. ctx,
  303. `ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_currency
  304. FOREIGN KEY (currency) REFERENCES shop_currencies (id) ON DELETE RESTRICT;
  305. `); err != nil {
  306. return err
  307. }
  308. return nil
  309. }