github.com/goravel/framework@v1.13.9/database/console/migrate_refresh_command.go (about)

     1  package console
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	_ "github.com/go-sql-driver/mysql"
     8  	"github.com/golang-migrate/migrate/v4"
     9  	_ "github.com/golang-migrate/migrate/v4/source/file"
    10  	"github.com/gookit/color"
    11  
    12  	"github.com/goravel/framework/contracts/config"
    13  	"github.com/goravel/framework/contracts/console"
    14  	"github.com/goravel/framework/contracts/console/command"
    15  )
    16  
    17  type MigrateRefreshCommand struct {
    18  	config  config.Config
    19  	artisan console.Artisan
    20  }
    21  
    22  func NewMigrateRefreshCommand(config config.Config, artisan console.Artisan) *MigrateRefreshCommand {
    23  	return &MigrateRefreshCommand{
    24  		config:  config,
    25  		artisan: artisan,
    26  	}
    27  }
    28  
    29  // Signature The name and signature of the console command.
    30  func (receiver *MigrateRefreshCommand) Signature() string {
    31  	return "migrate:refresh"
    32  }
    33  
    34  // Description The console command description.
    35  func (receiver *MigrateRefreshCommand) Description() string {
    36  	return "Reset and re-run all migrations"
    37  }
    38  
    39  // Extend The console command extend.
    40  func (receiver *MigrateRefreshCommand) Extend() command.Extend {
    41  	return command.Extend{
    42  		Category: "migrate",
    43  		Flags: []command.Flag{
    44  			&command.StringFlag{
    45  				Name:  "step",
    46  				Value: "",
    47  				Usage: "refresh steps",
    48  			},
    49  			&command.BoolFlag{
    50  				Name:  "seed",
    51  				Usage: "seed the database after running migrations",
    52  			},
    53  			&command.StringSliceFlag{
    54  				Name:  "seeder",
    55  				Usage: "specify the seeder(s) to use for seeding the database",
    56  			},
    57  		},
    58  	}
    59  }
    60  
    61  // Handle Execute the console command.
    62  func (receiver *MigrateRefreshCommand) Handle(ctx console.Context) error {
    63  	m, err := getMigrate(receiver.config)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	if m == nil {
    68  		color.Yellowln("Please fill database config first")
    69  
    70  		return nil
    71  	}
    72  
    73  	if step := ctx.Option("step"); step != "" {
    74  		stepString := "-" + step
    75  		s, err := strconv.Atoi(stepString)
    76  		if err != nil {
    77  			color.Redln("Migration refresh failed: invalid step", ctx.Option("step"))
    78  
    79  			return nil
    80  		}
    81  
    82  		if err = m.Steps(s); err != nil && err != migrate.ErrNoChange && err != migrate.ErrNilVersion {
    83  			switch err.(type) {
    84  			case migrate.ErrShortLimit:
    85  			default:
    86  				color.Redln("Migration refresh failed:", err.Error())
    87  
    88  				return nil
    89  			}
    90  		}
    91  	} else {
    92  		if err = m.Down(); err != nil && err != migrate.ErrNoChange {
    93  			color.Redln("Migration reset failed:", err.Error())
    94  
    95  			return nil
    96  		}
    97  	}
    98  
    99  	if err = m.Up(); err != nil && err != migrate.ErrNoChange {
   100  		color.Redln("Migration refresh failed:", err.Error())
   101  
   102  		return nil
   103  	}
   104  
   105  	// Seed the database if the "seed" flag is provided
   106  	if ctx.OptionBool("seed") {
   107  		seeders := ctx.OptionSlice("seeder")
   108  		seederFlag := ""
   109  		if len(seeders) > 0 {
   110  			seederFlag = " --seeder " + strings.Join(seeders, ",")
   111  		}
   112  		receiver.artisan.Call("db:seed" + seederFlag)
   113  	}
   114  	color.Greenln("Migration refresh success")
   115  
   116  	return nil
   117  }