Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3525 from trufflesuite/db-migrate
Browse files Browse the repository at this point in the history
Hook up `truffle migrate` to @truffle/db
  • Loading branch information
gnidan authored and g. nicholas d'andrea committed Feb 1, 2021
2 parents c2b2413 + bcb8319 commit b5c016a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/artifactor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class Artifactor {
if (Array.isArray(artifactObjects)) {
const tmpArtifactArray = artifactObjects;
tmpArtifactArray.forEach(artifactObj => {
if (newArtifactObjects[artifactObj.contract_name]) {
if (newArtifactObjects[artifactObj.contract_name || artifactObj.contractName]) {
console.warn(
`${OS.EOL}> Duplicate contract names found for ${
artifactObj.contract_name
}.${OS.EOL}` +
`> This can cause errors and unknown behavior. Please rename one of your contracts.`
);
}
newArtifactObjects[artifactObj.contract_name] = artifactObj;
newArtifactObjects[artifactObj.contract_name || artifactObj.contractName] = artifactObj;
});
} else {
newArtifactObjects = artifactObjects;
Expand Down
52 changes: 38 additions & 14 deletions packages/migrate/Migration.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const debug = require("debug")("migrate:Migration");
const path = require("path");
const Deployer = require("@truffle/deployer");
const Require = require("@truffle/require");
const Emittery = require("emittery");
const {
Web3Shim,
createInterfaceAdapter,
createInterfaceAdapter
} = require("@truffle/interface-adapter");
const {connect, Project} = require("@truffle/db");

const ResolverIntercept = require("./ResolverIntercept");

Expand Down Expand Up @@ -37,7 +39,7 @@ class Migration {
file: this.file,
context: context,
resolver: resolver,
args: [deployer],
args: [deployer]
};

const fn = Require.file(requireOptions);
Expand Down Expand Up @@ -89,23 +91,45 @@ class Migration {
const message = `Saving migration to chain.`;

if (!this.dryRun) {
const data = { message: message };
const data = {message: message};
await this.emitter.emit("startTransaction", data);
}

const migrations = await Migrations.deployed();
const receipt = await migrations.setCompleted(this.number);

if (!this.dryRun) {
const data = { receipt: receipt, message: message };
const data = {receipt: receipt, message: message};
await this.emitter.emit("endTransaction", data);
}
}

await this.emitter.emit("postMigrate", this.isLast);

let artifacts = resolver
.contracts()
.map(abstraction => abstraction._json);
if (this.config.db && this.config.db.enabled && artifacts.length > 0) {
const db = connect(this.config);
const project = await Project.initialize({
db,
project: {
directory: this.config.working_directory
}
});

({artifacts} = await project
.connect({provider: this.config.provider})
.loadMigrate({
network: {
name: this.config.network
},
artifacts
}));
}

// Save artifacts to local filesystem
await options.artifactor.saveAll(resolver.contracts());
await options.artifactor.saveAll(artifacts);

deployer.finish();

Expand All @@ -123,7 +147,7 @@ class Migration {
} catch (error) {
const payload = {
type: "migrateErr",
error: error,
error: error
};

await this.emitter.emit("error", payload);
Expand All @@ -143,7 +167,7 @@ class Migration {
interfaceAdapter,
resolver,
context,
deployer,
deployer
} = this.prepareForMigrations(options);

// Connect reporter to this migration
Expand All @@ -164,7 +188,7 @@ class Migration {
isFirst: this.isFirst,
network: options.network,
networkId: options.network_id,
blockLimit: block.gasLimit,
blockLimit: block.gasLimit
};

await this.emitter.emit("preMigrate", preMigrationsData);
Expand All @@ -175,17 +199,17 @@ class Migration {
const logger = options.logger;
const interfaceAdapter = createInterfaceAdapter({
provider: options.provider,
networkType: options.networks[options.network].type,
networkType: options.networks[options.network].type
});
const web3 = new Web3Shim({
provider: options.provider,
networkType: options.networks[options.network].type,
networkType: options.networks[options.network].type
});

const resolver = new ResolverIntercept(options.resolver);

// Initial context.
const context = { web3, interfaceAdapter, config: this.config };
const context = {web3, interfaceAdapter, config: this.config};

const deployer = new Deployer({
logger,
Expand All @@ -196,10 +220,10 @@ class Migration {
network_id: options.network_id,
provider: options.provider,
basePath: path.dirname(this.file),
ens: options.ens,
ens: options.ens
});

return { interfaceAdapter, resolver, context, deployer };
return {interfaceAdapter, resolver, context, deployer};
}

/**
Expand All @@ -213,7 +237,7 @@ class Migration {
isFirst: this.isFirst,
isLast: this.isLast,
dryRun: this.dryRun,
interactive: this.interactive,
interactive: this.interactive
};
}
}
Expand Down

0 comments on commit b5c016a

Please sign in to comment.