mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
dialect/sql: allow adding check clauses in create table
This commit is contained in:
committed by
Ariel Mashraki
parent
754b56856a
commit
b8f4614bfd
@@ -107,14 +107,15 @@ func (c *ColumnBuilder) Query() (string, []interface{}) {
|
|||||||
// TableBuilder is a query builder for `CREATE TABLE` statement.
|
// TableBuilder is a query builder for `CREATE TABLE` statement.
|
||||||
type TableBuilder struct {
|
type TableBuilder struct {
|
||||||
Builder
|
Builder
|
||||||
name string // table name.
|
name string // table name.
|
||||||
exists bool // check existence.
|
exists bool // check existence.
|
||||||
charset string // table charset.
|
charset string // table charset.
|
||||||
collation string // table collation.
|
collation string // table collation.
|
||||||
options string // table options.
|
options string // table options.
|
||||||
columns []Querier // table columns.
|
columns []Querier // table columns.
|
||||||
primary []string // primary key.
|
primary []string // primary key.
|
||||||
constraints []Querier // foreign keys and indices.
|
constraints []Querier // foreign keys and indices.
|
||||||
|
checks []func(*Builder) // check constraints.
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTable returns a query builder for the `CREATE TABLE` statement.
|
// CreateTable returns a query builder for the `CREATE TABLE` statement.
|
||||||
@@ -177,6 +178,12 @@ func (t *TableBuilder) Constraints(fks ...*ForeignKeyBuilder) *TableBuilder {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checks adds CHECK clauses to the CREATE TABLE statement.
|
||||||
|
func (t *TableBuilder) Checks(checks ...func(*Builder)) *TableBuilder {
|
||||||
|
t.checks = append(t.checks, checks...)
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
// Charset appends the `CHARACTER SET` clause to the statement. MySQL only.
|
// Charset appends the `CHARACTER SET` clause to the statement. MySQL only.
|
||||||
func (t *TableBuilder) Charset(s string) *TableBuilder {
|
func (t *TableBuilder) Charset(s string) *TableBuilder {
|
||||||
t.charset = s
|
t.charset = s
|
||||||
@@ -218,6 +225,9 @@ func (t *TableBuilder) Query() (string, []interface{}) {
|
|||||||
if len(t.constraints) > 0 {
|
if len(t.constraints) > 0 {
|
||||||
b.Comma().JoinComma(t.constraints...)
|
b.Comma().JoinComma(t.constraints...)
|
||||||
}
|
}
|
||||||
|
for _, check := range t.checks {
|
||||||
|
check(b.Comma())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
if t.charset != "" {
|
if t.charset != "" {
|
||||||
t.WriteString(" CHARACTER SET " + t.charset)
|
t.WriteString(" CHARACTER SET " + t.charset)
|
||||||
|
|||||||
@@ -86,8 +86,11 @@ func TestBuilder(t *testing.T) {
|
|||||||
).
|
).
|
||||||
PrimaryKey("id", "name").
|
PrimaryKey("id", "name").
|
||||||
ForeignKeys(ForeignKey().Columns("card_id").
|
ForeignKeys(ForeignKey().Columns("card_id").
|
||||||
Reference(Reference().Table("cards").Columns("id")).OnDelete("SET NULL")),
|
Reference(Reference().Table("cards").Columns("id")).OnDelete("SET NULL")).
|
||||||
wantQuery: "CREATE TABLE IF NOT EXISTS `users`(`id` int auto_increment, `card_id` int, `doc` longtext CHECK (JSON_VALID(`doc`)), PRIMARY KEY(`id`, `name`), FOREIGN KEY(`card_id`) REFERENCES `cards`(`id`) ON DELETE SET NULL)",
|
Checks(func(b *Builder) {
|
||||||
|
b.WriteString("CONSTRAINT ").Ident("valid_card").WriteString(" CHECK (").Ident("card_id").WriteString(" > 0)")
|
||||||
|
}),
|
||||||
|
wantQuery: "CREATE TABLE IF NOT EXISTS `users`(`id` int auto_increment, `card_id` int, `doc` longtext CHECK (JSON_VALID(`doc`)), PRIMARY KEY(`id`, `name`), FOREIGN KEY(`card_id`) REFERENCES `cards`(`id`) ON DELETE SET NULL, CONSTRAINT `valid_card` CHECK (`card_id` > 0))",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: Dialect(dialect.Postgres).CreateTable("users").
|
input: Dialect(dialect.Postgres).CreateTable("users").
|
||||||
|
|||||||
Reference in New Issue
Block a user