Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion internal/integration/mysql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ database "db" {
time_zone = "Asia/Shanghai"

config "development" {
url = "mysql://root:@127.0.0.1:3306/queryx_test"
url = "mysql://[email protected]:3306/queryx_test"
}
config "test" {
url = env("DATABASE_URL")
Expand Down Expand Up @@ -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"]
}
Expand Down
26 changes: 26 additions & 0 deletions internal/integration/postgresql.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
Expand Down
25 changes: 25 additions & 0 deletions internal/integration/sqlite.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
Expand Down
2 changes: 1 addition & 1 deletion schema/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 49 additions & 2 deletions schema/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schema

import (
"fmt"
"strconv"
"strings"

"ariga.io/atlas/sql/mysql"
Expand All @@ -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":
Expand All @@ -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
}
Expand Down
26 changes: 17 additions & 9 deletions schema/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ 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 {
col.SetType(&postgres.ArrayType{Type: &schema.StringType{T: "character varying", Size: 0}, T: "varchar[]"})
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 {
col.SetType(&schema.StringType{T: "character varying", Size: 0})
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)})
}
}
}
Expand All @@ -53,15 +59,15 @@ 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 {
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)})
}
}
}
Expand All @@ -70,15 +76,15 @@ 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":
col.SetType(&schema.BoolType{T: postgres.TypeBoolean})
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":
Expand All @@ -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
}
Expand Down
19 changes: 12 additions & 7 deletions schema/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,44 @@ 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 {
col.SetType(&schema.IntegerType{T: "integer"})
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":
col.SetType(&schema.BoolType{T: "boolean"})
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":
Expand All @@ -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
}
Expand Down