-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [#280] Refactor database migrate - optimize make:migration comm…
…and (#606) * feat: [#280] Refactor database migrate - optimize make:migration command * chore: update mocks * add Connection to Migration * chore: update mocks * fix unit test --------- Co-authored-by: hwbrzzl <hwbrzzl@users.noreply.github.com>
- Loading branch information
Showing
24 changed files
with
1,348 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package migration | ||
|
||
const ( | ||
DriverDefault = "default" | ||
DriverSql = "sql" | ||
) | ||
|
||
type Driver interface { | ||
Create(name string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package schema | ||
|
||
type Blueprint interface { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package schema | ||
|
||
type Schema interface { | ||
// Create a new table on the schema. | ||
//Create(table string, callback func(table Blueprint)) | ||
// Connection Get the connection for the schema. | ||
Connection() Schema | ||
// DropIfExists Drop a table from the schema if exists. | ||
//DropIfExists(table string) | ||
// Register migrations. | ||
Register([]Migration) | ||
// Sql Execute a sql directly. | ||
Sql(callback func(table Blueprint)) | ||
// Table Modify a table on the schema. | ||
//Table(table string, callback func(table Blueprint)) | ||
} | ||
|
||
type Migration interface { | ||
// Signature Get the migration signature. | ||
Signature() string | ||
// Connection Get the connection for the migration. | ||
Connection() string | ||
// Up Run the migrations. | ||
Up() | ||
// Down Reverse the migrations. | ||
Down() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package migration | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/goravel/framework/support/carbon" | ||
"github.com/goravel/framework/support/file" | ||
"github.com/goravel/framework/support/str" | ||
) | ||
|
||
type DefaultDriver struct { | ||
} | ||
|
||
func NewDefaultDriver() *DefaultDriver { | ||
return &DefaultDriver{} | ||
} | ||
|
||
func (r *DefaultDriver) Create(name string) error { | ||
// 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) | ||
|
||
// First we will get the stub file for the migration, which serves as a type | ||
// of template for the migration. Once we have those we will populate the | ||
// various place-holders, save the file, and run the post create event. | ||
stub := r.getStub(table, create) | ||
|
||
// Prepend timestamp to the file name. | ||
fileName := r.getFileName(name) | ||
|
||
// Create the up.sql file. | ||
if err := file.Create(r.getPath(fileName), r.populateStub(stub, fileName)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// getStub Get the migration stub file. | ||
func (r *DefaultDriver) getStub(table string, create bool) string { | ||
if table == "" { | ||
return Stubs{}.Empty() | ||
} | ||
|
||
if create { | ||
return Stubs{}.Create() | ||
} | ||
|
||
return Stubs{}.Update() | ||
} | ||
|
||
// populateStub Populate the place-holders in the migration stub. | ||
func (r *DefaultDriver) populateStub(stub, fileName string) string { | ||
stub = strings.ReplaceAll(stub, "DummyMigration", str.Of(fileName).Prepend("m_").Studly().String()) | ||
stub = strings.ReplaceAll(stub, "DummyName", fileName) | ||
|
||
return stub | ||
} | ||
|
||
// getPath Get the full path to the migration. | ||
func (r *DefaultDriver) getPath(name string) string { | ||
pwd, _ := os.Getwd() | ||
|
||
return filepath.Join(pwd, "database", "migrations", name+".go") | ||
} | ||
|
||
func (r *DefaultDriver) getFileName(name string) string { | ||
return fmt.Sprintf("%s_%s", carbon.Now().ToShortDateTimeString(), name) | ||
} |
Oops, something went wrong.