000000006.go 962 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package migrate
  2. import (
  3. "context"
  4. "golang-fave/engine/sqlw"
  5. )
  6. func Migrate_000000006(ctx context.Context, db *sqlw.DB, host string) error {
  7. // Table: shop_product_images
  8. if _, err := db.Exec(
  9. ctx,
  10. `CREATE TABLE shop_product_images (
  11. product_id int(11) NOT NULL,
  12. filename varchar(255) NOT NULL
  13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  14. ); err != nil {
  15. return err
  16. }
  17. // Indexes
  18. if _, err := db.Exec(ctx, `ALTER TABLE shop_product_images ADD UNIQUE KEY product_filename (product_id,filename) USING BTREE;`); err != nil {
  19. return err
  20. }
  21. if _, err := db.Exec(ctx, `ALTER TABLE shop_product_images ADD KEY FK_shop_product_images_product_id (product_id);`); err != nil {
  22. return err
  23. }
  24. // References
  25. if _, err := db.Exec(
  26. ctx,
  27. `ALTER TABLE shop_product_images ADD CONSTRAINT FK_shop_product_images_product_id
  28. FOREIGN KEY (product_id) REFERENCES shop_products (id) ON DELETE RESTRICT;
  29. `); err != nil {
  30. return err
  31. }
  32. return nil
  33. }