000000015.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package migrate
  2. import (
  3. "context"
  4. "golang-fave/engine/sqlw"
  5. )
  6. func Migrate_000000015(ctx context.Context, db *sqlw.DB, host string) error {
  7. // Table: shop_orders
  8. if _, err := db.Exec(
  9. ctx,
  10. `CREATE TABLE shop_orders (
  11. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  12. create_datetime datetime NOT NULL COMMENT 'Create date/time',
  13. update_datetime datetime NOT NULL COMMENT 'Update date/time',
  14. currency_id int(11) NOT NULL COMMENT 'Currency ID',
  15. currency_name varchar(255) NOT NULL COMMENT 'Currency name',
  16. currency_coefficient float(8,4) NOT NULL DEFAULT '1.0000' COMMENT 'Currency coefficient',
  17. currency_code varchar(10) NOT NULL COMMENT 'Currency code',
  18. currency_symbol varchar(5) NOT NULL COMMENT 'Currency symbol',
  19. client_last_name varchar(64) NOT NULL COMMENT 'Client last name',
  20. client_first_name varchar(64) NOT NULL COMMENT 'Client first name',
  21. client_middle_name varchar(64) NOT NULL DEFAULT '' COMMENT 'Client middle name',
  22. client_phone varchar(20) NOT NULL DEFAULT '' COMMENT 'Client phone',
  23. client_email varchar(64) NOT NULL COMMENT 'Client email',
  24. client_delivery_comment text NOT NULL COMMENT 'Client delivery comment',
  25. client_order_comment text NOT NULL COMMENT 'Client order comment',
  26. status int(1) NOT NULL COMMENT 'new/confirmed/inprogress/canceled/completed',
  27. PRIMARY KEY (id)
  28. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  29. ); err != nil {
  30. return err
  31. }
  32. // Table: shop_order_products
  33. if _, err := db.Exec(
  34. ctx,
  35. `CREATE TABLE shop_order_products (
  36. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  37. order_id int(11) NOT NULL COMMENT 'Order ID',
  38. product_id int(11) NOT NULL COMMENT 'Product ID',
  39. price float(8,2) NOT NULL COMMENT 'Product price',
  40. quantity int(11) NOT NULL COMMENT 'Quantity',
  41. PRIMARY KEY (id)
  42. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  43. ); err != nil {
  44. return err
  45. }
  46. // Indexes
  47. if _, err := db.Exec(ctx, `ALTER TABLE shop_orders ADD KEY FK_shop_orders_currency_id (currency_id);`); err != nil {
  48. return err
  49. }
  50. if _, err := db.Exec(ctx, `ALTER TABLE shop_order_products ADD UNIQUE KEY order_product (order_id,product_id) USING BTREE;`); err != nil {
  51. return err
  52. }
  53. if _, err := db.Exec(ctx, `ALTER TABLE shop_order_products ADD KEY FK_shop_order_products_order_id (order_id);`); err != nil {
  54. return err
  55. }
  56. if _, err := db.Exec(ctx, `ALTER TABLE shop_order_products ADD KEY FK_shop_order_products_product_id (product_id);`); err != nil {
  57. return err
  58. }
  59. // References
  60. if _, err := db.Exec(
  61. ctx,
  62. `ALTER TABLE shop_orders ADD CONSTRAINT FK_shop_orders_currency_id
  63. FOREIGN KEY (currency_id) REFERENCES shop_currencies (id) ON DELETE RESTRICT;
  64. `); err != nil {
  65. return err
  66. }
  67. if _, err := db.Exec(
  68. ctx,
  69. `ALTER TABLE shop_order_products ADD CONSTRAINT FK_shop_order_products_order_id
  70. FOREIGN KEY (order_id) REFERENCES shop_orders (id) ON DELETE RESTRICT;
  71. `); err != nil {
  72. return err
  73. }
  74. if _, err := db.Exec(
  75. ctx,
  76. `ALTER TABLE shop_order_products ADD CONSTRAINT FK_shop_order_products_product_id
  77. FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
  78. `); err != nil {
  79. return err
  80. }
  81. return nil
  82. }