added migration script for utxo space

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-04-13 15:19:56 +02:00
parent 6a3c655e3b
commit 47e7eeb4fe
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
4 changed files with 114 additions and 1 deletions

View File

@ -22,7 +22,9 @@ services:
- "3303:3303"
- "8081:8081"
volumes:
- ./planetmint/backend/tarantool/init.lua:/opt/tarantool/init.lua
- ./planetmint/backend/tarantool/opt/init.lua:/opt/tarantool/init.lua
- ./planetmint/backend/tarantool/opt/functions.lua:/opt/tarantool/functions.lua
- ./planetmint/backend/tarantool/opt/migrations.lua:/opt/tarantool/migrations.lua
entrypoint: tarantool /opt/tarantool/init.lua
restart: always
planetmint:

View File

@ -0,0 +1,20 @@
local fiber = require('fiber')
local export = {}
function export.atomic(batch_size, iter, fn)
box.atomic(function()
local i = 0
for _, x in iter:unwrap() do
fn(x)
i = i + 1
if i % batch_size == 0 then
box.commit()
fiber.yield() -- for read-only operations when `commit` doesn't yield
box.begin()
end
end
end)
end
return export

View File

@ -1,3 +1,5 @@
local migrations = require('migrations')
box.cfg{listen = 3303}
box.once("bootstrap", function()
@ -331,3 +333,13 @@ end
function delete_output( id )
box.space.outputs:delete(id)
end
function migrate_up()
migrations.update_utxo_13042023.up()
-- add newer migrations below
end
function migrate_down()
-- add newer migrations above
migrations.update_utxo_13042023.down()
end

View File

@ -0,0 +1,79 @@
local functions = require('functions')
local migrations = {}
migrations.update_utxo_13042023 = {}
migrations.update_utxo_13042023.up = function()
if utxos.index.public_keys == nil then
box.space.utxos:drop()
utxos = box.schema.create_space('utxos', { if_not_exists = true })
utxos:format({
{ name = 'id', type = 'string' },
{ name = 'amount' , type = 'unsigned' },
{ name = 'public_keys', type = 'array' },
{ name = 'condition', type = 'map' },
{ name = 'output_index', type = 'number' },
{ name = 'transaction_id' , type = 'string' }
})
utxos:create_index('id', {
if_not_exists = true,
parts = {{ field = 'id', type = 'string' }}
})
utxos:create_index('utxos_by_transaction_id', {
if_not_exists = true,
unique = false,
parts = {{ field = 'transaction_id', type = 'string' }}
})
utxos:create_index('utxo_by_transaction_id_and_output_index', {
if_not_exists = true,
parts = {
{ field = 'transaction_id', type = 'string' },
{ field = 'output_index', type = 'unsigned' }
}
})
utxos:create_index('public_keys', {
if_not_exists = true,
unique = false,
parts = {{field = 'public_keys[*]', type = 'string' }}
})
end
outputs = box.space.outputs
functions.atomic(1000, outputs:pairs(), function(output)
utxos:insert{output[0], output[1], output[2], output[3], output[4], output[5]}
end)
functions.atomic(1000, utxos:pairs(), function(utxo)
spending_transaction = transactions.index.spending_transaction_by_id_and_output_index:select{utxo[5], utxo[4]}
if table.getn(spending_transaction) > 0 then
utxos:delete(utxo[0])
end
end)
end
migrations.update_utxo_13042023.down = function()
box.space.utxos:drop()
utxos = box.schema.create_space('utxos', { if_not_exists = true })
utxos:format({
{ name = 'id', type = 'string' },
{ name = 'transaction_id', type = 'string' },
{ name = 'output_index', type = 'unsigned' },
{ name = 'utxo', type = 'map' }
})
utxos:create_index('id', {
if_not_exists = true,
parts = {{ field = 'id', type = 'string' }}
})
utxos:create_index('utxos_by_transaction_id', {
if_not_exists = true,
unique = false,
parts = {{ field = 'transaction_id', type = 'string' }}
})
utxos:create_index('utxo_by_transaction_id_and_output_index', {
if_not_exists = true,
parts = {
{ field = 'transaction_id', type = 'string' },
{ field = 'output_index', type = 'unsigned' }
}})
end
return migrations