migration - Tarantool 现有空间迁移
问题描述
我需要向现有空间添加一个参数并保留现有数据。
空间是这样创建的:
function create_user()
local space = box.schema.create_space('user', { engine = 'memtx' })
space:format({
{ name = 'user_id', type = 'unsigned' },
{ name = 'name', type = 'string' },
{ name = 'is_active', type = 'boolean' },
})
space:create_index('users_id', { type = 'TREE', parts = { 'user_id', 'name' } })
end
我需要添加以下参数
{ name = 'is_online', type = 'boolean' }
{ name = 'session_id', type = 'unsigned', is_nullable = true }
如何编写所需的迁移脚本?
解决方案
这是我的解决方案
function migrate_users()
local counter = 0
local space = box.space.users
space:format({})
for _, tuple in space:pairs(
nil, {iterator=box.index.ALL}
) do
local user_tuple = tuple:totable()
user_tuple[4] = nil
user_tuple[5] = false
space:replace(user_tuple)
counter = counter + 1
if counter % 10000 == 0 then
fiber.sleep(0)
end
end
space:format({
{ name = 'user_id', type = 'unsigned' },
{ name = 'name', type = 'string' },
{ name = 'is_active', type = 'boolean' },
{ name = 'session_id', type = 'unsigned', is_nullable = true },
{ name = 'is_online', type = 'boolean' }
})
space:create_index('session_id', { type = 'TREE', unique = false, parts = { 'session_id' } })
end
推荐阅读
- sql-server - 如何在 MS Access 中存储 ftp 用户的密码?
- html - 强制带有标签的文本在表格中与另一个没有标签的单元格高度相同
- java - 从长度为 n 的字符数组生成长度为 m 的所有子序列,其中 n >= m 在 Java 语言中
- python-3.x - 使用发布请求 [python] 将压缩的 numpy 数组(zlib)发送到烧瓶服务器
- c# - 使用 OpenId、OAuth2、Cognito、.NET Core 3.1 和 Swagger 的 Invalid_request、未授权客户端
- c - CS50 CREDIT 分配代码中的问题
- javascript - 在 React 中使用 useState 挂钩后浏览器选项卡冻结
- git - git 报告子模块有变化,即使它没有
- javascript - 如何从云 Firestore 数据库中检索 imageUrl 和 Text 数据?
- c# - 共享 RestAPI 令牌的模式是什么?