000000007.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package migrate
  2. import (
  3. "context"
  4. "golang-fave/engine/sqlw"
  5. )
  6. func Migrate_000000007(ctx context.Context, db *sqlw.DB, host string) error {
  7. // Changes
  8. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ADD COLUMN vendor VARCHAR(255) NOT NULL DEFAULT '' AFTER alias;`); err != nil {
  9. return err
  10. }
  11. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ADD COLUMN quantity INT(11) NOT NULL DEFAULT 0 AFTER vendor;`); err != nil {
  12. return err
  13. }
  14. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ADD COLUMN category INT(11) NOT NULL DEFAULT 1 AFTER quantity;`); err != nil {
  15. return err
  16. }
  17. // Indexes
  18. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ADD KEY FK_shop_products_category (category);`); err != nil {
  19. return err
  20. }
  21. // References
  22. if _, err := db.Exec(
  23. ctx,
  24. `ALTER TABLE shop_products ADD CONSTRAINT FK_shop_products_category
  25. FOREIGN KEY (category) REFERENCES shop_cats (id) ON DELETE RESTRICT;
  26. `); err != nil {
  27. return err
  28. }
  29. // Remove default
  30. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ALTER vendor DROP DEFAULT;`); err != nil {
  31. return err
  32. }
  33. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ALTER quantity DROP DEFAULT;`); err != nil {
  34. return err
  35. }
  36. if _, err := db.Exec(ctx, `ALTER TABLE shop_products ALTER category DROP DEFAULT;`); err != nil {
  37. return err
  38. }
  39. return nil
  40. }