Skip to content

Commit c1af61e

Browse files
kirrg001acburdine
authored andcommitted
fix(v2): enforce v1 => v2 migration rules with zips
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
1 parent 93d191f commit c1af61e

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

lib/utils/version-from-zip.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const errors = require('../errors');
55
const semver = require('semver');
66
const AdmZip = require('adm-zip');
77
const cliPackage = require('../../package.json');
8+
const resolveVersion = require('./resolve-version');
89

910
module.exports = function versionFromZip(zipPath, currentVersion, force = false) {
1011
if (!path.isAbsolute(zipPath)) {
@@ -47,11 +48,31 @@ module.exports = function versionFromZip(zipPath, currentVersion, force = false)
4748
}));
4849
}
4950

50-
if (force && semver.lt(pkg.version, currentVersion)) {
51-
return Promise.reject(
52-
new errors.SystemError('Zip file contains an older release version than what is currently installed.')
53-
);
54-
}
51+
return resolveVersion(null, currentVersion, true)
52+
.then((latestV1ReleaseVersion) => {
53+
// CASE: if major diff and you are not on the latest v1
54+
if (currentVersion && semver.major(currentVersion) !== semver.major(pkg.version) &&
55+
currentVersion !== latestV1ReleaseVersion) {
56+
return Promise.reject(new errors.SystemError({
57+
message: 'You are about to migrate to Ghost 2.0. Your blog is not on the latest Ghost 1.0 version.',
58+
help: 'Please run "ghost update --v1".'
59+
}));
60+
}
61+
62+
if (force && semver.lt(pkg.version, currentVersion)) {
63+
return Promise.reject(
64+
new errors.SystemError('Zip file contains an older release version than what is currently installed.')
65+
);
66+
}
67+
68+
return Promise.resolve(pkg.version);
69+
})
70+
.catch((err) => {
71+
// CASE: you are on latest v1, but you want to manually migrate to v2 using a zip
72+
if (err.message.match(/No valid versions found/) && semver.major(currentVersion) !== semver.major(pkg.version)) {
73+
return Promise.resolve(pkg.version);
74+
}
5575

56-
return Promise.resolve(pkg.version);
76+
throw err;
77+
});
5778
};

test/fixtures/ghost-2.0.zip

640 Bytes
Binary file not shown.

test/unit/utils/version-from-zip-spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@ describe('Unit: Utils > versionFromZip', function () {
2424
});
2525
});
2626

27+
it('rejects if you are not on the latest v1 release and you are trying to jump to the next major', function () {
28+
const resolveVersionStub = sinon.stub().resolves('1.25.4');
29+
const versionFromZip = proxyquire(modulePath, {
30+
'./resolve-version': resolveVersionStub
31+
});
32+
33+
return versionFromZip(path.join(__dirname, '../../fixtures/ghost-2.0.zip'), '1.20.0').then(() => {
34+
expect(false, 'error should have been thrown').to.be.true;
35+
}).catch((error) => {
36+
expect(error).to.be.an.instanceof(errors.SystemError);
37+
expect(error.message).to.match(/You are about to migrate to Ghost 2.0/);
38+
});
39+
});
40+
41+
it('resolves if you are on the latest v1 release and you are trying to jump to the next major', function () {
42+
const resolveVersionStub = sinon.stub().rejects(new Error('No valid versions found.'));
43+
const versionFromZip = proxyquire(modulePath, {
44+
'./resolve-version': resolveVersionStub
45+
});
46+
47+
return versionFromZip(path.join(__dirname, '../../fixtures/ghost-2.0.zip'), '1.25.4');
48+
});
49+
2750
it('rejects if zip file doesn\'t have a .zip extension', function () {
2851
const existsStub = sinon.stub().returns(true);
2952
const versionFromZip = proxyquire(modulePath, {

0 commit comments

Comments
 (0)