def migration_0001(tester_chain, MATH): project = tester_chain.project migrations_dir = project.migrations_dir class Migration0001(Migration): migration_id = '0001_deploy_v1_math' dependencies = [] operations = [ DeployContract('Math'), ] compiled_contracts = { 'Math': MATH, } migration_filename = os.path.join(migrations_dir, '0001_deploy_v1_math.py') with open(migration_filename, 'w') as migration_0001_file: write_migration(migration_0001_file, Migration0001) assert len(project.migrations) == 1 migrations_to_execute = get_migration_classes_for_execution( project.migrations, tester_chain, ) assert len(migrations_to_execute) == 1 the_migration_instance = migrations_to_execute[0] the_migration_instance.execute() return Migration0001
def test_single_migration_execution(web3, chain, MATH): class TestMigration(Migration): migration_id = '0001_initial' dependencies = [] operations = [ DeployContract('Math'), ] compiled_contracts = { 'Math': { 'code': MATH['code'], 'code_runtime': MATH['code_runtime'], 'abi': MATH['abi'], }, } migrations_to_run = get_migration_classes_for_execution([TestMigration], chain) for migration in migrations_to_run: migration.execute() assert chain.registrar.call().exists('contract/Math') is True math_address = chain.registrar.call().getAddress('contract/Math') math = web3.eth.contract(address=math_address, **MATH) assert math.call().multiply7(3) == 21
def migration_0003(tester_chain, migration_0002, MATH): project = tester_chain.project migrations_dir = project.migrations_dir class Migration0003(Migration): migration_id = '0003_deploy_alternate_math' dependencies = ['0002_deploy_v2_math'] operations = [ DeployContract('Math', contract_registrar_name='FancyMath'), ] compiled_contracts = { 'Math': MATH, } migration_filename = os.path.join(migrations_dir, '0003_deploy_alternate_math.py') with open(migration_filename, 'w') as migration_0003_file: write_migration(migration_0003_file, Migration0003) assert len(project.migrations) == 3 migrations_to_execute = get_migration_classes_for_execution( project.migrations, tester_chain, ) assert len(migrations_to_execute) == 1 the_migration_instance = migrations_to_execute[0] the_migration_instance.execute() return Migration0003
def test_single_migration_execution(web3, chain, MATH): class TestMigration(Migration): migration_id = '0001_initial' dependencies = [] operations = [ DeployContract('Math'), ] compiled_contracts = { 'Math': { 'code': MATH['code'], 'code_runtime': MATH['code_runtime'], 'abi': MATH['abi'], }, } migrations_to_run = get_migration_classes_for_execution([TestMigration], chain) for migration in migrations_to_run: migration.execute() assert chain.registrar.call().exists('contract/Math') is True math_address = chain.registrar.call().getAddress('contract/Math') math = web3.eth.contract(address=math_address, **MATH) assert math.call().multiply7(3) == 21
def migration_0001(testrpc_chain, MATH): project = testrpc_chain.project migrations_dir = project.migrations_dir class Migration0001(Migration): migration_id = '0001_deploy_v1_math' dependencies = [] operations = [ DeployContract('Math'), ] compiled_contracts = { 'Math': MATH, } migration_filename = os.path.join(migrations_dir, '0001_deploy_v1_math.py') with open(migration_filename, 'w') as migration_0001_file: write_migration(migration_0001_file, Migration0001) assert len(project.migrations) == 1 migrations_to_execute = get_migration_classes_for_execution( project.migrations, testrpc_chain, ) assert len(migrations_to_execute) == 1 the_migration_instance = migrations_to_execute[0] the_migration_instance.execute() return Migration0001
def migration_0003(testrpc_chain, migration_0002, MATH): project = testrpc_chain.project migrations_dir = project.migrations_dir class Migration0003(Migration): migration_id = '0003_deploy_alternate_math' dependencies = ['0002_deploy_v2_math'] operations = [ DeployContract('Math', contract_registrar_name='FancyMath'), ] compiled_contracts = { 'Math': MATH, } migration_filename = os.path.join(migrations_dir, '0003_deploy_alternate_math.py') with open(migration_filename, 'w') as migration_0003_file: write_migration(migration_0003_file, Migration0003) assert len(project.migrations) == 3 migrations_to_execute = get_migration_classes_for_execution( project.migrations, testrpc_chain, ) assert len(migrations_to_execute) == 1 the_migration_instance = migrations_to_execute[0] the_migration_instance.execute() return Migration0003
def chain(unmigrated_chain): # Determine if we have any migrations to run. migrations_to_execute = get_migration_classes_for_execution( unmigrated_chain.project.migrations, unmigrated_chain, ) for migration in migrations_to_execute: migration.execute() return unmigrated_chain
def chain(unmigrated_chain): # Determine if we have any migrations to run. migrations_to_execute = get_migration_classes_for_execution( unmigrated_chain.project.migrations, unmigrated_chain, ) for migration in migrations_to_execute: migration.execute() return unmigrated_chain
def chain(_unmigrated_chain): # Determine if we have any migrations to run. migrations_to_execute = get_migration_classes_for_execution( _unmigrated_chain.project.migrations, _unmigrated_chain, ) if migrations_to_execute: warnings.warn( PendingDeprecationWarning( "The migrations API is deprecated and will be removed in the near " "future")) for migration in migrations_to_execute: migration.execute() return _unmigrated_chain
def migrate(ctx, chain_name): """ Run project migrations """ project = ctx.obj['PROJECT'] if not project.migrations: raise click.ClickException(( "The project does not appear to have any migrations. You can use " "the `populus makemigration` command to generate project migrations" )) # Validate the project migrations validate_migration_classes(project.migrations) # Determine which chain should be used. if not chain_name: chain_name = select_chain(project) chain_config = project.config.chains[chain_name] # Determine if the chain is *migratable* if 'registrar' not in chain_config: # TODO: this should be a property of the chain object itself. # Something like `chain.is_ready_for_migrations`. if chain_name not in {'testrpc', 'temp'}: # ignore `testrpc` and `temp` because they lazily create their # registrar contracts. # TODO: We can present the use with the option to just initialize # the chain right here rather than throwing an error. initialize_chain_prompt = ( "The chain '{0}' is not configured with a registrar contract " "address. Would you like to deploy one now?".format( chain_name, ) ) if click.confirm(initialize_chain_prompt, default=True): from .chain_cmd import chain_init ctx.invoke(chain_init, chain_name=chain_name) else: no_registrar_message = ( "In order to use the migrations functionality, a registrar " "contract is required. You can initialize this chain with a " "registrar using the command `$ populus chain init " "{name}`".format(name=chain_name) ) click.echo(no_registrar_message, err=True) click.exit(1) with project.get_chain(chain_name) as chain: if chain_name in {'mainnet', 'morden'}: show_chain_sync_progress(chain) account = get_unlocked_deploy_from_address(chain) chain.web3.eth.defaultAccount = account # Wait for chain sync if this is a public network. if chain_name in {'mainnet', 'morden'}: show_chain_sync_progress(chain) # Determine if we have any migrations to run. migrations_to_execute = get_migration_classes_for_execution( project.migrations, chain, ) if not migrations_to_execute: click.echo("All migrations have been run.") ctx.exit(0) click.echo("Migration operations to perform:") for migration in migrations_to_execute: click.echo(''.join(( " ", migration.migration_id, " ({0} operations)".format(len(migration.operations)), ":", ))) for operation_index, operation in enumerate(migration.operations): click.echo(''.join(( " ", str(operation_index), " - ", str(operation), ))) click.echo("Executing migrations:") for migration in migrations_to_execute: click.echo(''.join(( " ", migration.migration_id, "... ", )), nl=False) migration.execute() click.echo(" DONE")