|
| 1 | +local ddl = require('ddl') |
| 2 | +local helper = require('test.helper') |
| 3 | + |
| 4 | +local some_module = { |
| 5 | + sharding_func = function(key) |
| 6 | + if key ~= nil and key[1] ~= nil then |
| 7 | + return key[1] % 10 |
| 8 | + end |
| 9 | + end |
| 10 | +} |
| 11 | + |
| 12 | +return { |
| 13 | + init = function() |
| 14 | + rawset(_G, 'some_module', some_module) |
| 15 | + |
| 16 | + local engine = os.getenv('ENGINE') or 'memtx' |
| 17 | + local customers_schema = { |
| 18 | + engine = engine, |
| 19 | + is_local = true, |
| 20 | + temporary = false, |
| 21 | + format = { |
| 22 | + {name = 'id', is_nullable = false, type = 'unsigned'}, |
| 23 | + {name = 'bucket_id', is_nullable = false, type = 'unsigned'}, |
| 24 | + {name = 'name', is_nullable = false, type = 'string'}, |
| 25 | + {name = 'age', is_nullable = false, type = 'number'}, |
| 26 | + }, |
| 27 | + indexes = { |
| 28 | + -- This table is intentionally blank. |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + local primary_index = { |
| 33 | + name = 'id', |
| 34 | + type = 'TREE', |
| 35 | + unique = true, |
| 36 | + parts = { |
| 37 | + {path = 'id', is_nullable = false, type = 'unsigned'}, |
| 38 | + {path = 'name', is_nullable = false, type = 'string'}, |
| 39 | + }, |
| 40 | + } |
| 41 | + local primary_index_id = { |
| 42 | + name = 'id', |
| 43 | + type = 'TREE', |
| 44 | + unique = true, |
| 45 | + parts = { |
| 46 | + {path = 'id', is_nullable = false, type = 'unsigned'}, |
| 47 | + }, |
| 48 | + } |
| 49 | + local bucket_id_index = { |
| 50 | + name = 'bucket_id', |
| 51 | + type = 'TREE', |
| 52 | + unique = false, |
| 53 | + parts = { |
| 54 | + {path = 'bucket_id', is_nullable = false, type = 'unsigned'}, |
| 55 | + } |
| 56 | + } |
| 57 | + local name_index = { |
| 58 | + name = 'name', |
| 59 | + type = 'TREE', |
| 60 | + unique = true, |
| 61 | + parts = { |
| 62 | + {path = 'name', is_nullable = false, type = 'string'}, |
| 63 | + }, |
| 64 | + } |
| 65 | + local age_index = { |
| 66 | + name = 'age', |
| 67 | + type = 'TREE', |
| 68 | + unique = false, |
| 69 | + parts = { |
| 70 | + {path = 'age', is_nullable = false, type = 'number'}, |
| 71 | + }, |
| 72 | + } |
| 73 | + local secondary_index = { |
| 74 | + name = 'secondary', |
| 75 | + type = 'TREE', |
| 76 | + unique = false, |
| 77 | + parts = { |
| 78 | + {path = 'id', is_nullable = false, type = 'unsigned'}, |
| 79 | + {path = 'name', is_nullable = false, type = 'string'}, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + local three_fields_index = { |
| 84 | + name = 'three_fields', |
| 85 | + type = 'TREE', |
| 86 | + unique = false, |
| 87 | + parts = { |
| 88 | + {path = 'age', is_nullable = false, type = 'number'}, |
| 89 | + {path = 'name', is_nullable = false, type = 'string'}, |
| 90 | + {path = 'id', is_nullable = false, type = 'unsigned'}, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + local customers_id_schema = table.deepcopy(customers_schema) |
| 95 | + customers_id_schema.sharding_key = {'id'} |
| 96 | + table.insert(customers_id_schema.indexes, primary_index_id) |
| 97 | + table.insert(customers_id_schema.indexes, bucket_id_index) |
| 98 | + table.insert(customers_id_schema.indexes, age_index) |
| 99 | + |
| 100 | + local customers_name_key_schema = table.deepcopy(customers_schema) |
| 101 | + customers_name_key_schema.sharding_key = {'name'} |
| 102 | + table.insert(customers_name_key_schema.indexes, primary_index) |
| 103 | + table.insert(customers_name_key_schema.indexes, bucket_id_index) |
| 104 | + |
| 105 | + local customers_name_key_uniq_index_schema = table.deepcopy(customers_schema) |
| 106 | + customers_name_key_uniq_index_schema.sharding_key = {'name'} |
| 107 | + table.insert(customers_name_key_uniq_index_schema.indexes, primary_index) |
| 108 | + table.insert(customers_name_key_uniq_index_schema.indexes, bucket_id_index) |
| 109 | + table.insert(customers_name_key_uniq_index_schema.indexes, name_index) |
| 110 | + |
| 111 | + local customers_name_key_non_uniq_index_schema = table.deepcopy(customers_schema) |
| 112 | + customers_name_key_non_uniq_index_schema.sharding_key = {'name'} |
| 113 | + name_index.unique = false |
| 114 | + table.insert(customers_name_key_non_uniq_index_schema.indexes, primary_index) |
| 115 | + table.insert(customers_name_key_non_uniq_index_schema.indexes, bucket_id_index) |
| 116 | + table.insert(customers_name_key_non_uniq_index_schema.indexes, name_index) |
| 117 | + |
| 118 | + local customers_secondary_idx_name_key_schema = table.deepcopy(customers_schema) |
| 119 | + customers_secondary_idx_name_key_schema.sharding_key = {'name'} |
| 120 | + table.insert(customers_secondary_idx_name_key_schema.indexes, primary_index_id) |
| 121 | + table.insert(customers_secondary_idx_name_key_schema.indexes, secondary_index) |
| 122 | + table.insert(customers_secondary_idx_name_key_schema.indexes, bucket_id_index) |
| 123 | + |
| 124 | + local customers_age_key_schema = table.deepcopy(customers_schema) |
| 125 | + customers_age_key_schema.sharding_key = {'age'} |
| 126 | + table.insert(customers_age_key_schema.indexes, primary_index) |
| 127 | + table.insert(customers_age_key_schema.indexes, bucket_id_index) |
| 128 | + |
| 129 | + local customers_name_age_key_different_indexes_schema = table.deepcopy(customers_schema) |
| 130 | + customers_name_age_key_different_indexes_schema.sharding_key = {'name', 'age'} |
| 131 | + table.insert(customers_name_age_key_different_indexes_schema.indexes, primary_index) |
| 132 | + table.insert(customers_name_age_key_different_indexes_schema.indexes, bucket_id_index) |
| 133 | + table.insert(customers_name_age_key_different_indexes_schema.indexes, age_index) |
| 134 | + |
| 135 | + local customers_name_age_key_three_fields_index_schema = table.deepcopy(customers_schema) |
| 136 | + customers_name_age_key_three_fields_index_schema.sharding_key = {'name', 'age'} |
| 137 | + table.insert(customers_name_age_key_three_fields_index_schema.indexes, primary_index_id) |
| 138 | + table.insert(customers_name_age_key_three_fields_index_schema.indexes, bucket_id_index) |
| 139 | + table.insert(customers_name_age_key_three_fields_index_schema.indexes, three_fields_index) |
| 140 | + |
| 141 | + local customers_id_key_schema = table.deepcopy(customers_schema) |
| 142 | + customers_id_key_schema.sharding_key = {'id'} |
| 143 | + table.insert(customers_id_key_schema.indexes, primary_index) |
| 144 | + table.insert(customers_id_key_schema.indexes, bucket_id_index) |
| 145 | + table.insert(customers_id_key_schema.indexes, name_index) |
| 146 | + |
| 147 | + local customers_body_func_schema = table.deepcopy(customers_id_key_schema) |
| 148 | + customers_body_func_schema.sharding_func = { body = 'function(key) return key[1] % 10 end' } |
| 149 | + |
| 150 | + local customers_G_func_schema = table.deepcopy(customers_id_key_schema) |
| 151 | + customers_G_func_schema.sharding_func = 'some_module.sharding_func' |
| 152 | + |
| 153 | + local customers_empty_sharding_func_schema = table.deepcopy(customers_id_key_schema) |
| 154 | + |
| 155 | + local customers_vshard_mpcrc32_schema = table.deepcopy(customers_id_key_schema) |
| 156 | + customers_vshard_mpcrc32_schema.sharding_func = 'vshard.router.bucket_id_mpcrc32' |
| 157 | + |
| 158 | + local customers_vshard_strcrc32_schema = table.deepcopy(customers_id_key_schema) |
| 159 | + customers_vshard_strcrc32_schema.sharding_func = 'vshard.router.bucket_id_strcrc32' |
| 160 | + |
| 161 | + local schema = { |
| 162 | + spaces = { |
| 163 | + customers = customers_id_schema, |
| 164 | + customers_name_key = customers_name_key_schema, |
| 165 | + customers_name_key_uniq_index = customers_name_key_uniq_index_schema, |
| 166 | + customers_name_key_non_uniq_index = customers_name_key_non_uniq_index_schema, |
| 167 | + customers_secondary_idx_name_key = customers_secondary_idx_name_key_schema, |
| 168 | + customers_age_key = customers_age_key_schema, |
| 169 | + customers_name_age_key_different_indexes = customers_name_age_key_different_indexes_schema, |
| 170 | + customers_name_age_key_three_fields_index = customers_name_age_key_three_fields_index_schema, |
| 171 | + customers_G_func = customers_G_func_schema, |
| 172 | + customers_body_func = customers_body_func_schema, |
| 173 | + customers_empty_sharding_func = customers_empty_sharding_func_schema, |
| 174 | + customers_vshard_mpcrc32 = customers_vshard_mpcrc32_schema, |
| 175 | + customers_vshard_strcrc32 = customers_vshard_strcrc32_schema, |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + rawset(_G, 'set_sharding_key', function(space_name, sharding_key_def) |
| 180 | + local fieldno_sharding_key = 2 |
| 181 | + box.space['_ddl_sharding_key']:update(space_name, {{'=', fieldno_sharding_key, sharding_key_def}}) |
| 182 | + end) |
| 183 | + rawset(_G, 'set_sharding_func', function(space_name, fieldno_sharding_func, sharding_func_def) |
| 184 | + local record = {space_name, box.NULL, box.NULL} |
| 185 | + record[fieldno_sharding_func] = sharding_func_def |
| 186 | + box.space['_ddl_sharding_func']:replace(record) |
| 187 | + end) |
| 188 | + |
| 189 | + helper.wrap_schema_init(function() |
| 190 | + local ok, err = ddl.set_schema(schema) |
| 191 | + if not ok then |
| 192 | + error(err) |
| 193 | + end |
| 194 | + end)() |
| 195 | + end, |
| 196 | + wait_until_ready = helper.wait_schema_init, |
| 197 | +} |
0 commit comments