github.com/goravel/framework@v1.13.9/database/console/migrate_creator.go (about) 1 package console 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/goravel/framework/contracts/config" 9 "github.com/goravel/framework/contracts/database/orm" 10 "github.com/goravel/framework/support/carbon" 11 "github.com/goravel/framework/support/file" 12 ) 13 14 type MigrateCreator struct { 15 config config.Config 16 } 17 18 func NewMigrateCreator(config config.Config) *MigrateCreator { 19 return &MigrateCreator{ 20 config: config, 21 } 22 } 23 24 // Create a new migration 25 func (receiver MigrateCreator) Create(name string, table string, create bool) error { 26 // First we will get the stub file for the migration, which serves as a type 27 // of template for the migration. Once we have those we will populate the 28 // various place-holders, save the file, and run the post create event. 29 upStub, downStub := receiver.getStub(table, create) 30 31 //Create the up.sql file. 32 if err := file.Create(receiver.getPath(name, "up"), receiver.populateStub(upStub, table)); err != nil { 33 return err 34 } 35 36 //Create the down.sql file. 37 if err := file.Create(receiver.getPath(name, "down"), receiver.populateStub(downStub, table)); err != nil { 38 return err 39 } 40 41 return nil 42 } 43 44 // getStub Get the migration stub file. 45 func (receiver MigrateCreator) getStub(table string, create bool) (string, string) { 46 if table == "" { 47 return "", "" 48 } 49 50 driver := receiver.config.GetString("database.connections." + receiver.config.GetString("database.default") + ".driver") 51 switch orm.Driver(driver) { 52 case orm.DriverPostgresql: 53 if create { 54 return PostgresqlStubs{}.CreateUp(), PostgresqlStubs{}.CreateDown() 55 } 56 57 return PostgresqlStubs{}.UpdateUp(), PostgresqlStubs{}.UpdateDown() 58 case orm.DriverSqlite: 59 if create { 60 return SqliteStubs{}.CreateUp(), SqliteStubs{}.CreateDown() 61 } 62 63 return SqliteStubs{}.UpdateUp(), SqliteStubs{}.UpdateDown() 64 case orm.DriverSqlserver: 65 if create { 66 return SqlserverStubs{}.CreateUp(), SqlserverStubs{}.CreateDown() 67 } 68 69 return SqlserverStubs{}.UpdateUp(), SqlserverStubs{}.UpdateDown() 70 default: 71 if create { 72 return MysqlStubs{}.CreateUp(), MysqlStubs{}.CreateDown() 73 } 74 75 return MysqlStubs{}.UpdateUp(), MysqlStubs{}.UpdateDown() 76 } 77 } 78 79 // populateStub Populate the place-holders in the migration stub. 80 func (receiver MigrateCreator) populateStub(stub string, table string) string { 81 stub = strings.ReplaceAll(stub, "DummyDatabaseCharset", receiver.config.GetString("database.connections."+receiver.config.GetString("database.default")+".charset")) 82 83 if table != "" { 84 stub = strings.ReplaceAll(stub, "DummyTable", table) 85 } 86 87 return stub 88 } 89 90 // getPath Get the full path to the migration. 91 func (receiver MigrateCreator) getPath(name string, category string) string { 92 pwd, _ := os.Getwd() 93 94 return fmt.Sprintf("%s/database/migrations/%s_%s.%s.sql", pwd, carbon.Now().ToShortDateTimeString(), name, category) 95 }