000000008.go 802 B

1234567891011121314151617181920212223242526272829303132333435
  1. package migrate
  2. import (
  3. "context"
  4. "golang-fave/engine/sqlw"
  5. )
  6. func Migrate_000000008(ctx context.Context, db *sqlw.DB, host string) error {
  7. // Changes
  8. if _, err := db.Exec(ctx, `ALTER TABLE blog_posts ADD COLUMN category INT(11) NOT NULL DEFAULT 1 AFTER alias;`); err != nil {
  9. return err
  10. }
  11. // Indexes
  12. if _, err := db.Exec(ctx, `ALTER TABLE blog_posts ADD KEY FK_blog_posts_category (category);`); err != nil {
  13. return err
  14. }
  15. // References
  16. if _, err := db.Exec(
  17. ctx,
  18. `ALTER TABLE blog_posts ADD CONSTRAINT FK_blog_posts_category
  19. FOREIGN KEY (category) REFERENCES blog_cats (id) ON DELETE RESTRICT;
  20. `); err != nil {
  21. return err
  22. }
  23. // Remove default
  24. if _, err := db.Exec(ctx, `ALTER TABLE blog_posts ALTER category DROP DEFAULT;`); err != nil {
  25. return err
  26. }
  27. return nil
  28. }