---
# System prepended metadata

title: Mev-M data migration
tags: [checklist, mev-monitoring]

---

Request relevant data from KAPI at first.
Then, perform SQL query with necesssary parameters, like staking_module_address, nonce and so on.

The query depicted on the following code section:
```sql!
START TRANSACTION;

INSERT INTO public.meta_version (
  block_number, block_hash, timestamp, status
)
SELECT
  block_number, block_hash, timestamp, 0
FROM registry_meta__old
LIMIT 1;

WITH op_key_ids AS (
  INSERT INTO public.node_operator_key (
    "index", operator_index, operator_id, module_address)
  select "index", operator_index, NULL, '0x595F64Ddc3856a3b5Ff4f4CC1d1fb4B46cFd2bAC'
  from registry_key__old
  RETURNING *
)
INSERT INTO public.node_operator_key_version(
	key_id, meta_version_id, key, deposit_signature, used)
SELECT
  ks."id", mv.version_id, rkold.key, rkold.deposit_signature, rkold.used
FROM op_key_ids ks
  INNER JOIN registry_key__old rkold
    ON rkold.index = ks.index AND rkold.operator_index = ks.operator_index
  CROSS JOIN meta_version mv
;

WITH op_ids AS (
  INSERT INTO public.node_operator("index", "name", module_address)
  SELECT "index", "name", '0x595F64Ddc3856a3b5Ff4f4CC1d1fb4B46cFd2bAC'
  FROM registry_operator__old
  RETURNING *
)
INSERT INTO public.node_operator_version(
	operator_id, meta_version_id,
  active, "name", reward_address, staking_limit, stopped_validators,
  total_signing_keys, used_signing_keys
)
SELECT os.id, mv.version_id, rold.active, rold."name", rold.reward_address,
  rold.staking_limit, rold.stopped_validators,
  rold.total_signing_keys, rold.used_signing_keys
FROM op_ids os
  INNER JOIN registry_operator__old rold
    ON rold.index = os.index
  CROSS JOIN meta_version mv
;

WITH sm_ids AS (
  INSERT INTO public.staking_module(
    staking_module_id, staking_module_address, name, type)
  VALUES (
    1, '0x595F64Ddc3856a3b5Ff4f4CC1d1fb4B46cFd2bAC', 'curated-onchain-v1', 'curated-onchain-v1'
  )
  RETURNING *
)
INSERT INTO staking_module_version(
  staking_module_id, meta_version_id, staking_module_fee, treasury_fee, 
  target_share, status, name, last_deposit_at, last_deposit_block, 
  exited_validators_count, nonce
)
SELECT
  si.staking_module_id, mv.version_id, 500 as staking_module_fee, 500 as treasury_fee, 
  10000 as target_share, 0 as status, 'curated-onchain-v1' as "name",
  1699459536 as last_deposit_at, 275454 as last_deposit_block, 
  1 as exited_validators_count,
  339 as nonce
FROM sm_ids as si
  CROSS JOIN meta_version mv
;

UPDATE meta_version
  SET status = 1
;

ROLLBACK TRANSACTION;
COMMIT TRANSACTION;
```

Perform data binding for keys:
```sql!
UPDATE node_operator_key as rk
  SET operator_id = oe.id
FROM node_operator oe
WHERE oe.index = rk.operator_index
  AND oe.module_address = rk.module_address
  AND rk.operator_id is null;

UPDATE node_operator_key_version as kv
  SET node_operator_version_id = nov.version_id
FROM node_operator_version as nov
  INNER JOIN node_operator as nop
    ON nop.id = nov.operator_id
  INNER JOIN node_operator_key as nok
    ON nok.operator_index = nop.index
      AND nok.module_address = nop.module_address
WHERE kv.node_operator_version_id is null
  AND kv.meta_version_id = nov.meta_version_id
  AND kv.key_id = nok.id;
```

Perform data binding for node operators:
```sql!
UPDATE node_operator oe
  SET staking_module_id = sm.id
FROM staking_module sm
WHERE sm.staking_module_address = oe.module_address
  AND oe.staking_module_id is null;

UPDATE node_operator_version as nov
  SET staking_module_version_id = smv.version_id
FROM staking_module_version as smv
  INNER JOIN staking_module as sm
    ON sm.id = smv.staking_module_id
  INNER JOIN node_operator as nop
    ON nop.module_address = sm.staking_module_address
WHERE nop.id = nov.operator_id
  AND smv.meta_version_id = nov.meta_version_id;
```

Perform data binding for already existing relation between node operators and slots:
```sql!
UPDATE slot_storage ss
  SET operator_version_id = ove.version_id
FROM node_operator_version as ove
  INNER JOIN node_operator as op
    ON op.id = ove.operator_id
WHERE ss.operator_index = op.index
  AND ove.meta_version_id IN (
    SELECT mve.version_id
    FROM meta_version mve
    WHERE mve.status = 1
    LIMIT 1
  )
  -- for testing purposes
  AND slot_number IN (
    SELECT slot_number
    FROM slot_storage
    WHERE operator_index IS NOT NULL
    ORDER BY slot_number
    LIMIT 1
  )
;
```