forked from goravel/framework
/
migrate_make_command.go
62 lines (49 loc) · 1.74 KB
/
migrate_make_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package console
import (
"github.com/gookit/color"
"github.com/mewway/go-laravel/contracts/config"
"github.com/mewway/go-laravel/contracts/console"
"github.com/mewway/go-laravel/contracts/console/command"
)
type MigrateMakeCommand struct {
config config.Config
}
func NewMigrateMakeCommand(config config.Config) *MigrateMakeCommand {
return &MigrateMakeCommand{config: config}
}
// Signature The name and signature of the console command.
func (receiver *MigrateMakeCommand) Signature() string {
return "make:migration"
}
// Description The console command description.
func (receiver *MigrateMakeCommand) Description() string {
return "Create a new migration file"
}
// Extend The console command extend.
func (receiver *MigrateMakeCommand) Extend() command.Extend {
return command.Extend{
Category: "make",
}
}
// Handle Execute the console command.
func (receiver *MigrateMakeCommand) Handle(ctx console.Context) error {
// It's possible for the developer to specify the tables to modify in this
// schema operation. The developer may also specify if this table needs
// to be freshly created, so we can create the appropriate migrations.
name := ctx.Argument(0)
if name == "" {
color.Redln("Not enough arguments (missing: name)")
return nil
}
// We will attempt to guess the table name if this the migration has
// "create" in the name. This will allow us to provide a convenient way
// of creating migrations that create new tables for the application.
table, create := TableGuesser{}.Guess(name)
// Write the migration file to disk.
migrateCreator := NewMigrateCreator(receiver.config)
if err := migrateCreator.Create(name, table, create); err != nil {
return err
}
color.Green.Printf("Created Migration: %s\n", name)
return nil
}