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

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