|
| 1 | +local checks = require('checks') |
| 2 | +local errors = require('errors') |
| 3 | + |
| 4 | +local SchemaError = errors.new_class('SchemaError', {capture_stack = false}) |
| 5 | + |
| 6 | +local schema_module = require('crud.common.schema') |
| 7 | +local utils = require('crud.common.utils') |
| 8 | + |
| 9 | +local schema = {} |
| 10 | + |
| 11 | +local system_spaces = { |
| 12 | + -- https://github.com/tarantool/tarantool/blob/3240201a2f5bac3bddf8a74015db9b351954e0b5/src/box/schema_def.h#L77-L127 |
| 13 | + ['_vinyl_deferred_delete'] = true, |
| 14 | + ['_schema'] = true, |
| 15 | + ['_collation'] = true, |
| 16 | + ['_vcollation'] = true, |
| 17 | + ['_space'] = true, |
| 18 | + ['_vspace'] = true, |
| 19 | + ['_sequence'] = true, |
| 20 | + ['_sequence_data'] = true, |
| 21 | + ['_vsequence'] = true, |
| 22 | + ['_index'] = true, |
| 23 | + ['_vindex'] = true, |
| 24 | + ['_func'] = true, |
| 25 | + ['_vfunc'] = true, |
| 26 | + ['_user'] = true, |
| 27 | + ['_vuser'] = true, |
| 28 | + ['_priv'] = true, |
| 29 | + ['_vpriv'] = true, |
| 30 | + ['_cluster'] = true, |
| 31 | + ['_trigger'] = true, |
| 32 | + ['_truncate'] = true, |
| 33 | + ['_space_sequence'] = true, |
| 34 | + ['_vspace_sequence'] = true, |
| 35 | + ['_fk_constraint'] = true, |
| 36 | + ['_ck_constraint'] = true, |
| 37 | + ['_func_index'] = true, |
| 38 | + ['_session_settings'] = true, |
| 39 | + -- https://github.com/tarantool/vshard/blob/b3c27b32637863e9a03503e641bb7c8c69779a00/vshard/storage/init.lua#L752 |
| 40 | + ['_bucket'] = true, |
| 41 | + -- https://github.com/tarantool/ddl/blob/b55d0ff7409f32e4d527e2d25444d883bce4163b/test/set_sharding_metadata_test.lua#L92-L98 |
| 42 | + ['_ddl_sharding_key'] = true, |
| 43 | + ['_ddl_sharding_func'] = true, |
| 44 | +} |
| 45 | + |
| 46 | +local function get_crud_schema(space) |
| 47 | + local sch = schema_module.get_normalized_space_schema(space) |
| 48 | + |
| 49 | + -- bucket_id is not nullable for a storage, yet |
| 50 | + -- it is optional for a crud user. |
| 51 | + for _, v in ipairs(sch.format) do |
| 52 | + if v.name == 'bucket_id' then |
| 53 | + v.is_nullable = true |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + for id, v in pairs(sch.indexes) do |
| 58 | + -- There is no reason for a user to know about |
| 59 | + -- bucket_id index. |
| 60 | + if v.name == 'bucket_id' then |
| 61 | + sch.indexes[id] = nil |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + return sch |
| 66 | +end |
| 67 | + |
| 68 | +schema.call = function(space_name, opts) |
| 69 | + checks('?string', { |
| 70 | + vshard_router = '?string|table', |
| 71 | + timeout = '?number', |
| 72 | + }) |
| 73 | + |
| 74 | + opts = opts or {} |
| 75 | + |
| 76 | + local vshard_router, err = utils.get_vshard_router_instance(opts.vshard_router) |
| 77 | + if err ~= nil then |
| 78 | + return nil, SchemaError:new(err) |
| 79 | + end |
| 80 | + |
| 81 | + local _, err = schema_module.reload_schema(vshard_router) |
| 82 | + if err ~= nil then |
| 83 | + return nil, SchemaError:new(err) |
| 84 | + end |
| 85 | + |
| 86 | + local spaces, err = utils.get_spaces(vshard_router, opts.timeout) |
| 87 | + if err ~= nil then |
| 88 | + return nil, SchemaError:new(err) |
| 89 | + end |
| 90 | + |
| 91 | + if space_name ~= nil then |
| 92 | + local space = spaces[space_name] |
| 93 | + if space == nil then |
| 94 | + return nil, SchemaError:new("Space %q doesn't exist", space_name) |
| 95 | + end |
| 96 | + return get_crud_schema(space) |
| 97 | + else |
| 98 | + local resp = {} |
| 99 | + |
| 100 | + for name, space in pairs(spaces) do |
| 101 | + -- Can be indexed by space id and space name, |
| 102 | + -- so we need to be careful with duplicates. |
| 103 | + if type(name) == 'string' and system_spaces[name] == nil then |
| 104 | + resp[name] = get_crud_schema(space) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + return resp |
| 109 | + end |
| 110 | +end |
| 111 | + |
| 112 | +return schema |
0 commit comments