Skip to content

Commit

Permalink
fix(v2): changed major version comparison
Browse files Browse the repository at this point in the history
refs TryGhost#759
- `semver.satisfies(version, '^2.0.0')` doesn't work with release candidates
- switch to compare the major version with `semver.major`
  • Loading branch information
kirrg001 authored and acburdine committed Aug 16, 2018
1 parent ba8a35e commit c4b9e07
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class SetupCommand extends Command {
task: migrator.migrate,
// CASE: We are about to install Ghost 2.0. We moved the execution of knex-migrator into Ghost.
enabled: () => {
if (semver.satisfies(instance.cliConfig.get('active-version'), '^2.0.0')) {
if (semver.major(instance.cliConfig.get('active-version')) === 2) {
return false;
}

Expand Down
9 changes: 5 additions & 4 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class UpdateCommand extends Command {
task: majorUpdate,
// CASE: Skip if you are already on ^2 or you update from v1 to v1.
enabled: () => {
if (semver.satisfies(instance.cliConfig.get('active-version'), '^2.0.0') ||
!semver.satisfies(context.version, '^2.0.0')) {
if (semver.major(instance.cliConfig.get('active-version')) === 2 ||
semver.major(context.version) === 1) {
return false;
}

Expand All @@ -96,9 +96,10 @@ class UpdateCommand extends Command {
task: migrator.migrate,
// CASE: We have moved the execution of knex-migrator into Ghost 2.0.0.
// If you are already on v2 or you update from v1 to v2, then skip the task.
// We compare the major versions, otherwise pre-releases won't match.
enabled: () => {
if (semver.satisfies(instance.cliConfig.get('active-version'), '^2.0.0') ||
semver.satisfies(context.version, '^2.0.0')) {
if (semver.major(instance.cliConfig.get('active-version')) === 2 ||
semver.major(context.version) === 2) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/process-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ProcessManager {
stopOnError: true,
port: this.instance.config.get('server.port'),
host: this.instance.config.get('server.host', 'localhost'),
useNetServer: semver.satisfies(this.instance.cliConfig.get('active-version'), '^2.0.0')
useNetServer: semver.major(this.instance.cliConfig.get('active-version')) === 2
}, options || {});

return portPolling(options).catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module.exports.rollback = function runRollback(context) {

// Ghost 2.0.0 uses the new knex-migrator version. We have to ensure you can still use CLI 1.9 with an older blog, because
// we haven't restricted a CLI version range in Ghost (we only used cli: > X.X.X)
if (semver.satisfies(context.instance.cliConfig.get('active-version'), '^2.0.0')) {
if (semver.major(context.instance.cliConfig.get('active-version')) === 2) {
args = ['--force', '--v', context.instance.cliConfig.get('previous-version'), '--mgpath', currentDir];
} else {
args = ['--force', '--mgpath', currentDir];
Expand Down
16 changes: 8 additions & 8 deletions test/unit/tasks/migrator-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,15 @@ describe('Unit: Tasks > Migrator', function () {
});

describe('rollback', function () {
let cliConfig;

beforeEach(function () {
cliConfig = configStub();
cliConfig.get.withArgs('active-version').returns('1.25.3');
});

it('runs direct command if useGhostUser returns false', function () {
const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().resolves();
const useGhostUserStub = sinon.stub().returns(false);

Expand All @@ -181,7 +187,7 @@ describe('Unit: Tasks > Migrator', function () {
});
});

it('forward version option to knex-migrator if blog is on v1', function () {
it('forward version option to knex-migrator if blog jumps from v1 to v2', function () {
const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().resolves();
Expand Down Expand Up @@ -209,7 +215,6 @@ describe('Unit: Tasks > Migrator', function () {

it('runs sudo command if useGhostUser returns true', function () {
const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().resolves();
const useGhostUserStub = sinon.stub().returns(true);

Expand All @@ -230,7 +235,6 @@ describe('Unit: Tasks > Migrator', function () {

it('throws config error with db host if database not found', function () {
const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().returns(Promise.reject({stderr: 'CODE: ENOTFOUND'}));
const useGhostUserStub = sinon.stub().returns(false);

Expand All @@ -249,7 +253,6 @@ describe('Unit: Tasks > Migrator', function () {

it('throws config error with db user if access denied error', function () {
const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().returns(Promise.reject({stderr: 'CODE: ER_ACCESS_DENIED_ERROR'}));
const useGhostUserStub = sinon.stub().returns(false);

Expand All @@ -268,7 +271,6 @@ describe('Unit: Tasks > Migrator', function () {

it('throws system error if sqlite3 error is thrown by knex', function () {
const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().returns(Promise.reject({stdout: 'Knex: run\n$ npm install sqlite3 --save\nError:'}));
const useGhostUserStub = sinon.stub().returns(false);

Expand All @@ -287,7 +289,6 @@ describe('Unit: Tasks > Migrator', function () {

it('knex-migrator complains that no more migrations to rollback available', function () {
const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().returns(Promise.reject({stderr: 'No migrations available to rollback'}));
const useGhostUserStub = sinon.stub().returns(false);

Expand All @@ -305,7 +306,6 @@ describe('Unit: Tasks > Migrator', function () {
process.argv = ['node', 'ghost', 'update', '--rollback'];

const config = configStub();
const cliConfig = configStub();
const execaStub = sinon.stub().rejects({stderr: 'YA_GOOFED'});
const useGhostUserStub = sinon.stub().returns(false);

Expand Down

0 comments on commit c4b9e07

Please sign in to comment.