forked from tarantool/tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
box: loose truncate check in case of foreign key
In tarantool#7309 a truncation of a space that was referenced by foreign key from some other space was prohibited. It appeared that this solution is too bothering since a user can't truncate a space even if he truncated referring space before that. Fix it by allowing space truncate if referring spaces are empty. Also allow drop of the primary index in the same case with the same reason: logically the index along with all space data is not needed for consistency if there's no referring data. Note that by design space truncate is implemented quite similar to space drop. Both delete all indexes, from secondary to primary. Since this patch allows deletion of the primary index (which is the action that actually deletes all data from the space), this patch changes the result of space drop too: the space remains alive with no indexes, while before this patch it remained alive with no secondary indexes but with present primary. In both cases the behaviour is quite strange and must be fixed in tarantool#4348. To make tests pass I had to perform drop in box.atomic manually. Closes tarantool#8946 NO_DOC=bugfix (cherry picked from commit 983a7ec)
- Loading branch information
Showing
8 changed files
with
188 additions
and
11 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
changelogs/unreleased/gh-8946-foreign-key-disables-truncate.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## bugfix/core | ||
|
||
* Fixed a bug when a space that is referenced by a foreign key could not | ||
be truncated even if the referring space was empty (gh-8946). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
test/engine-luatest/gh_8946_foreign_key_disables_truncate_test.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
-- https://github.com/tarantool/tarantool/issues/8946 | ||
-- Test that a space that another empty space refers to can be truncated. | ||
local server = require('luatest.server') | ||
local t = require('luatest') | ||
|
||
local engines = {{engine = 'memtx'}, {engine = 'vinyl'}} | ||
local g = t.group('gh-8946-foreign-key-truncate-test', engines) | ||
|
||
g.before_all(function(cg) | ||
cg.server = server:new({alias = 'master'}) | ||
cg.server:start() | ||
end) | ||
|
||
g.after_all(function(cg) | ||
cg.server:stop() | ||
cg.server = nil | ||
end) | ||
|
||
g.after_each(function(cg) | ||
cg.server:exec(function() | ||
if box.space.client_phones then | ||
box.space.client_phones:drop() | ||
end | ||
if box.space.client then | ||
box.space.client:drop() | ||
end | ||
end) | ||
end) | ||
|
||
-- Field foreign key must not disable truncate if referring space is empty. | ||
g.test_field_foreign_key_truncate = function(cg) | ||
local engine = cg.params.engine | ||
|
||
cg.server:exec(function(engine) | ||
box.schema.space.create('client', {engine = engine}) | ||
box.space.client:format({ | ||
{name = 'customer_id', type = 'string', is_nullable = false}, | ||
{name = 'esia_id', type = 'string', is_nullable = false}, | ||
}) | ||
|
||
box.space.client:create_index('pk_client_customer_id', { | ||
parts = {{field = 'customer_id', collation = 'unicode'}}, | ||
type = 'tree', unique = true | ||
}) | ||
box.space.client:create_index('idx_client_esia_id', { | ||
parts = {{field = 'esia_id', collation = 'unicode'}}, | ||
type = 'tree', unique = false | ||
}) | ||
|
||
box.schema.space.create('client_phones', {engine = engine}) | ||
box.space.client_phones:format({ | ||
{name = 'phone', type = 'string', is_nullable = false}, | ||
{name = 'customer_id', | ||
foreign_key = {space = 'client', field = 'customer_id'}}, | ||
}) | ||
|
||
box.space.client_phones:create_index('idx_client_phones_phone', { | ||
parts = {{field = 'phone', collation = 'unicode'}}, | ||
type = 'tree', unique = true | ||
}) | ||
|
||
box.space.client:insert{'01','esia-01'} | ||
box.space.client:insert{'02','esia-02'} | ||
|
||
box.space.client_phones:insert{'9121234','01'} | ||
box.space.client_phones:insert{'3222222','02'} | ||
|
||
-- Now truncate is prohibited. | ||
t.assert_error_msg_content_equals( | ||
"Can't modify space 'client': space is referenced by foreign key", | ||
box.space.client.truncate, box.space.client) | ||
|
||
box.space.client_phones:truncate() | ||
|
||
-- Now truncate is allowed. | ||
box.space.client:truncate() | ||
end, {engine}) | ||
end | ||
|
||
-- Tuple foreign key must not disable truncate if referring space is empty. | ||
g.test_tuple_foreign_key_truncate = function(cg) | ||
local engine = cg.params.engine | ||
|
||
cg.server:exec(function(engine) | ||
box.schema.space.create('client', {engine = engine}) | ||
box.space.client:format({ | ||
{name = 'customer_id', type = 'string', is_nullable = false}, | ||
{name = 'esia_id', type = 'string', is_nullable = false}, | ||
}) | ||
|
||
box.space.client:create_index('pk_client_customer_id', { | ||
parts = {{field = 'customer_id', collation = 'unicode'}}, | ||
type = 'tree', unique = true | ||
}) | ||
box.space.client:create_index('idx_client_esia_id', { | ||
parts = {{field = 'esia_id', collation = 'unicode'}}, | ||
type = 'tree', unique = false | ||
}) | ||
|
||
box.schema.space.create('client_phones', { | ||
engine = engine, | ||
foreign_key = {space = 'client', | ||
field = {customer_id = 'customer_id'}} | ||
}) | ||
box.space.client_phones:format({ | ||
{name = 'phone', type = 'string', is_nullable = false}, | ||
{name = 'customer_id'}, | ||
}) | ||
|
||
box.space.client_phones:create_index('idx_client_phones_phone', { | ||
parts = {{field = 'phone', collation = 'unicode'}}, | ||
type = 'tree', unique = true | ||
}) | ||
|
||
box.space.client:insert{'01','esia-01'} | ||
box.space.client:insert{'02','esia-02'} | ||
|
||
box.space.client_phones:insert{'9121234','01'} | ||
box.space.client_phones:insert{'3222222','02'} | ||
|
||
-- Now truncate is prohibited. | ||
t.assert_error_msg_content_equals( | ||
"Can't modify space 'client': space is referenced by foreign key", | ||
box.space.client.truncate, box.space.client) | ||
|
||
box.space.client_phones:truncate() | ||
|
||
-- Now truncate is allowed. | ||
box.space.client:truncate() | ||
end, {engine}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters