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

     1  package console
     2  
     3  import (
     4  	_ "github.com/go-sql-driver/mysql"
     5  	"github.com/golang-migrate/migrate/v4"
     6  	_ "github.com/golang-migrate/migrate/v4/source/file"
     7  	"github.com/gookit/color"
     8  
     9  	"github.com/goravel/framework/contracts/config"
    10  	"github.com/goravel/framework/contracts/console"
    11  	"github.com/goravel/framework/contracts/console/command"
    12  )
    13  
    14  type MigrateResetCommand struct {
    15  	config config.Config
    16  }
    17  
    18  func NewMigrateResetCommand(config config.Config) *MigrateResetCommand {
    19  	return &MigrateResetCommand{
    20  		config: config,
    21  	}
    22  }
    23  
    24  // Signature The name and signature of the console command.
    25  func (receiver *MigrateResetCommand) Signature() string {
    26  	return "migrate:reset"
    27  }
    28  
    29  // Description The console command description.
    30  func (receiver *MigrateResetCommand) Description() string {
    31  	return "Rollback all database migrations"
    32  }
    33  
    34  // Extend The console command extend.
    35  func (receiver *MigrateResetCommand) Extend() command.Extend {
    36  	return command.Extend{
    37  		Category: "migrate",
    38  	}
    39  }
    40  
    41  // Handle Execute the console command.
    42  func (receiver *MigrateResetCommand) Handle(ctx console.Context) error {
    43  	m, err := getMigrate(receiver.config)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	if m == nil {
    48  		color.Yellowln("Please fill database config first")
    49  
    50  		return nil
    51  	}
    52  
    53  	// Rollback all migrations.
    54  	if err = m.Down(); err != nil && err != migrate.ErrNoChange {
    55  		color.Redln("Migration reset failed:", err.Error())
    56  
    57  		return nil
    58  	}
    59  
    60  	color.Greenln("Migration reset success")
    61  
    62  	return nil
    63  }