Oz upgrade command and the initialize function - does the latter get called by default

Hi @gitpusha,

Thank you for all your feedback.

Issues and PRs to improve the documentation are very welcome.
Specifically if the initializers documentation could be improved.
https://docs.openzeppelin.com/sdk/2.5/writing-contracts#initializers

One thing to note is that an upgrade function should be protected from being called more than once. A guard could look as follows:

contract MyContract is Initializable {
    uint256 private _value;
    bool private _upgraded;

    function initialize(uint256 value) public initializer {
        _value = value;
    }

    function upgrade(uint256 value) public {
        require(!_upgraded, "MyContract: already upgraded");
        _upgraded = true;
        _value = value;
    }

    function getValue() public view returns (uint256) {
        return _value;
    }
}
1 Like