I am developing a smart contract using solidity. Every time I edit the contract and want to prove it I run truffle migrate --reset
.
By doing so, I lose all the data that I had saved in the contract.
I wonder if there is any way to migrate the contract while preserving the data, as it is done with traditional databases, since only by truffle migrate
the contract is not migrated, it is only recompiled.
Thanks a lot!
Short answer: Currently not possible using Truffle. But if you know what you're doing, you could keep the storage with more low-level approach.
By running truffle migrate
you are usually running a javascript code that deploys the Solidity contract using the standard CREATE
EVM opcode. So every time you're running truffle migrate
, Truffle deploys the contract to a new address that has empty storage slots.
Even though it's possible to redeploy a smart contract to an already used address using CREATE2
opcode (and to keep the original storage data), Truffle currently doesn't support this option.
Note: The --reset
option only runs all migrations from the beginning in case of previous failure, but has no effect on the storage or the contract address.
You could achieve this goal (keeping the storage) by setting the (contract deployment) transaction data to an instruction to deploy to an address that already contains a self-destruct contract. But it's much more low-level approach than Truffle currently allows. If you want to learn more about this technique, these articles would be a good start: