Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use migrations/sample-migration.js as sample content if it exists #74

Merged
merged 6 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ module.exports = {
};
````

#### Overriding the sample migration
To override the content of the sample migration that will be created by the `create` command,
create a file **`sample-migration.js`** in the migrations directory.

### Checking the status of the migrations
At any time, you can check which migrations are applied (or not)

Expand Down
13 changes: 11 additions & 2 deletions lib/actions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ module.exports = async description => {
throw new Error("Missing parameter: description");
}
await migrationsDir.shouldExist();
const source = path.join(__dirname, "../../samples/migration.js");
const migrationsDirPath = await migrationsDir.resolve();

// Check if there is a 'sample-migration.js' file in migrations dir - if there is, use that
let source;
if (await migrationsDir.doesSampleMigrationExist()) {
source = await migrationsDir.resolveSampleMigrationPath();
} else {
source = path.join(__dirname, "../../samples/migration.js");
}

const filename = `${date.nowAsString()}-${description
.split(" ")
.join("_")}.js`;
const destination = path.join(await migrationsDir.resolve(), filename);
const destination = path.join(migrationsDirPath, filename);
await fs.copy(source, destination);
return filename;
};
20 changes: 18 additions & 2 deletions lib/env/migrationsDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ async function resolveMigrationsDirPath() {
return path.join(process.cwd(), migrationsDir);
}

async function resolveSampleMigrationPath() {
const migrationsDir = await resolveMigrationsDirPath();
return path.join(migrationsDir, 'sample-migration.js');
}

module.exports = {
resolve: resolveMigrationsDirPath,
resolveSampleMigrationPath,

async shouldExist() {
const migrationsDir = await resolveMigrationsDirPath();
Expand Down Expand Up @@ -55,11 +61,21 @@ module.exports = {
async getFileNames() {
const migrationsDir = await resolveMigrationsDirPath();
const files = await fs.readdir(migrationsDir);
return files.filter(file => path.extname(file) === ".js");
return files.filter(file => path.extname(file) === ".js" && path.basename(file) !== 'sample-migration.js');
},

async loadMigration(fileName) {
const migrationsDir = await resolveMigrationsDirPath();
return require(path.join(migrationsDir, fileName)); // eslint-disable-line
}
},

async doesSampleMigrationExist() {
const samplePath = await resolveSampleMigrationPath();
try {
await fs.stat(samplePath);
return true;
} catch (err) {
return false;
}
},
};
21 changes: 20 additions & 1 deletion test/actions/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe("create", () => {

function mockMigrationsDir() {
return {
shouldExist: sinon.stub().returns(Promise.resolve())
shouldExist: sinon.stub().returns(Promise.resolve()),
doesSampleMigrationExist: sinon.stub().returns(Promise.resolve(false))
};
}

Expand Down Expand Up @@ -114,4 +115,22 @@ describe("create", () => {
expect(err.message).to.equal("Copy failed");
}
});

it("should use the sample migration file if it exists", async () => {
const clock = sinon.useFakeTimers(
new Date("2016-06-09T08:07:00.077Z").getTime()
);
migrationsDir.doesSampleMigrationExist.returns(true);
const filename = await create("my_description");
expect(migrationsDir.doesSampleMigrationExist.called).to.equal(true);
expect(fs.copy.called).to.equal(true);
expect(fs.copy.getCall(0).args[0]).to.equal(
path.join(process.cwd(), "migrations", "sample-migration.js")
);
expect(fs.copy.getCall(0).args[1]).to.equal(
path.join(process.cwd(), "migrations", "20160609080700-my_description.js")
);
expect(filename).to.equal("20160609080700-my_description.js");
clock.restore();
});
});
14 changes: 14 additions & 0 deletions test/env/migrationsDir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,18 @@ describe("migrationsDir", () => {
}
});
});

describe("doesSampleMigrationExist()", () => {
it("should return true if sample migration exists", async () => {
fs.stat.returns(Promise.resolve());
const result = await migrationsDir.doesSampleMigrationExist();
expect(result).to.equal(true);
});

it("should return false if sample migration doesn't exists", async () => {
fs.stat.returns(Promise.reject(new Error("It does not exist")));
const result = await migrationsDir.doesSampleMigrationExist();
expect(result).to.equal(false);
});
});
});