Skip to content

Commit e919085

Browse files
authored
Add support for CHECK constraints in create_table operation (#588)
This PR adds a new type of constraints to `create_table` operation, named `check`. So from now, it is possible to configure table level check constraints. Available settings: * `check`: required, check expression * `no_inherit`: disable constraint inheritance in child tables (default: `false`) Example: ```json { "name": "50_create_table_with_table_constraint", "operations": [ { "create_table": { "name": "phonebook", "columns": [ { "name": "id", "type": "serial", "pk": true }, { "name": "name", "type": "varchar(255)" } ], "constraints": [ { "name": "name_must_be_present", "type": "check", "check": "length(name) > 0" } ] } } ] } ``` Part of #580
1 parent 9204268 commit e919085

23 files changed

+669
-19
lines changed

docs/operations/create_table.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ Each `constraint` is defined as:
4848
"name": "constraint name",
4949
"type": "constraint type",
5050
"columns": ["list", "of", "columns"],
51+
"check": "condition of CHECK constraint",
5152
"nulls_not_distinct": true|false,
5253
"deferrable": true|false,
5354
"initially_deferred": true|false,
55+
"no_inherit": true|false,
5456
"index_parameters": {
5557
"tablespace": "index_tablespace",
5658
"storage_parameters": "parameter=value",
@@ -59,7 +61,7 @@ Each `constraint` is defined as:
5961
},
6062
```
6163

62-
Supported constraint types: `unique`.
64+
Supported constraint types: `unique`, `check`.
6365

6466
## Examples
6567

@@ -120,7 +122,7 @@ Create a table with different `DEFAULT` values:
120122

121123
<ExampleSnippet example="28_different_defaults.json" language="json" />
122124

123-
### Create a table with table level unique constraint
125+
### Create a table with multiple table level constraints
124126

125127
Create a table with table level constraints:
126128

examples/50_create_table_with_table_constraint.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
"name"
3636
]
3737
}
38+
},
39+
{
40+
"name": "name_must_be_present",
41+
"type": "check",
42+
"check": "length(name) > 0"
3843
}
3944
]
4045
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/lib/pq v1.10.9
99
github.com/oapi-codegen/nullable v1.1.0
1010
github.com/pterm/pterm v0.12.80
11-
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
11+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1
1212
github.com/spf13/cobra v1.8.1
1313
github.com/spf13/viper v1.19.0
1414
github.com/stretchr/testify v1.10.0

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
4747
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4848
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
4949
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
50+
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
51+
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
5052
github.com/docker/docker v27.1.1+incompatible h1:hO/M4MtV36kzKldqnA37IWhebRA+LnqqcqDja6kVaKY=
5153
github.com/docker/docker v27.1.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
5254
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
@@ -168,8 +170,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
168170
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
169171
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
170172
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
171-
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
172-
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
173+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
174+
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
173175
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
174176
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
175177
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=

internal/jsonschema/jsonschema_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13-
"github.com/santhosh-tekuri/jsonschema/v5"
13+
"github.com/santhosh-tekuri/jsonschema/v6"
1414
"github.com/stretchr/testify/assert"
1515
"golang.org/x/tools/txtar"
1616
)
@@ -23,7 +23,9 @@ const (
2323
func TestJSONSchemaValidation(t *testing.T) {
2424
t.Parallel()
2525

26-
sch := jsonschema.MustCompile(schemaPath)
26+
compiler := jsonschema.NewCompiler()
27+
sch, err := compiler.Compile(schemaPath)
28+
assert.NoError(t, err)
2729

2830
files, err := os.ReadDir(testDataDir)
2931
assert.NoError(t, err)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is an invalid 'create_table' migration.
2+
Unique constraint must have list of columns configured
3+
4+
-- create_table.json --
5+
{
6+
"name": "migration_name",
7+
"operations": [
8+
{
9+
"create_table": {
10+
"name": "posts",
11+
"columns": [
12+
{
13+
"name": "title",
14+
"type": "varchar(255)"
15+
},
16+
{
17+
"name": "user_id",
18+
"type": "integer",
19+
"nullable": true
20+
}
21+
],
22+
"constraints": [
23+
{
24+
"name": "my_invalid_unique",
25+
"type": "unique"
26+
}
27+
]
28+
}
29+
}
30+
]
31+
}
32+
33+
-- valid --
34+
false
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
This is a valid 'create_table' migration.
2+
3+
-- create_table.json --
4+
{
5+
"name": "migration_name",
6+
"operations": [
7+
{
8+
"create_table": {
9+
"name": "posts",
10+
"columns": [
11+
{
12+
"name": "id",
13+
"type": "serial",
14+
"pk": true
15+
},
16+
{
17+
"name": "title",
18+
"type": "varchar(255)"
19+
},
20+
{
21+
"name": "user_id",
22+
"type": "integer",
23+
"nullable": true
24+
}
25+
],
26+
"constraints": [
27+
{
28+
"name": "my_check",
29+
"type": "check",
30+
"check": "lenth(title) > 30"
31+
}
32+
]
33+
}
34+
}
35+
]
36+
}
37+
38+
-- valid --
39+
true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is an invalid 'create_table' migration.
2+
Check constraint is missing the expression
3+
4+
-- create_table.json --
5+
{
6+
"name": "migration_name",
7+
"operations": [
8+
{
9+
"create_table": {
10+
"name": "posts",
11+
"columns": [
12+
{
13+
"name": "title",
14+
"type": "varchar(255)"
15+
},
16+
{
17+
"name": "user_id",
18+
"type": "integer",
19+
"nullable": true
20+
}
21+
],
22+
"constraints": [
23+
{
24+
"name": "my_invalid_check",
25+
"type": "check"
26+
}
27+
]
28+
}
29+
}
30+
]
31+
}
32+
33+
-- valid --
34+
false
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
This is a valid 'create_table' migration.
2+
3+
-- create_table.json --
4+
{
5+
"name": "migration_name",
6+
"operations": [
7+
{
8+
"create_table": {
9+
"name": "posts",
10+
"columns": [
11+
{
12+
"name": "title",
13+
"type": "varchar(255)"
14+
},
15+
{
16+
"name": "user_id",
17+
"type": "integer",
18+
"nullable": true
19+
}
20+
],
21+
"constraints": [
22+
{
23+
"name": "my_invalid_unique",
24+
"type": "unique",
25+
"columns": ["title"]
26+
}
27+
]
28+
}
29+
}
30+
]
31+
}
32+
33+
-- valid --
34+
true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This is an invalid 'create_table' migration.
2+
Check constraint is not deferrable
3+
4+
-- create_table.json --
5+
{
6+
"name": "migration_name",
7+
"operations": [
8+
{
9+
"create_table": {
10+
"name": "posts",
11+
"columns": [
12+
{
13+
"name": "title",
14+
"type": "varchar(255)"
15+
},
16+
{
17+
"name": "user_id",
18+
"type": "integer",
19+
"nullable": true
20+
}
21+
],
22+
"constraints": [
23+
{
24+
"name": "my_invalid_check",
25+
"type": "check",
26+
"check": "length(title) > 10",
27+
"deferrable": true
28+
}
29+
]
30+
}
31+
}
32+
]
33+
}
34+
35+
-- valid --
36+
false

0 commit comments

Comments
 (0)