first commit
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"git.fsdpf.net/go/db"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const test_table = "test_users"
|
||||
|
||||
var mysql *Builder
|
||||
var sqlite3 *Builder
|
||||
|
||||
func init() {
|
||||
odbc := db.Open(map[string]db.DBConfig{
|
||||
"default": {
|
||||
Driver: "mysql",
|
||||
Host: "localhost",
|
||||
Port: "3366",
|
||||
Database: "demo",
|
||||
Username: "demo",
|
||||
Password: "ded86bf25d661bb723f3898b2440dd678382e2dd",
|
||||
Charset: "utf8mb4",
|
||||
MultiStatements: true,
|
||||
// ParseTime: true,
|
||||
},
|
||||
"sqlite3": {
|
||||
Driver: "sqlite3",
|
||||
File: "test.db3",
|
||||
},
|
||||
})
|
||||
|
||||
mysql = NewSchema(odbc.Connection("default"))
|
||||
sqlite3 = NewSchema(odbc.Connection("sqlite3"))
|
||||
}
|
||||
|
||||
func TestHasTable(t *testing.T) {
|
||||
t.Log(mysql.HasTable("users"))
|
||||
t.Log(sqlite3.HasTable("users"))
|
||||
}
|
||||
|
||||
func TestHasColumns(t *testing.T) {
|
||||
t.Log("mysql")
|
||||
t.Log(mysql.HasColumns("users", "id"))
|
||||
t.Log("sqlite3")
|
||||
t.Log(sqlite3.HasColumns("users", "id"))
|
||||
}
|
||||
|
||||
func TestGetColumnListing(t *testing.T) {
|
||||
t.Log("mysql")
|
||||
t.Log(mysql.GetColumnListing("users"))
|
||||
t.Log("sqlite3")
|
||||
t.Log(sqlite3.GetColumnListing("users"))
|
||||
}
|
||||
|
||||
// Mysql 创建表
|
||||
func TestMysqlCreateTable(t *testing.T) {
|
||||
if err := mysql.Create(test_table, func(table *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.Raw("ON UPDATE CURRENT_TIMESTAMP")).Comment("更新时间")
|
||||
table.DateTime("deleted_at").Nullable().Comment("删除时间")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Sqlite 创建表
|
||||
func TestSqliteCreateTable(t *testing.T) {
|
||||
if err := sqlite3.Create(test_table, func(table *Blueprint) {
|
||||
table.Charset("utf8mb4")
|
||||
|
||||
table.Increments("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.Raw("ON UPDATE CURRENT_TIMESTAMP")).Comment("更新时间")
|
||||
table.DateTime("deleted_at").Nullable().Comment("删除时间")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Mysql 修改表, 添加字段
|
||||
func TestMysqlAddColumn(t *testing.T) {
|
||||
if err := mysql.Table(test_table, func(table *Blueprint) {
|
||||
table.String("name", 50).Nullable().Comment("用户名")
|
||||
table.TinyInteger("age").Nullable().Comment("年龄")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Sqlite 修改表, 添加字段
|
||||
func TestSqliteAddColumn(t *testing.T) {
|
||||
if err := sqlite3.Table(test_table, func(table *Blueprint) {
|
||||
table.String("name", 50).Nullable().Comment("用户名")
|
||||
table.TinyInteger("age").Nullable().Comment("年龄")
|
||||
}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Mysql 修改表, 添加/编辑字段
|
||||
func TestMysqlChangeColumn(t *testing.T) {
|
||||
if err := mysql.Table(test_table, func(table *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)
|
||||
}
|
||||
}
|
||||
|
||||
// Sqlite 修改表, 添加/编辑字段
|
||||
func TestSqliteChangeColumn(t *testing.T) {
|
||||
if err := sqlite3.Table(test_table, func(table *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)
|
||||
}
|
||||
}
|
||||
|
||||
// Mysql 修改表, 添加/编辑/删除字段
|
||||
func TestMysqlDropColumn(t *testing.T) {
|
||||
if err := mysql.Table(test_table, func(table *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)
|
||||
}
|
||||
}
|
||||
|
||||
// Sqlite 修改表, 添加/编辑/删除字段
|
||||
func TestSqliteDropColumn(t *testing.T) {
|
||||
if err := sqlite3.Table(test_table, func(table *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)
|
||||
}
|
||||
}
|
||||
|
||||
// Mysql重命名表
|
||||
func TestMysqlRename(t *testing.T) {
|
||||
if err := mysql.Rename(test_table, test_table+"_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// SQLite 重命名表
|
||||
func TestSqliteRename(t *testing.T) {
|
||||
if err := sqlite3.Rename(test_table, test_table+"_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Mysql 删除表
|
||||
func TestMysqlDropTable(t *testing.T) {
|
||||
if err := mysql.Drop(test_table + "_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Sqlite 删除表
|
||||
func TestSqliteDropTable(t *testing.T) {
|
||||
if err := sqlite3.Drop(test_table + "_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Mysql 删除表
|
||||
func TestMysqlDropTableIfExists(t *testing.T) {
|
||||
if err := mysql.DropIfExists(test_table + "_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Sqlite 删除表
|
||||
func TestSqliteDropTableIfExists(t *testing.T) {
|
||||
if err := sqlite3.DropIfExists(test_table + "_alias"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user