mirror of
https://github.com/ent/ent.git
synced 2026-05-03 08:00:58 +03:00
dialect/entsql: add support for table checks in schema/migration
This commit is contained in:
committed by
Ariel Mashraki
parent
b8f4614bfd
commit
42a2c67cc4
@@ -7,6 +7,7 @@ package schema
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -542,3 +543,33 @@ func compare(v1, v2 int) int {
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// addChecks appends the CHECK clauses from the entsql.Annotation.
|
||||
func addChecks(t *sql.TableBuilder, ant *entsql.Annotation) {
|
||||
if check := ant.Check; check != "" {
|
||||
t.Checks(func(b *sql.Builder) {
|
||||
b.WriteString("CHECK " + checkExpr(check))
|
||||
})
|
||||
}
|
||||
if checks := ant.Checks; len(ant.Checks) > 0 {
|
||||
names := make([]string, 0, len(checks))
|
||||
for name := range checks {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
t.Checks(func(b *sql.Builder) {
|
||||
b.WriteString("CONSTRAINT ").Ident(name).WriteString(" CHECK " + checkExpr(checks[name]))
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// checkExpr formats the CHECK expression.
|
||||
func checkExpr(expr string) string {
|
||||
expr = strings.TrimSpace(expr)
|
||||
if !strings.HasPrefix(expr, "(") && !strings.HasSuffix(expr, ")") {
|
||||
expr = "(" + expr + ")"
|
||||
}
|
||||
return expr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user