From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 3F0322B51B for ; Fri, 29 Mar 2019 14:24:31 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id TQueCWbf73qz for ; Fri, 29 Mar 2019 14:24:31 -0400 (EDT) Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id E85E62B515 for ; Fri, 29 Mar 2019 14:24:30 -0400 (EDT) From: Nikita Pettik Subject: [tarantool-patches] [PATCH 2/4] Fix creation of FK constraint in case of no child's PK Date: Fri, 29 Mar 2019 21:24:22 +0300 Message-Id: <20935e8a64d7056a0316b71a933521567705be26.1553883575.git.korablev@tarantool.org> In-Reply-To: References: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: v.shpilevoy@tarantool.org, Nikita Pettik on_replace_dd_fk_constraint() assumes that child's primary index exists: it is used to verify emptiness of space invoking index_size(). This commit fixes this obvious bug which could lead to crash. --- src/box/alter.cc | 2 +- test/sql/foreign-keys.result | 25 +++++++++++++++++++++++++ test/sql/foreign-keys.test.lua | 10 ++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/box/alter.cc b/src/box/alter.cc index 40b32eafe..2f783c8e7 100644 --- a/src/box/alter.cc +++ b/src/box/alter.cc @@ -3901,7 +3901,7 @@ on_replace_dd_fk_constraint(struct trigger * /* trigger*/, void *event) * checks on existing data in space. */ struct index *pk = space_index(child_space, 0); - if (index_size(pk) > 0) { + if (pk != NULL && index_size(pk) > 0) { tnt_raise(ClientError, ER_CREATE_FK_CONSTRAINT, fk_def->name, "referencing space must be empty"); diff --git a/test/sql/foreign-keys.result b/test/sql/foreign-keys.result index 731807227..97c3e2442 100644 --- a/test/sql/foreign-keys.result +++ b/test/sql/foreign-keys.result @@ -385,5 +385,30 @@ box.sql.execute("CREATE TABLE t4 (id INT PRIMARY KEY REFERENCES t4);") box.space.T4:drop() --- ... +-- Make sure that child space can feature no PK. +-- +t1 = box.schema.create_space("T1") +--- +... +box.space.T1:format({'ID'}) +--- +... +t2 = box.schema.create_space("T2") +--- +... +i1 = box.space.T2:create_index('I1') +--- +... +box.sql.execute("ALTER TABLE t1 ADD CONSTRAINT fk FOREIGN KEY (id) REFERENCES t2;") +--- +- error: 'Failed to create foreign key constraint ''FK'': foreign key refers to nonexistent + field' +... +t1:drop() +--- +... +t2:drop() +--- +... --- Clean-up SQL DD hash. -test_run:cmd('restart server default with cleanup=1') diff --git a/test/sql/foreign-keys.test.lua b/test/sql/foreign-keys.test.lua index 6f4941e09..078c10c54 100644 --- a/test/sql/foreign-keys.test.lua +++ b/test/sql/foreign-keys.test.lua @@ -174,5 +174,15 @@ box.space.T1:drop() box.sql.execute("CREATE TABLE t4 (id INT PRIMARY KEY REFERENCES t4);") box.space.T4:drop() +-- Make sure that child space can feature no PK. +-- +t1 = box.schema.create_space("T1") +box.space.T1:format({'ID'}) +t2 = box.schema.create_space("T2") +i1 = box.space.T2:create_index('I1') +box.sql.execute("ALTER TABLE t1 ADD CONSTRAINT fk FOREIGN KEY (id) REFERENCES t2;") +t1:drop() +t2:drop() + --- Clean-up SQL DD hash. -test_run:cmd('restart server default with cleanup=1') -- 2.15.1