Skip to content
Merged
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
62 changes: 62 additions & 0 deletions doc/code_snippets/test/constraints/constraint_sql_expr_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local fio = require('fio')
local server = require('luatest.server')
local t = require('luatest')
local g = t.group()
g.before_each(function(cg)
cg.server = server:new {
box_cfg = {},
workdir = fio.cwd() .. '/tmp'
}
cg.server:start()
end)

g.after_each(function(cg)
cg.server:stop()
cg.server:drop()
end)

g.test_constraints = function(cg)
cg.server:exec(function()
-- create_constraint_start
box.schema.func.create('check_person', {
language = 'SQL_EXPR',
is_deterministic = true,
body = [["age" > 21 AND "name" != 'Admin']]
})
-- create_constraint_end

-- configure_space_start
local customers = box.schema.space.create('customers', { constraint = 'check_person' })
customers:format({
{ name = 'id', type = 'number' },
{ name = 'name', type = 'string' },
{ name = 'age', type = 'number' },
})
customers:create_index('primary', { parts = { 1 } })
-- configure_space_end

-- insert_ok_start
customers:insert { 1, "Alice", 30 }
-- insert_ok_end

local _, age_err = pcall(function()
-- insert_age_error_start
customers:insert { 2, "Bob", 18 }
-- error: Check constraint 'check_person' failed for tuple
-- insert_age_error_end
end)

local _, name_err = pcall(function()
-- insert_name_error_start
customers:insert { 3, "Admin", 25 }
-- error: Check constraint 'check_person' failed for tuple
-- insert_name_error_end
end)

-- Tests --
t.assert_equals(customers:count(), 1)
t.assert_equals(customers:get(1), { 1, "Alice", 30 })
t.assert_equals(age_err:unpack().message, 'Check constraint \'check_person\' failed for tuple')
t.assert_equals(name_err:unpack().message, 'Check constraint \'check_person\' failed for tuple')
end)
end
4 changes: 2 additions & 2 deletions doc/concepts/data_model/value_store.rst
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ a wider range of limitations.
Constraint functions
~~~~~~~~~~~~~~~~~~~~

Constraints use stored Lua functions, which must return ``true`` when the constraint
Constraints use stored Lua functions or :ref:`SQL expressions <sql_expressions>`, which must return ``true`` when the constraint
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's worth mentioning that the Lua and SQL_EXPR functions can be used as constraints. For example, C functions can also be used as constraints. Maybe not mention it here?

is satisfied. Other return values (including ``nil``) and exceptions make the
check fail and prevent tuple insertion or modification.

To create a constraint function, use :ref:`func.create with function body <box_schema-func_create_with-body>`.
To create a constraint function, call :ref:`box.schema.func.create() <box_schema-func_create>` with the function definition specified in the ``body`` attribute.

Constraint functions take two parameters:

Expand Down
Loading