0

Please help me to understand what the Migrations contract in the truffle suite does. Are we supposed to inherit from it? What if I'm using OpenZepp's Ownable? Or does this file allow for long term migrations added to the contracts from early on? Thank you.

contracts/Migrations.sol

pragma solidity ^0.4.24;

contract Migrations {
  address public owner;
  uint public last_completed_migration;

  modifier restricted() {
    if (msg.sender == owner) _;
  }

  constructor() public {
    owner = msg.sender;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }

  function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
  }
}
1

The migrations contract keeps track of your deployment progress. It is not meant to be inherited from.

Truffle asks that you put contract deployments in numbered migration files, with 1_*.js being provided for you (the first one deploys the migration contract).

Thus, if you are deploying five contracts, you would usually end up with 6 migration files (the preexisting migration deployment + five of your own).

As truffle deploys each of these, it will set the last successful deployed number in the migrations contract.

If, for example, migration 3 fails due to some error, truffle will not redeploy 1 and 2 when you fix the error and rerun the migration. Instead, it will look at the migrations contract, notice that deployments up to 2 were successful, and carry on from 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.