EOSIO based blockchains allow developers to easily update their smart contract code. However, a few things need to be considered when it comes to data updates and/or migration. The multi-index table API is one of the mechanisms (Key-Value API being the other) for storing and updating blockchain state. The multi-index table API creates and uses data structures in RAM. Once created and deployed on the blockchain there are limitations if you want to update these structures. Below you will find a few approaches to your smart contract data design, updates to this design, and for data migration.
Modifying a deployed multi-index table structure may be done by selecting one of the different strategies outlined below:
If you don't mind losing the data from the initial table you can follow these two steps:
- Erase all records from first table
- Deploy a new contract with modified table structure
If you want to keep the existing data there are two ways to do it:
To learn how to modify the structure using binary extensions read this tutorial.
To learn how to modify the structure using ABI variants read this tutorial.
- Create the new version of your multi-index table alongside the old one.
- Transfer data from the old table to the new one. You may do so as part of your normal access pattern, first checking the new table to see if the entry you seek is present and if not, check the original table, and if it's present, migrate it while adding the data for the new field, then remove it from the original table to save RAM costs.
- You must retain both versions of your multi-index table until you have completed this migration, at which point you may update your contract to remove the original version of your multi-index table.
If you prefer less code complexity and can accept downtime for your application:
- Deploy a version of your contract solely for migration purposes, and run migration transactions on every row of your table until complete. If the first table is big, e.g. has a large number of rows, the transaction time limit could be reached while running the migration transactions. To mitigate this implement the migrate function to move a limited number of rows each time it runs.
- Deploy a new contract using only the new version of the table, at which point, your migration and downtime is complete.