From 4f3e445ad1b36112b41aa6f581bd2f82d8b53f5f Mon Sep 17 00:00:00 2001
From: Roman Tsisyk <roman@tsisyk.com>
Date: Fri, 23 May 2014 16:46:40 +0400
Subject: [PATCH] Fix #197: box.space.space0:len() returns an error if there is
 no index

---
 src/box/lua/schema.lua       | 14 +++++++++++++-
 test/box/alter.result        | 17 +++++++++++++++++
 test/box/alter.test.lua      |  9 +++++++++
 test/box/alter_limits.result |  1 -
 4 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua
index 00346e1cfb..3e8fdd7976 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -438,7 +438,12 @@ function box.schema.space.bless(space)
     end
     --
     local space_mt = {}
-    space_mt.len = function(space) return space.index[0]:len() end
+    space_mt.len = function(space)
+        if space.index[0] == nil then
+            return 0 -- empty space without indexes, return 0
+        end
+        return space.index[0]:len()
+    end
     space_mt.__newindex = index_mt.__newindex
 
     space_mt.get = function(space, key)
@@ -477,12 +482,19 @@ function box.schema.space.bless(space)
         return space:insert(tuple)
     end
     space_mt.pairs = function(space, key)
+        if space.index[0] == nil then
+            -- empty space without indexes, return empty iterator
+            return fun.iter({})
+        end
         check_index(space, 0)
         return space.index[0]:pairs(key)
     end
     space_mt.__pairs = space_mt.pairs -- Lua 5.2 compatibility
     space_mt.__ipairs = space_mt.pairs -- Lua 5.2 compatibility
     space_mt.truncate = function(space)
+        if space.index[0] == nil then
+            return -- empty space without indexes, nothing to truncate
+        end
         check_index(space, 0)
         local pk = space.index[0]
         while #pk.idx > 0 do
diff --git a/test/box/alter.result b/test/box/alter.result
index 744100e6d9..ce36bb7e73 100644
--- a/test/box/alter.result
+++ b/test/box/alter.result
@@ -227,3 +227,20 @@ box.space[1000]
 ---
 - null
 ...
+--------------------------------------------------------------------------------
+-- #197: box.space.space0:len() returns an error if there is no index
+--------------------------------------------------------------------------------
+space = box.schema.create_space('gh197')
+---
+...
+space:len()
+---
+- 0
+...
+space:truncate()
+---
+...
+space:pairs():totable()
+---
+- []
+...
diff --git a/test/box/alter.test.lua b/test/box/alter.test.lua
index 940e42bec1..088ad97164 100644
--- a/test/box/alter.test.lua
+++ b/test/box/alter.test.lua
@@ -80,3 +80,12 @@ box.space['_space']:insert{1000, ADMIN, 'test', 'memtx', 0}
 box.space[1000].n
 box.space['_space']:delete{1000}
 box.space[1000]
+
+--------------------------------------------------------------------------------
+-- #197: box.space.space0:len() returns an error if there is no index
+--------------------------------------------------------------------------------
+
+space = box.schema.create_space('gh197')
+space:len()
+space:truncate()
+space:pairs():totable()
diff --git a/test/box/alter_limits.result b/test/box/alter_limits.result
index 4f9eeaa970..ef1ef5a650 100644
--- a/test/box/alter_limits.result
+++ b/test/box/alter_limits.result
@@ -159,7 +159,6 @@ s.index[0]
 ...
 s:truncate()
 ---
-- error: 'No index #0 is defined in space 512'
 ...
 s.enabled
 ---
-- 
GitLab