Skip to content

Commit

Permalink
fix(v2): enforce v1 => v2 migration rules with zips
Browse files Browse the repository at this point in the history
refs #759
- helpful for testing
- otherwise we allow a direct jump from e.g. 1.20 to 2.0 and this will crash
- it will crash because Ghost v2 can't execute v1 migration scripts, because the context/code has changed
  • Loading branch information
kirrg001 authored and acburdine committed Aug 16, 2018
1 parent 6d9c8cb commit 8845f9e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lib/utils/version-from-zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const errors = require('../errors');
const semver = require('semver');
const AdmZip = require('adm-zip');
const cliPackage = require('../../package.json');
const resolveVersion = require('./resolve-version');

module.exports = function versionFromZip(zipPath, currentVersion, force = false) {
if (!path.isAbsolute(zipPath)) {
Expand Down Expand Up @@ -47,11 +48,31 @@ module.exports = function versionFromZip(zipPath, currentVersion, force = false)
}));
}

if (force && semver.lt(pkg.version, currentVersion)) {
return Promise.reject(
new errors.SystemError('Zip file contains an older release version than what is currently installed.')
);
}
return resolveVersion(null, currentVersion, true)
.then((latestV1ReleaseVersion) => {
// CASE: if major diff and you are not on the latest v1
if (currentVersion && semver.major(currentVersion) !== semver.major(pkg.version) &&
currentVersion !== latestV1ReleaseVersion) {
return Promise.reject(new errors.SystemError({
message: 'You are about to migrate to Ghost 2.0. Your blog is not on the latest Ghost 1.0 version.',
help: 'Please run "ghost update --v1".'
}));
}

if (force && semver.lt(pkg.version, currentVersion)) {
return Promise.reject(
new errors.SystemError('Zip file contains an older release version than what is currently installed.')
);
}

return Promise.resolve(pkg.version);
})
.catch((err) => {
// CASE: you are on latest v1, but you want to manually migrate to v2 using a zip
if (err.message.match(/No valid versions found/) && semver.major(currentVersion) !== semver.major(pkg.version)) {
return Promise.resolve(pkg.version);
}

return Promise.resolve(pkg.version);
throw err;
});
};
Binary file added test/fixtures/ghost-2.0.zip
Binary file not shown.
23 changes: 23 additions & 0 deletions test/unit/utils/version-from-zip-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ describe('Unit: Utils > versionFromZip', function () {
});
});

it('rejects if you are not on the latest v1 release and you are trying to jump to the next major', function () {
const resolveVersionStub = sinon.stub().resolves('1.25.4');
const versionFromZip = proxyquire(modulePath, {
'./resolve-version': resolveVersionStub
});

return versionFromZip(path.join(__dirname, '../../fixtures/ghost-2.0.zip'), '1.20.0').then(() => {
expect(false, 'error should have been thrown').to.be.true;
}).catch((error) => {
expect(error).to.be.an.instanceof(errors.SystemError);
expect(error.message).to.match(/You are about to migrate to Ghost 2.0/);
});
});

it('resolves if you are on the latest v1 release and you are trying to jump to the next major', function () {
const resolveVersionStub = sinon.stub().rejects(new Error('No valid versions found.'));
const versionFromZip = proxyquire(modulePath, {
'./resolve-version': resolveVersionStub
});

return versionFromZip(path.join(__dirname, '../../fixtures/ghost-2.0.zip'), '1.25.4');
});

it('rejects if zip file doesn\'t have a .zip extension', function () {
const existsStub = sinon.stub().returns(true);
const versionFromZip = proxyquire(modulePath, {
Expand Down

0 comments on commit 8845f9e

Please sign in to comment.