[tarantool-patches] [PATCH v2] Return valid lua error for func creation error

Georgy Kirichenko georgy at tarantool.org
Wed Nov 7 10:40:35 MSK 2018


Return valid lua error if something fails during sql function creation.

Closes #3724
---
https://github.com/tarantool/tarantool/tree/g.kirichenko/gh-3724-return-error-from-func-creation
https://github.com/tarantool/tarantool/issues/3724

Changes in v2:
  - Rebased against latest 2.1

 src/box/lua/lua_sql.c           | 10 +++++----
 test/sql/func-recreate.result   | 36 +++++++++++++++++++++++++++++++++
 test/sql/func-recreate.test.lua | 17 ++++++++++++++++
 3 files changed, 59 insertions(+), 4 deletions(-)
 create mode 100644 test/sql/func-recreate.result
 create mode 100644 test/sql/func-recreate.test.lua

diff --git a/src/box/lua/lua_sql.c b/src/box/lua/lua_sql.c
index f9e63aae6..5c77b8036 100644
--- a/src/box/lua/lua_sql.c
+++ b/src/box/lua/lua_sql.c
@@ -188,9 +188,11 @@ lbox_sql_create_function(struct lua_State *L)
 	if (func_info == NULL)
 		return luaL_error(L, "out of memory");
 	func_info->func_ref = luaL_ref(L, LUA_REGISTRYINDEX);
-	sqlite3_create_function_v2(db, normalized_name, type, func_arg_num,
-				   is_deterministic ? SQLITE_DETERMINISTIC : 0,
-				   func_info, lua_sql_call, NULL, NULL,
-				   lua_sql_destroy);
+	int rc = sqlite3_create_function_v2(db, normalized_name, type, func_arg_num,
+					   is_deterministic ? SQLITE_DETERMINISTIC : 0,
+					   func_info, lua_sql_call, NULL, NULL,
+					   lua_sql_destroy);
+	if (rc != 0)
+		return luaL_error(L, sqlite3ErrStr(rc));
 	return 0;
 }
diff --git a/test/sql/func-recreate.result b/test/sql/func-recreate.result
new file mode 100644
index 000000000..22e169bdf
--- /dev/null
+++ b/test/sql/func-recreate.result
@@ -0,0 +1,36 @@
+test_run = require('test_run').new()
+---
+...
+engine = test_run:get_cfg('engine')
+---
+...
+box.sql.execute('pragma sql_default_engine=\''..engine..'\'')
+---
+...
+-- Check errors during function create process
+fiber = require('fiber')
+---
+...
+box.internal.sql_create_function('WAITFOR', 'INT', function (n) fiber.sleep(n) return n end)
+---
+...
+ch = fiber.channel(1)
+---
+...
+_ = fiber.create(function () ch:put(box.sql.execute('select WAITFOR(0.2)')) end)
+---
+...
+fiber.sleep(0.1)
+---
+...
+box.internal.sql_create_function('WAITFOR', 'INT', function (n) require('fiber').sleep(n) return n end)
+---
+- error: database is locked
+...
+ch:get()
+---
+- - [0.2]
+...
+box.internal.sql_create_function('WAITFOR', 'INT', function (n) require('fiber').sleep(n) return n end)
+---
+...
diff --git a/test/sql/func-recreate.test.lua b/test/sql/func-recreate.test.lua
new file mode 100644
index 000000000..b4e1fbe62
--- /dev/null
+++ b/test/sql/func-recreate.test.lua
@@ -0,0 +1,17 @@
+test_run = require('test_run').new()
+engine = test_run:get_cfg('engine')
+box.sql.execute('pragma sql_default_engine=\''..engine..'\'')
+
+-- Check errors during function create process
+fiber = require('fiber')
+box.internal.sql_create_function('WAITFOR', 'INT', function (n) fiber.sleep(n) return n end)
+
+ch = fiber.channel(1)
+
+_ = fiber.create(function () ch:put(box.sql.execute('select WAITFOR(0.2)')) end)
+fiber.sleep(0.1)
+
+box.internal.sql_create_function('WAITFOR', 'INT', function (n) require('fiber').sleep(n) return n end)
+ch:get()
+box.internal.sql_create_function('WAITFOR', 'INT', function (n) require('fiber').sleep(n) return n end)
+
-- 
2.19.1





More information about the Tarantool-patches mailing list