1

I need to add a parameter to existing space and keep the existing data.

space is created like this:

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

i need to add the following parameters

{ name = 'is_online', type = 'boolean' }
{ name = 'session_id', type = 'unsigned', is_nullable = true }

how to write the required migration script?

2 Answers2

0

it's my solution

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})` is the same as `space:pairs()` 2. `user_tuple[4] = nil` is completely useless. use `user_tuple[4] = box.NULL` 3. instead of replace better use update you need no implicit yield (`sleep(0)`), since every update will do yield Other things are ok – Mons Anderson Jul 31 '20 at 22:18
0

it is better to use spacer immediately https://github.com/igorcoding/tarantool-spacer

Andrew Nodermann
  • 610
  • 8
  • 13