Create comprehensive documentation for future Claude Code instances working in this repository, including: - Development commands for testing, building, and code quality - Core architecture overview of the SQL query builder system - Directory structure and component explanations - Testing patterns and conventions - Key dependencies and their purposes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
package mysql_test
|
|
|
|
import (
|
|
"os"
|
|
|
|
"git.fsdpf.net/go/db"
|
|
"git.fsdpf.net/go/db/engine"
|
|
"git.fsdpf.net/go/db/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)
|
|
}
|
|
}
|