dialect/entsql: add support for table checks in schema/migration

This commit is contained in:
Ariel Mashraki
2021-06-17 23:11:12 +03:00
committed by Ariel Mashraki
parent b8f4614bfd
commit 42a2c67cc4
11 changed files with 111 additions and 6 deletions

View File

@@ -204,6 +204,12 @@ var (
func init() {
CarsTable.ForeignKeys[0].RefTable = UsersTable
MediaTable.Annotation = &entsql.Annotation{
Check: "text <> 'boring'",
}
MediaTable.Annotation.Checks = map[string]string{
"boring_check": "source_uri <> 'entgo.io'",
}
PetsTable.ForeignKeys[0].RefTable = UsersTable
FriendsTable.ForeignKeys[0].RefTable = UsersTable
FriendsTable.ForeignKeys[1].RefTable = UsersTable

View File

@@ -7,6 +7,7 @@ package schema
import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
@@ -39,3 +40,14 @@ func (Media) Indexes() []ent.Index {
Annotations(entsql.Prefix(100)),
}
}
func (Media) Annotations() []schema.Annotation {
return []schema.Annotation{
&entsql.Annotation{
Check: "text <> 'boring'",
Checks: map[string]string{
"boring_check": "source_uri <> 'entgo.io'",
},
},
}
}

View File

@@ -45,6 +45,9 @@ func TestMySQL(t *testing.T) {
clientv1 := entv1.NewClient(entv1.Driver(drv))
clientv2 := entv2.NewClient(entv2.Driver(drv))
V1ToV2(t, drv.Dialect(), clientv1, clientv2)
if version == "8" {
CheckConstraint(t, clientv2)
}
})
}
}
@@ -73,6 +76,7 @@ func TestPostgres(t *testing.T) {
clientv1 := entv1.NewClient(entv1.Driver(drv))
clientv2 := entv2.NewClient(entv2.Driver(drv))
V1ToV2(t, drv.Dialect(), clientv1, clientv2)
CheckConstraint(t, clientv2)
})
}
}
@@ -101,6 +105,7 @@ func TestSQLite(t *testing.T) {
require.NoError(t, err)
EqualFold(t, client)
ContainsFold(t, client)
CheckConstraint(t, client)
}
func TestStorageKey(t *testing.T) {
@@ -294,6 +299,15 @@ func SanityV2(t *testing.T, dbdialect string, client *entv2.Client) {
}
}
func CheckConstraint(t *testing.T, client *entv2.Client) {
ctx := context.Background()
t.Log("testing check constraints")
_, err := client.Media.Create().SetText("boring").Save(ctx)
require.Error(t, err)
_, err = client.Media.Create().SetSourceURI("entgo.io").Save(ctx)
require.Error(t, err)
}
func EqualFold(t *testing.T, client *entv2.Client) {
ctx := context.Background()
t.Log("testing equal-fold on sql specific dialects")