-
Notifications
You must be signed in to change notification settings - Fork 88
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 command
- Loading branch information
Showing
17 changed files
with
727 additions
and
21 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,25 @@ | ||
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 | ||
// 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
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) | ||
} |
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,127 @@ | ||
package migration | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/goravel/framework/support/carbon" | ||
"github.com/goravel/framework/support/file" | ||
) | ||
|
||
func TestDefaultDriverCreate(t *testing.T) { | ||
now := carbon.FromDateTime(2024, 8, 17, 21, 45, 1) | ||
carbon.SetTestNow(now) | ||
pwd, _ := os.Getwd() | ||
path := filepath.Join(pwd, "database", "migrations") | ||
|
||
tests := []struct { | ||
name string | ||
argument string | ||
file string | ||
content string | ||
}{ | ||
{ | ||
name: "empty template", | ||
argument: "fix_users_table", | ||
file: "20240817214501_fix_users_table", | ||
content: `package migrations | ||
type M20240817214501FixUsersTable struct { | ||
} | ||
// Signature The unique signature for the migration. | ||
func (r *M20240817214501FixUsersTable) Signature() string { | ||
return "20240817214501_fix_users_table" | ||
} | ||
// Connection The database connection that should be used by the migration. | ||
func (r *M20240817214501FixUsersTable) Connection() string { | ||
return "" | ||
} | ||
// Up Run the migrations. | ||
func (r *M20240817214501FixUsersTable) Up() { | ||
} | ||
// Down Reverse the migrations. | ||
func (r *M20240817214501FixUsersTable) Down() { | ||
}`, | ||
}, | ||
{ | ||
name: "create template", | ||
argument: "create_users_table", | ||
file: "20240817214501_create_users_table", | ||
content: `package migrations | ||
type M20240817214501CreateUsersTable struct { | ||
} | ||
// Signature The unique signature for the migration. | ||
func (r *M20240817214501CreateUsersTable) Signature() string { | ||
return "20240817214501_create_users_table" | ||
} | ||
// Connection The database connection that should be used by the migration. | ||
func (r *M20240817214501CreateUsersTable) Connection() string { | ||
return "" | ||
} | ||
// Up Run the migrations. | ||
func (r *M20240817214501CreateUsersTable) Up() { | ||
} | ||
// Down Reverse the migrations. | ||
func (r *M20240817214501CreateUsersTable) Down() { | ||
}`, | ||
}, | ||
{ | ||
name: "update template", | ||
argument: "add_name_to_users_table", | ||
file: "20240817214501_add_name_to_users_table", | ||
content: `package migrations | ||
type M20240817214501AddNameToUsersTable struct { | ||
} | ||
// Signature The unique signature for the migration. | ||
func (r *M20240817214501AddNameToUsersTable) Signature() string { | ||
return "20240817214501_add_name_to_users_table" | ||
} | ||
// Connection The database connection that should be used by the migration. | ||
func (r *M20240817214501AddNameToUsersTable) Connection() string { | ||
return "" | ||
} | ||
// Up Run the migrations. | ||
func (r *M20240817214501AddNameToUsersTable) Up() { | ||
} | ||
// Down Reverse the migrations. | ||
func (r *M20240817214501AddNameToUsersTable) Down() { | ||
}`, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
driver := &DefaultDriver{} | ||
|
||
assert.Nil(t, driver.Create(test.argument)) | ||
assert.Equal(t, test.file, driver.getFileName(test.argument)) | ||
assert.True(t, file.Exists(filepath.Join(path, test.file+".go"))) | ||
assert.True(t, file.Contain(driver.getPath(driver.getFileName(test.argument)), test.content)) | ||
}) | ||
} | ||
|
||
assert.Nil(t, file.Remove("database")) | ||
} |
Oops, something went wrong.