github.com/goravel/framework@v1.13.9/database/console/migrate_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 MigrateCommand struct {
    15  	config config.Config
    16  }
    17  
    18  func NewMigrateCommand(config config.Config) *MigrateCommand {
    19  	return &MigrateCommand{
    20  		config: config,
    21  	}
    22  }
    23  
    24  // Signature The name and signature of the console command.
    25  func (receiver *MigrateCommand) Signature() string {
    26  	return "migrate"
    27  }
    28  
    29  // Description The console command description.
    30  func (receiver *MigrateCommand) Description() string {
    31  	return "Run the database migrations"
    32  }
    33  
    34  // Extend The console command extend.
    35  func (receiver *MigrateCommand) Extend() command.Extend {
    36  	return command.Extend{
    37  		Category: "migrate",
    38  	}
    39  }
    40  
    41  // Handle Execute the console command.
    42  func (receiver *MigrateCommand) 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  	if err = m.Up(); err != nil && err != migrate.ErrNoChange {
    54  		color.Redln("Migration failed:", err.Error())
    55  
    56  		return nil
    57  	}
    58  
    59  	color.Greenln("Migration success")
    60  
    61  	return nil
    62  }