cartridge-java icon indicating copy to clipboard operation
cartridge-java copied to clipboard

Add check input data for insert, upsert, update operations according to space format

Open no1seman opened this issue 5 years ago • 0 comments

If anyone tries to insert null-value into a space field that is part of the primary index then tarantool runs into an error. In some cases, this can leads to fatal error, and exiting the event loop.

This situation can be initiated from the Java driver as well. So, it is necessary to add a check for input data, according space format and space indices before performing insert, upsert, update operations or/and make it in the safe way.

Here is an example:

local yaml = require('yaml')

local space_name = 'megaspace'

local initial_format = {
    {name = 'customer_id', type = 'string', is_nullable = false},
    {name = 'id', type = 'string', is_nullable = false},
}

local new_format = {
    {name = 'customer_id', type = 'string', is_nullable = false},
    {name = 'id', type = 'string', is_nullable = true},
}

local function init_space(space)
    print('init space: %s', space)
    local _space = box.schema.space.create(space, {if_not_exists = true})
    _space:format(initial_format)
    _space:create_index('pk', {unique = true, parts = {'id'}, if_not_exists = true})
    return _space
end

local function change_format(space)
    box.space[space]:format(new_format)
end

local function add_data(data)
    box.space[space_name]:insert(data)
end

box.cfg({})

--create space
local space = init_space(space_name)
print(yaml.encode(space:format()))

-- add some data
add_data(box.tuple.new({'VIP1', '1'}))
print(yaml.encode(space:select()))

-- change format
change_format(space_name)
print(yaml.encode(space:format()))

--add_data(box.tuple.new({'VIP2', box.NULL}))

no1seman avatar Dec 07 '20 09:12 no1seman