[Tarantool-patches] [PATCH] box: disallow to modify format of a view
Roman Khabibov
roman.habibov at tarantool.org
Tue Sep 22 18:59:03 MSK 2020
Ban ability to modify format or field count of a view. Since a
view is a named select, and not a space, in fact, changing view
format is not a valid operation.
---
Branch: https://github.com/tarantool/tarantool/tree/romanhabibov/ban-view-mod
@ChangeLog
- Ban ability to modify view's format and field count.
src/box/alter.cc | 40 +++++++++++++
test/box/view-dont-modify-format.result | 69 +++++++++++++++++++++++
test/box/view-dont-modify-format.test.lua | 29 ++++++++++
3 files changed, 138 insertions(+)
create mode 100644 test/box/view-dont-modify-format.result
create mode 100644 test/box/view-dont-modify-format.test.lua
diff --git a/src/box/alter.cc b/src/box/alter.cc
index ba96d9c62..4d0da347a 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -2086,6 +2086,32 @@ update_view_references(struct Select *select, int update_value,
return 0;
}
+/**
+ * Check whether space format is changed during UPDATE/REPLACE.
+ *
+ * Used to disallow view's format modification.
+ *
+ * @param old_tuple Tuple of a space before UPDATE/REPLACE.
+ * @param new_tuple Tuple of a space after UPDATE/REPLACE.
+ */
+static bool
+format_is_changed(struct tuple *old_tuple, struct tuple *new_tuple)
+{
+ const char *old_format = tuple_field(old_tuple, BOX_SPACE_FIELD_FORMAT);
+ const char *new_format = tuple_field(new_tuple, BOX_SPACE_FIELD_FORMAT);
+ assert(old_format != NULL && new_format != NULL);
+ const char *end = old_format;
+ mp_next(&end);
+ uint32_t length = end - old_format;
+ end = new_format;
+ mp_next(&end);
+ if (length != end - new_format)
+ return true;
+ if (memcmp(old_format, new_format, length) != 0)
+ return true;
+ return false;
+}
+
/**
* Trigger which is fired to commit creation of new SQL view.
* Its purpose is to release memory of SELECT.
@@ -2441,6 +2467,20 @@ on_replace_dd_space(struct trigger * /* trigger */, void *event)
"view");
return -1;
}
+ if (def->opts.is_view && old_space->def->field_count !=
+ def->field_count) {
+ diag_set(ClientError, ER_ALTER_SPACE,
+ space_name(old_space),
+ "can not modify field count of a view");
+ return -1;
+ }
+ if (def->opts.is_view && format_is_changed(old_tuple,
+ new_tuple)) {
+ diag_set(ClientError, ER_ALTER_SPACE,
+ space_name(old_space),
+ "can not modify format of a view");
+ return -1;
+ }
/*
* Allow change of space properties, but do it
* in WAL-error-safe mode.
diff --git a/test/box/view-dont-modify-format.result b/test/box/view-dont-modify-format.result
new file mode 100644
index 000000000..0d8c50601
--- /dev/null
+++ b/test/box/view-dont-modify-format.result
@@ -0,0 +1,69 @@
+-- test-run result file version 2
+--
+-- Make sure we can't modify a view format.
+--
+box.execute("CREATE TABLE t1 (a INT PRIMARY KEY);")
+ | ---
+ | - row_count: 1
+ | ...
+box.execute("CREATE VIEW v AS SELECT * FROM t1;")
+ | ---
+ | - row_count: 1
+ | ...
+
+--
+-- Try to add a field.
+--
+view = box.space._space.index[2]:select('V')[1]:totable()
+ | ---
+ | ...
+view_format = box.space.V:format()
+ | ---
+ | ...
+f = {type = 'string', nullable_action = 'none', name = 'B', is_nullable = true}
+ | ---
+ | ...
+table.insert(view_format, f)
+ | ---
+ | ...
+view[5] = 2
+ | ---
+ | ...
+view[7] = view_format
+ | ---
+ | ...
+box.space._space:replace(view)
+ | ---
+ | - error: 'Can''t modify space ''V'': can not modify field count of a view'
+ | ...
+
+--
+-- Try to modify format only.
+--
+view = box.space.V
+ | ---
+ | ...
+view:format{}
+ | ---
+ | - error: 'Can''t modify space ''V'': can not modify field count of a view'
+ | ...
+
+view_format = box.space.V:format()
+ | ---
+ | ...
+view_format[1].name = 'B'
+ | ---
+ | ...
+view:format(view_format)
+ | ---
+ | - error: 'Can''t modify space ''V'': can not modify format of a view'
+ | ...
+
+box.execute("DROP VIEW v;")
+ | ---
+ | - row_count: 1
+ | ...
+box.execute("DROP TABLE t1;")
+ | ---
+ | - row_count: 1
+ | ...
diff --git a/test/box/view-dont-modify-format.test.lua b/test/box/view-dont-modify-format.test.lua
new file mode 100644
index 000000000..e553e7012
--- /dev/null
+++ b/test/box/view-dont-modify-format.test.lua
@@ -0,0 +1,29 @@
+--
+-- Make sure we can't modify a view format.
+--
+box.execute("CREATE TABLE t1 (a INT PRIMARY KEY);")
+box.execute("CREATE VIEW v AS SELECT * FROM t1;")
+
+--
+-- Try to add a field.
+--
+view = box.space._space.index[2]:select('V')[1]:totable()
+view_format = box.space.V:format()
+f = {type = 'string', nullable_action = 'none', name = 'B', is_nullable = true}
+table.insert(view_format, f)
+view[5] = 2
+view[7] = view_format
+box.space._space:replace(view)
+
+--
+-- Try to modify format only.
+--
+view = box.space.V
+view:format{}
+
+view_format = box.space.V:format()
+view_format[1].name = 'B'
+view:format(view_format)
+
+box.execute("DROP VIEW v;")
+box.execute("DROP TABLE t1;")
--
2.24.3 (Apple Git-128)
More information about the Tarantool-patches
mailing list