Merge pull request #1111 from poanetwork/1109
Allow gradual data migration after schema and code is deployedpull/1164/head
commit
ae297e566f
@ -0,0 +1,64 @@ |
||||
DO $$ |
||||
DECLARE |
||||
row_count integer := 1; |
||||
batch_size integer := 50000; -- HOW MANY ITEMS WILL BE UPDATED AT TIME |
||||
iterator integer := batch_size; |
||||
max_row_number integer; |
||||
next_iterator integer; |
||||
updated_row_count integer; |
||||
deleted_row_count integer; |
||||
BEGIN |
||||
DROP TABLE IF EXISTS current_suicide_internal_transactions_temp; |
||||
-- CREATES TEMP TABLE TO STORE DATA TO BE UPDATED |
||||
CREATE TEMP TABLE current_suicide_internal_transactions_temp( |
||||
transaction_hash bytea NOT NULL, |
||||
index bigint NOT NULL, |
||||
row_number integer |
||||
); |
||||
INSERT INTO current_suicide_internal_transactions_temp |
||||
SELECT DISTINCT ON (transaction_hash, index) |
||||
transaction_hash, |
||||
index, |
||||
ROW_NUMBER () OVER () |
||||
FROM internal_transactions |
||||
WHERE type = 'suicide' |
||||
ORDER BY transaction_hash, index DESC; |
||||
|
||||
max_row_number := (SELECT MAX(row_number) FROM current_suicide_internal_transactions_temp); |
||||
RAISE NOTICE '% items to be updated', max_row_number + 1; |
||||
|
||||
-- ITERATES THROUGH THE ITEMS UNTIL THE TEMP TABLE IS EMPTY |
||||
WHILE iterator <= max_row_number LOOP |
||||
next_iterator := iterator + batch_size; |
||||
|
||||
RAISE NOTICE '-> suicide internal transactions % to % to be updated', iterator, next_iterator - 1; |
||||
|
||||
UPDATE internal_transactions |
||||
SET type = 'selfdestruct' |
||||
FROM current_suicide_internal_transactions_temp |
||||
WHERE internal_transactions.transaction_hash = current_suicide_internal_transactions_temp.transaction_hash AND |
||||
internal_transactions.index = current_suicide_internal_transactions_temp.index AND |
||||
current_suicide_internal_transactions_temp.row_number < next_iterator; |
||||
|
||||
GET DIAGNOSTICS updated_row_count = ROW_COUNT; |
||||
|
||||
RAISE NOTICE '-> % internal transactions updated from suicide to selfdesruct', updated_row_count; |
||||
|
||||
DELETE FROM current_suicide_internal_transactions_temp |
||||
WHERE row_number < next_iterator; |
||||
|
||||
GET DIAGNOSTICS deleted_row_count = ROW_COUNT; |
||||
|
||||
ASSERT updated_row_count = deleted_row_count; |
||||
|
||||
-- COMMITS THE BATCH UPDATES |
||||
CHECKPOINT; |
||||
|
||||
-- UPDATES THE COUNTER SO IT DOESN'T TURN INTO AN INFINITE LOOP |
||||
iterator := next_iterator; |
||||
END LOOP; |
||||
|
||||
RAISE NOTICE 'All suicide type internal transactions updated to selfdestruct. Validating constraint.'; |
||||
|
||||
ALTER TABLE internal_transactions VALIDATE CONSTRAINT selfdestruct_has_from_and_to_address; |
||||
END $$; |
@ -0,0 +1,80 @@ |
||||
DO $$ |
||||
DECLARE |
||||
row_count integer := 1; |
||||
batch_size integer := 50000; -- HOW MANY ITEMS WILL BE UPDATED AT TIME |
||||
iterator integer := batch_size; |
||||
max_row_number integer; |
||||
next_iterator integer; |
||||
updated_transaction_count integer; |
||||
deleted_internal_transaction_count integer; |
||||
deleted_row_count integer; |
||||
BEGIN |
||||
DROP TABLE IF EXISTS transactions_with_deprecated_internal_transactions; |
||||
-- CREATES TEMP TABLE TO STORE DATA TO BE UPDATED |
||||
CREATE TEMP TABLE transactions_with_deprecated_internal_transactions( |
||||
hash bytea NOT NULL, |
||||
row_number integer |
||||
); |
||||
INSERT INTO transactions_with_deprecated_internal_transactions |
||||
SELECT DISTINCT ON (transaction_hash) |
||||
transaction_hash, |
||||
ROW_NUMBER () OVER () |
||||
FROM internal_transactions |
||||
WHERE |
||||
-- call_has_call_type CONSTRAINT |
||||
(type = 'call' AND call_type IS NULL) OR |
||||
-- call_has_input CONSTRAINT |
||||
(type = 'call' AND input IS NULL) OR |
||||
-- create_has_init CONSTRAINT |
||||
(type = 'create' AND init is NULL) |
||||
ORDER BY transaction_hash DESC; |
||||
|
||||
max_row_number := (SELECT MAX(row_number) FROM transactions_with_deprecated_internal_transactions); |
||||
RAISE NOTICE '% transactions to be updated', max_row_number + 1; |
||||
|
||||
-- ITERATES THROUGH THE ITEMS UNTIL THE TEMP TABLE IS EMPTY |
||||
WHILE iterator <= max_row_number LOOP |
||||
next_iterator := iterator + batch_size; |
||||
|
||||
RAISE NOTICE '-> transactions with deprecated internal transactions % to % to be updated', iterator, next_iterator - 1; |
||||
|
||||
UPDATE transactions |
||||
SET internal_transactions_indexed_at = NULL, |
||||
error = NULL |
||||
FROM transactions_with_deprecated_internal_transactions |
||||
WHERE transactions.hash = transactions_with_deprecated_internal_transactions.hash AND |
||||
transactions_with_deprecated_internal_transactions.row_number < next_iterator; |
||||
|
||||
GET DIAGNOSTICS updated_transaction_count = ROW_COUNT; |
||||
|
||||
RAISE NOTICE '-> % transactions updated to refetch internal transactions', updated_transaction_count; |
||||
|
||||
DELETE FROM internal_transactions |
||||
USING transactions_with_deprecated_internal_transactions |
||||
WHERE internal_transactions.transaction_hash = transactions_with_deprecated_internal_transactions.hash AND |
||||
transactions_with_deprecated_internal_transactions.row_number < next_iterator; |
||||
|
||||
GET DIAGNOSTICS deleted_internal_transaction_count = ROW_COUNT; |
||||
|
||||
RAISE NOTICE '-> % internal transactions deleted', deleted_internal_transaction_count; |
||||
|
||||
DELETE FROM transactions_with_deprecated_internal_transactions |
||||
WHERE row_number < next_iterator; |
||||
|
||||
GET DIAGNOSTICS deleted_row_count = ROW_COUNT; |
||||
|
||||
ASSERT updated_transaction_count = deleted_row_count; |
||||
|
||||
-- COMMITS THE BATCH UPDATES |
||||
CHECKPOINT; |
||||
|
||||
-- UPDATES THE COUNTER SO IT DOESN'T TURN INTO AN INFINITE LOOP |
||||
iterator := next_iterator; |
||||
END LOOP; |
||||
|
||||
RAISE NOTICE 'All deprecated internal transactions will be refetched. Validating constraints.'; |
||||
|
||||
ALTER TABLE internal_transactions VALIDATE CONSTRAINT call_has_call_type; |
||||
ALTER TABLE internal_transactions VALIDATE CONSTRAINT call_has_input; |
||||
ALTER TABLE internal_transactions VALIDATE CONSTRAINT create_has_init; |
||||
END $$; |
Loading…
Reference in new issue