diff --git a/internal/integration/client_test.go b/internal/integration/client_test.go index 2e3474e6..cb4a8bb5 100644 --- a/internal/integration/client_test.go +++ b/internal/integration/client_test.go @@ -74,6 +74,16 @@ func TestCreate(t *testing.T) { require.True(t, user.ID > 0) } +func TestDefault(t *testing.T) { + device, err := c.QueryDevice().Create(c.ChangeDevice().SetID("c7e5b9af-0499-4eca-a7e6-77e10d56987b")) + require.NoError(t, err) + require.Equal(t, "device", device.Name.Val) + require.Equal(t, int64(31415926359899), device.Sequence.Val) + require.Equal(t, 3.1415, device.Weight.Val) + require.Equal(t, "c7e5b9af-0499-4eca-a7e6-77e10d56987b", device.UUID.Val) + require.Equal(t, int32(5), device.Age.Val) +} + func TestInsertAll(t *testing.T) { _, err := c.QueryUserPost().DeleteAll() require.NoError(t, err) diff --git a/internal/integration/mysql.hcl b/internal/integration/mysql.hcl index a329c30e..6e09f4d1 100644 --- a/internal/integration/mysql.hcl +++ b/internal/integration/mysql.hcl @@ -3,7 +3,7 @@ database "db" { time_zone = "Asia/Shanghai" config "development" { - url = "mysql://root:@127.0.0.1:3306/queryx_test" + url = "mysql://root@127.0.0.1:3306/queryx_test" } config "test" { url = env("DATABASE_URL") @@ -149,6 +149,31 @@ database "db" { null = false } + column "name" { + type = string + default = "device" + } + + column "sequence" { + type = bigint + default= 31415926359899 + } + + column "weight" { + type = float + default= 3.1415 + } + + column "uuid" { + type = uuid + default= "c7e5b9af-0499-4eca-a7e6-77e10d56987b" + } + + column "age" { + type = integer + default= 5 + } + primary_key { columns = ["id"] } diff --git a/internal/integration/postgresql.hcl b/internal/integration/postgresql.hcl index 80bda1c8..97372590 100644 --- a/internal/integration/postgresql.hcl +++ b/internal/integration/postgresql.hcl @@ -149,6 +149,32 @@ database "db" { null = false } + column "name" { + type = string + default = "device" + } + + column "sequence" { + type = bigint + default= 31415926359899 + } + + column "weight" { + type = float + default= 3.1415 + } + + column "uuid" { + type = uuid + default = "c7e5b9af-0499-4eca-a7e6-77e10d56987b" + } + + column "age" { + type = integer + default= 5 + } + + primary_key { columns = ["id"] } diff --git a/internal/integration/sqlite.hcl b/internal/integration/sqlite.hcl index c056094a..7bf08e98 100644 --- a/internal/integration/sqlite.hcl +++ b/internal/integration/sqlite.hcl @@ -149,6 +149,31 @@ database "db" { null = false } + column "name" { + type = string + default = "device" + } + + column "sequence" { + type = bigint + default= 31415926359899 + } + + column "weight" { + type = float + default= 3.1415 + } + + column "uuid" { + type = uuid + default= "c7e5b9af-0499-4eca-a7e6-77e10d56987b" + } + + column "age" { + type = integer + default= 5 + } + primary_key { columns = ["id"] } diff --git a/schema/hcl.go b/schema/hcl.go index f86642cf..11278ec9 100644 --- a/schema/hcl.go +++ b/schema/hcl.go @@ -507,7 +507,7 @@ func columnFromBlock(block *hcl.Block, ctx *hcl.EvalContext) (*Column, error) { switch column.Type { case "string", "uuid": column.Default = value.AsString() - case "integer": + case "integer", "bigint": column.Default = valueAsInt(value) case "boolean": column.Default = valueAsBool(value) diff --git a/schema/mysql.go b/schema/mysql.go index 264dfae7..1d964ab7 100644 --- a/schema/mysql.go +++ b/schema/mysql.go @@ -2,6 +2,7 @@ package schema import ( "fmt" + "strconv" "strings" "ariga.io/atlas/sql/mysql" @@ -23,17 +24,56 @@ func (d *Database) CreateMySQLSchema(dbName string) *schema.Schema { col.SetType(&schema.IntegerType{T: mysql.TypeBigInt}) if c.AutoIncrement { col.AddAttrs(&mysql.AutoIncrement{}) + } else { + if c.Default != nil { + d, ok := c.Default.(int) + if ok { + col.SetType(&schema.IntegerType{T: mysql.TypeBigInt}).SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) + } + } } case "string": col.SetType(&schema.StringType{T: mysql.TypeVarchar, Size: 255}) + if c.Default != nil { + d, ok := c.Default.(string) + if ok { + col.SetDefault(&schema.RawExpr{X: d}) + } + } case "text": col.SetType(&schema.StringType{T: mysql.TypeText}) + if c.Default != nil { + d, ok := c.Default.(string) + if ok { + col.SetDefault(&schema.RawExpr{X: d}) + } + } case "integer": col.SetType(&schema.IntegerType{T: mysql.TypeInt}) + if c.Default != nil { + d, ok := c.Default.(int) + if ok { + col.SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) + } + } case "float": col.SetType(&schema.FloatType{T: mysql.TypeFloat}) + if c.Default != nil { + f, ok := c.Default.(float64) + if ok { + str := strconv.FormatFloat(f, 'E', -10, 64) + col.SetDefault(&schema.RawExpr{X: str}) + } + } case "boolean": col.SetType(&schema.BoolType{T: mysql.TypeBoolean}) + if c.Default != nil { + b, ok := c.Default.(bool) + if ok { + str := strconv.FormatBool(b) + col.SetDefault(&schema.RawExpr{X: str}) + } + } case "enum": col.SetType(&schema.StringType{T: mysql.TypeEnum, Size: 0}) case "date": @@ -47,9 +87,16 @@ func (d *Database) CreateMySQLSchema(dbName string) *schema.Schema { col.SetType(&schema.JSONType{T: mysql.TypeJSON}) case "uuid": col.SetType(&schema.StringType{T: mysql.TypeVarchar, Size: 36}) + if c.Default != nil { + d, ok := c.Default.(string) + if ok { + col.SetDefault(&schema.RawExpr{X: d}) + } + } + } + if c.Default == nil { + col.SetNull(c.Null) } - - col.SetNull(c.Null) t.AddColumns(col) columnMap[c.Name] = col } diff --git a/schema/postgresql.go b/schema/postgresql.go index 64e735ab..3cca4883 100644 --- a/schema/postgresql.go +++ b/schema/postgresql.go @@ -25,7 +25,13 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.AutoIncrement { col.SetType(&postgres.SerialType{T: postgres.TypeBigSerial}) } else { - col.SetType(&schema.IntegerType{T: postgres.TypeInt8}) + col.SetType(&schema.IntegerType{T: postgres.TypeBigInt}) + if c.Default != nil { + d, ok := c.Default.(int) + if ok { + col.SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) + } + } } case "string": if c.Array { @@ -33,7 +39,7 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(string) if ok { - col.SetType(&postgres.ArrayType{Type: &schema.StringType{T: "character varying", Size: 0}, T: "varchar[]"}).SetDefault(&schema.RawExpr{X: fmt.Sprintf("'%s'", d)}) + col.SetDefault(&schema.RawExpr{X: fmt.Sprintf("'%s'", d)}) } } } else { @@ -41,7 +47,7 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(string) if ok { - col.SetType(&schema.StringType{T: "character varying", Size: 0}).SetDefault(&schema.RawExpr{X: fmt.Sprintf("'%s'", d)}) + col.SetDefault(&schema.RawExpr{X: fmt.Sprintf("'%s'", d)}) } } } @@ -53,7 +59,7 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(int) if ok { - col.SetType(&postgres.ArrayType{Type: &schema.IntegerType{T: "integer"}, T: "int[]"}).SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) + col.SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) } } } else { @@ -61,7 +67,7 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(int) if ok { - col.SetType(&schema.IntegerType{T: "integer"}).SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) + col.SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) } } } @@ -70,7 +76,7 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(float64) if ok { - col.SetType(&schema.FloatType{T: postgres.TypeFloat8}).SetDefault(&schema.RawExpr{X: strconv.FormatFloat(d, 'f', 10, 64)}) + col.SetDefault(&schema.RawExpr{X: strconv.FormatFloat(d, 'f', 10, 64)}) } } case "boolean": @@ -78,7 +84,7 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(bool) if ok { - col.SetType(&schema.BoolType{T: postgres.TypeBoolean}).SetDefault(&schema.RawExpr{X: strconv.FormatBool(d)}) + col.SetDefault(&schema.RawExpr{X: strconv.FormatBool(d)}) } } case "enum": @@ -98,12 +104,14 @@ func (d *Database) CreatePostgreSQLSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(string) if ok { - col.SetType(&postgres.UUIDType{T: postgres.TypeUUID}).SetDefault(&schema.RawExpr{X: d}) + col.SetDefault(&schema.RawExpr{X: fmt.Sprintf("'%s'", d)}) } } } - col.SetNull(c.Null) + if c.Default == nil { + col.SetNull(c.Null) + } t.AddColumns(col) columnMap[c.Name] = col } diff --git a/schema/sqlite.go b/schema/sqlite.go index 10f8005f..0637478a 100644 --- a/schema/sqlite.go +++ b/schema/sqlite.go @@ -28,19 +28,18 @@ func (d *Database) CreateSQLiteSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(string) if ok { - col.SetType(&schema.StringType{T: "varchar", Size: 0}).SetDefault(&schema.RawExpr{X: fmt.Sprintf("'%s'", d)}) + col.SetDefault(&schema.RawExpr{X: fmt.Sprintf("'%s'", d)}) } } case "text": col.SetType(&schema.StringType{T: "text", Size: 0}) case "integer": + col.SetType(&schema.IntegerType{T: "integer"}) if c.Default != nil { d, ok := c.Default.(int) if ok { - col.SetType(&schema.IntegerType{T: "integer"}).SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) + col.SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) } - } else { - col.SetType(&schema.IntegerType{T: "integer"}) } case "bigint": if c.AutoIncrement { @@ -48,13 +47,17 @@ func (d *Database) CreateSQLiteSchema(dbName string) *schema.Schema { col.AddAttrs(&sqlite.AutoIncrement{}) } else { col.SetType(&schema.IntegerType{T: "bigint"}) + d, ok := c.Default.(int) + if ok { + col.SetDefault(&schema.RawExpr{X: strconv.Itoa(d)}) + } } case "float": col.SetType(&schema.FloatType{T: "float"}) if c.Default != nil { d, ok := c.Default.(float64) if ok { - col.SetType(&schema.FloatType{T: "float"}).SetDefault(&schema.RawExpr{X: strconv.FormatFloat(d, 'f', 10, 64)}) + col.SetDefault(&schema.RawExpr{X: strconv.FormatFloat(d, 'f', 10, 64)}) } } case "boolean": @@ -62,7 +65,7 @@ func (d *Database) CreateSQLiteSchema(dbName string) *schema.Schema { if c.Default != nil { d, ok := c.Default.(bool) if ok { - col.SetType(&schema.BoolType{T: "boolean"}).SetDefault(&schema.RawExpr{X: strconv.FormatBool(d)}) + col.SetDefault(&schema.RawExpr{X: strconv.FormatBool(d)}) } } case "enum": @@ -79,7 +82,9 @@ func (d *Database) CreateSQLiteSchema(dbName string) *schema.Schema { fmt.Printf("This type is not supported:%+v", col.Type) } - col.SetNull(c.Null) + if c.Default == nil { + col.SetNull(c.Null) + } t.AddColumns(col) columnMap[c.Name] = col }