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

     1  package console
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     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  	"github.com/goravel/framework/contracts/database/seeder"
    13  )
    14  
    15  type SeedCommand struct {
    16  	config config.Config
    17  	seeder seeder.Facade
    18  }
    19  
    20  func NewSeedCommand(config config.Config, seeder seeder.Facade) *SeedCommand {
    21  	return &SeedCommand{
    22  		config: config,
    23  		seeder: seeder,
    24  	}
    25  }
    26  
    27  // Signature The name and signature of the console command.
    28  func (receiver *SeedCommand) Signature() string {
    29  	return "db:seed"
    30  }
    31  
    32  // Description The console command description.
    33  func (receiver *SeedCommand) Description() string {
    34  	return "Seed the database with records"
    35  }
    36  
    37  // Extend The console command extend.
    38  func (receiver *SeedCommand) Extend() command.Extend {
    39  	return command.Extend{
    40  		Category: "db",
    41  		Flags: []command.Flag{
    42  			&command.BoolFlag{
    43  				Name:    "force",
    44  				Aliases: []string{"f"},
    45  				Usage:   "force the operation to run when in production",
    46  			},
    47  			&command.StringSliceFlag{
    48  				Name:    "seeder",
    49  				Aliases: []string{"s"},
    50  				Usage:   "specify the seeder(s) to run",
    51  			},
    52  		},
    53  	}
    54  }
    55  
    56  // Handle executes the console command.
    57  func (receiver *SeedCommand) Handle(ctx console.Context) error {
    58  	force := ctx.OptionBool("force")
    59  	if err := receiver.ConfirmToProceed(force); err != nil {
    60  		color.Redln(err)
    61  		return nil
    62  	}
    63  
    64  	names := ctx.OptionSlice("seeder")
    65  	seeders, err := receiver.GetSeeders(names)
    66  	if err != nil {
    67  		color.Redln(err)
    68  		return nil
    69  	}
    70  	if len(seeders) == 0 {
    71  		color.Redln("no seeders found")
    72  		return nil
    73  	}
    74  
    75  	if err := receiver.seeder.Call(seeders); err != nil {
    76  		color.Redf("error running seeder: %v\n", err)
    77  	}
    78  	color.Greenln("Database seeding completed successfully.")
    79  
    80  	return nil
    81  }
    82  
    83  // ConfirmToProceed determines if the command should proceed based on user confirmation.
    84  func (receiver *SeedCommand) ConfirmToProceed(force bool) error {
    85  	if force || (receiver.config.Env("APP_ENV") != "production") {
    86  		return nil
    87  	}
    88  
    89  	return errors.New("application in production use --force to run this command")
    90  }
    91  
    92  // GetSeeders returns a seeder instances
    93  func (receiver *SeedCommand) GetSeeders(names []string) ([]seeder.Seeder, error) {
    94  	if len(names) == 0 {
    95  		return receiver.seeder.GetSeeders(), nil
    96  	}
    97  	var seeders []seeder.Seeder
    98  	for _, name := range names {
    99  		seeder := receiver.seeder.GetSeeder(name)
   100  		if seeder == nil {
   101  			return nil, fmt.Errorf("no seeder of %s found", name)
   102  		}
   103  		seeders = append(seeders, seeder)
   104  	}
   105  	return seeders, nil
   106  }