[feat] 新增 表结构操作
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package mysql_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.fsdpf.net/go/db/v2"
|
||||
"git.fsdpf.net/go/db/v2/engine"
|
||||
"git.fsdpf.net/go/db/v2/schema"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type mysqlBuilderTest struct {
|
||||
suite.Suite
|
||||
builder *schema.Builder
|
||||
}
|
||||
|
||||
func (t *mysqlBuilderTest) SetupSuite() {
|
||||
db := engine.Open(map[string]engine.DBConfig{
|
||||
"test-mysql": engine.NewDBConfig("mysql",
|
||||
engine.WithHost(os.Getenv("MYSQL_HOST")),
|
||||
engine.WithPort(os.Getenv("MYSQL_PORT")),
|
||||
engine.WithDatabase(os.Getenv("MYSQL_DB")),
|
||||
engine.WithUsername(os.Getenv("MYSQL_USER")),
|
||||
engine.WithPassword(os.Getenv("MYSQL_PASSWD")),
|
||||
engine.WithParseTime(true),
|
||||
),
|
||||
}).Connection("test-mysql")
|
||||
|
||||
t.builder = schema.New(db)
|
||||
}
|
||||
|
||||
func (t *mysqlBuilderTest) TestHasTable() {
|
||||
t.builder.HasTable("users")
|
||||
}
|
||||
|
||||
func (t *mysqlBuilderTest) TestGetColumnListing() {
|
||||
t.builder.GetColumnListing("users")
|
||||
}
|
||||
|
||||
// 创建表
|
||||
func (t *mysqlBuilderTest) TestCreateTable() {
|
||||
if err := t.builder.Create("users", func(table *schema.Blueprint) {
|
||||
table.Charset("utf8mb4")
|
||||
|
||||
table.BigIncrements("id").Comment("ID")
|
||||
table.Boolean("enabled").Default("1").Comment("是否有效")
|
||||
table.Char("created_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("创建者")
|
||||
table.Char("owned_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("拥有者")
|
||||
table.Timestamp("created_at").UseCurrent().Comment("创建时间")
|
||||
table.Timestamp("updated_at").UseCurrent().Default(db.L("ON UPDATE CURRENT_TIMESTAMP")).Comment("更新时间")
|
||||
table.DateTime("deleted_at").Nullable().Comment("删除时间")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 修改表, 添加字段
|
||||
func (t *mysqlBuilderTest) TestAddColumn() {
|
||||
if err := t.builder.Table("users", func(table *schema.Blueprint) {
|
||||
table.String("name", 50).Nullable().Comment("用户名")
|
||||
table.TinyInteger("age").Nullable().Comment("年龄")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 修改表, 添加/编辑字段
|
||||
func (t *mysqlBuilderTest) TestChangeColumn() {
|
||||
if err := t.builder.Table("users", func(table *schema.Blueprint) {
|
||||
table.String("name", 100).Nullable().Comment("用户名").Change("username")
|
||||
table.SmallInteger("age").Nullable().Comment("年龄").Change()
|
||||
|
||||
table.String("nickname", 100).Nullable().Comment("昵称")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 修改表, 添加/编辑/删除字段
|
||||
func (t *mysqlBuilderTest) TestDropColumn() {
|
||||
if err := t.builder.Table("users", func(table *schema.Blueprint) {
|
||||
table.String("username", 100).Default("").Comment("用户名").Change("name")
|
||||
table.Boolean("is_vip").Default(1).Comment("是否VIP")
|
||||
table.DropColumn("age")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 重命名表
|
||||
func (t *mysqlBuilderTest) TestRename() {
|
||||
if err := t.builder.Rename("users", "users_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 删除表
|
||||
func (t *mysqlBuilderTest) TestDropTable() {
|
||||
if err := t.builder.Drop("users_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 删除表
|
||||
func (t *mysqlBuilderTest) TestDropTableIfExists() {
|
||||
if err := t.builder.DropIfExists("users_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user