【问题标题】:Tarantool existing space migrationTarantool 现有空间迁移
【发布时间】:2020-07-22 00:36:45
【问题描述】:

我需要在现有空间中添加一个参数并保留现有数据。

空间是这样创建的:

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 }

如何编写所需的迁移脚本?

【问题讨论】:

    标签: migration tarantool


    【解决方案1】:

    这是我的解决方案

    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() 2 相同。user_tuple[4] = nil 完全没用。使用user_tuple[4] = box.NULL 3. 而不是替换更好的使用更新,您不需要隐式产量(sleep(0)),因为每次更新都会产生产量其他事情都可以
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2012-07-27
      • 2021-01-27
      • 1970-01-01
      • 2021-02-19
      • 2015-03-04
      • 1970-01-01
      • 2016-03-14
      • 2020-10-05
      • 1970-01-01
      相关资源
      最近更新 更多