I am learning Solidity and have confusion on contract upgrade-ability. From my understanding, a deployed smart contract is immutable and cannot be altered. If I need to add additional functionality to a contract, I can have the contract versioned and deploy a new version. But the storage data in the old contract remains as-is.
I have a contract like the below
pragma solidity >=0.5.10 <0.7.0;
contract User {
struct UserEntity {
address userAddress;
uint8 UserID;
string UserName;
}
UserEntity public _UserEntity;
mapping (address=> UserEntity) UserMapping;
function addUser(address _userAddress, uint8 _UserID, string memory _UserName) public {
_UserEntity = UserEntity(_userAddress, _UserID, _UserName);
UserMapping[_userAddress] = _UserEntity;
}
function getUserID(address _userAddress) public view returns(uint8) {
return(UserMapping[_userAddress].UserID);
}
}
If I need to search for the UserID, I can use the getter as above. But if I deploy a new version of the contract, the mapping UserMapping is not available. What is the best approach to be able to search the entire storage for the entry that I need? I read somewhere that a NameRegistry might be an option but I am not sure how to implement this. My end goal is to have a UI query this data and contract upgrades should be transparent to the UI
N.B. I am using a single deployment of the contract to add multiple users by loading the contract from it's deployed address, creating an externally owned account using web3js and then calling the function addUser and passing this EOA as a parameter.
The problem was solved using EternalStorage. Looking further at more elegant upgrade techniques. But the issue is resolved for now.