Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org, Nikita Pettik <korablev@tarantool.org>
Subject: [tarantool-patches] [PATCH 1/4] Drop foreign keys before indexes in space:drop()
Date: Fri, 29 Mar 2019 21:24:21 +0300	[thread overview]
Message-ID: <d61f624be3a40e0b6c68e87f0fd8601bdd097359.1553883575.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1553883575.git.korablev@tarantool.org>
In-Reply-To: <cover.1553883575.git.korablev@tarantool.org>

Implicitly involved in foreign key constraint index can't be dropped
before constraint itself. So, to drop self-referenced table, we should
firstly drop foreign key constraints, and only then indexes.
---
 src/box/lua/schema.lua         | 6 +++---
 test/sql/foreign-keys.result   | 8 ++++++++
 test/sql/foreign-keys.test.lua | 5 +++++
 3 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua
index d9483a6b2..f31cf7f2c 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -521,14 +521,14 @@ box.schema.space.drop = function(space_id, space_name, opts)
     for _, t in _trigger.index.space_id:pairs({space_id}) do
         _trigger:delete({t.name})
     end
+    for _, t in _fk_constraint.index.child_id:pairs({space_id}) do
+        _fk_constraint:delete({t.name, space_id})
+    end
     local keys = _vindex:select(space_id)
     for i = #keys, 1, -1 do
         local v = keys[i]
         _index:delete{v[1], v[2]}
     end
-    for _, t in _fk_constraint.index.child_id:pairs({space_id}) do
-        _fk_constraint:delete({t.name, space_id})
-    end
     revoke_object_privs('space', space_id)
     _truncate:delete{space_id}
     if _space:delete{space_id} == nil then
diff --git a/test/sql/foreign-keys.result b/test/sql/foreign-keys.result
index 3c6464e7d..731807227 100644
--- a/test/sql/foreign-keys.result
+++ b/test/sql/foreign-keys.result
@@ -377,5 +377,13 @@ box.sql.execute('CREATE TABLE t2 (id INT PRIMARY KEY REFERENCES t2 ON DELETE CAS
 box.space.T1:drop()
 ---
 ...
+-- Make sure that space:drop() works fine on self-referenced spaces.
+--
+box.sql.execute("CREATE TABLE t4 (id INT PRIMARY KEY REFERENCES t4);")
+---
+...
+box.space.T4: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 3fb7cab18..6f4941e09 100644
--- a/test/sql/foreign-keys.test.lua
+++ b/test/sql/foreign-keys.test.lua
@@ -169,5 +169,10 @@ box.sql.execute('CREATE TABLE t2 (id INT PRIMARY KEY REFERENCES t2 ON DELETE CAS
 box.sql.execute('CREATE TABLE t2 (id INT PRIMARY KEY REFERENCES t2 ON DELETE CASCADE MATCH FULL);')
 box.space.T1:drop()
 
+-- Make sure that space:drop() works fine on self-referenced spaces.
+--
+box.sql.execute("CREATE TABLE t4 (id INT PRIMARY KEY REFERENCES t4);")
+box.space.T4:drop()
+
 --- Clean-up SQL DD hash.
 -test_run:cmd('restart server default with cleanup=1')
-- 
2.15.1

  reply	other threads:[~2019-03-29 18:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-29 18:24 [tarantool-patches] [PATCH 0/4] Fixes in SQL involving no-pk or no-format spaces Nikita Pettik
2019-03-29 18:24 ` Nikita Pettik [this message]
2019-03-29 18:24 ` [tarantool-patches] [PATCH 2/4] Fix creation of FK constraint in case of no child's PK Nikita Pettik
2019-04-01 13:41   ` [tarantool-patches] " Vladislav Shpilevoy
2019-03-29 18:24 ` [tarantool-patches] [PATCH 3/4] sql: disallow creation of index on space without format Nikita Pettik
2019-03-29 18:24 ` [tarantool-patches] [PATCH 4/4] sql: disallow creation of FK referencing space without PK Nikita Pettik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d61f624be3a40e0b6c68e87f0fd8601bdd097359.1553883575.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH 1/4] Drop foreign keys before indexes in space:drop()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox