Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 54c468c

Browse files
committed
Assert on several primary indexes
1 parent e08cb5c commit 54c468c

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

graphql/accessor_general.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,26 @@ local function get_primary_index_meta(self, collection_name)
112112
type(collection_name))
113113

114114
local indexes = self.indexes[collection_name]
115+
116+
local res_index_name
117+
115118
for index_name, index in pairs(indexes) do
116-
if index.primary then
117-
return index_name, index
119+
if res_index_name == nil and index.primary then
120+
res_index_name = index_name
121+
elseif res_index_name ~= nil and index.primary then
122+
error(('several indexes were marked as primary in ' ..
123+
'the "%s" collection, at least "%s" and "%s"'):format(
124+
collection_name, res_index_name, index_name))
118125
end
119126
end
120127

121-
error(('cannot find primary index for collection "%s"'):format(
122-
collection_name))
128+
if res_index_name == nil then
129+
error(('cannot find primary index for collection "%s"'):format(
130+
collection_name))
131+
end
132+
133+
local res_index = indexes[res_index_name]
134+
return res_index_name, res_index
123135
end
124136

125137
--- Get a key to lookup index by `lookup_index_name` (part of `index_cache`).

test/local/init_fail.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
INIT: ok: false; err: 1:1 connection "user_connection" of collection "order_collection" has less fields than the index of "user_str_num_index" collection (cannot prove uniqueness of the partial index)
22
INIT: ok: true; type(res): table
3+
INIT: ok: false; err: several indexes were marked as primary in the "user_collection" collection, at least "user_str_index" and "user_str_num_index"

test/local/init_fail.test.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ local function create_gql_wrapper(metadata)
5454
collections = metadata.collections,
5555
accessor = accessor,
5656
})
57-
5857
end
5958

6059
local ok, err = pcall(create_gql_wrapper, metadata)
@@ -66,6 +65,24 @@ metadata.collections.order_collection.connections[1].parts[2] = saved_part
6665
local ok, res = pcall(create_gql_wrapper, metadata)
6766
print(('INIT: ok: %s; type(res): %s'):format(tostring(ok), type(res)))
6867

68+
-- multiple primary indexes
69+
-- ------------------------
70+
71+
-- inject an error into the metadata
72+
metadata.indexes.user_collection.user_str_index = {
73+
service_fields = {},
74+
fields = {'user_str'},
75+
index_type = 'tree',
76+
unique = true,
77+
primary = true,
78+
}
79+
80+
local ok, err = pcall(create_gql_wrapper, metadata)
81+
print(('INIT: ok: %s; err: %s'):format(tostring(ok), strip_error(err)))
82+
83+
-- restore metadata back
84+
metadata.indexes.user_collection.user_str_index = nil
85+
6986
-- clean up
7087
-- --------
7188

0 commit comments

Comments
 (0)