Migration 塔兰托现有空间迁移

Migration 塔兰托现有空间迁移,migration,tarantool,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' }, {

我需要向现有空间添加一个参数,并保留现有数据

空间是这样创建的:

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
这是我的解决办法

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

最好立即使用垫片
最好立即使用垫片

1
space:pairs(nil,{iterator=box.index.ALL})
space:pairs()相同
user\u tuple[4]=nil
是完全无用的。使用
user\u tuple[4]=box.NULL
3。代替替换更好地使用更新,您不需要隐式的收益(
sleep(0)
),因为每次更新都会产生收益,其他事情都是确定的
space:pairs(nil,{iterator=box.index.ALL})
space:pairs()相同
user\u tuple[4]=nil
是完全无用的。使用
user\u tuple[4]=box.NULL
3。不需要替换更好地使用更新,而需要隐式的收益(
sleep(0)
),因为每次更新都会产生其他的事情