2020-04-29 18:44:27 +03:00
|
|
|
const KnexMigrator = require('knex-migrator');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-04-29 18:44:27 +03:00
|
|
|
|
|
|
|
const knexMigrator = new KnexMigrator({
|
|
|
|
knexMigratorFilePath: config.get('paths:appRoot')
|
|
|
|
});
|
2018-07-17 19:10:05 +03:00
|
|
|
|
|
|
|
module.exports.getState = () => {
|
2020-04-29 18:44:27 +03:00
|
|
|
let state;
|
|
|
|
let err;
|
2018-07-17 19:10:05 +03:00
|
|
|
|
|
|
|
return knexMigrator.isDatabaseOK()
|
|
|
|
.then(() => {
|
|
|
|
state = 1;
|
|
|
|
return state;
|
|
|
|
})
|
|
|
|
.catch((_err) => {
|
|
|
|
err = _err;
|
|
|
|
|
|
|
|
// CASE: database was never created
|
|
|
|
if (err.code === 'DB_NOT_INITIALISED') {
|
|
|
|
state = 2;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: you have created the database on your own, you have an existing none compatible db?
|
|
|
|
if (err.code === 'MIGRATION_TABLE_IS_MISSING') {
|
|
|
|
state = 3;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: database needs migrations
|
|
|
|
if (err.code === 'DB_NEEDS_MIGRATION') {
|
|
|
|
state = 4;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: database connection errors, unknown cases
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.dbInit = () => {
|
|
|
|
return knexMigrator.init();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.migrate = () => {
|
|
|
|
return knexMigrator.migrate();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.isDbCompatible = (connection) => {
|
|
|
|
return connection.raw('SELECT `key` FROM settings WHERE `key`="databaseVersion";')
|
|
|
|
.then((response) => {
|
|
|
|
if (!response || !response[0].length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.DatabaseVersionError({
|
2018-07-17 19:10:05 +03:00
|
|
|
message: 'Your database version is not compatible with Ghost 2.0.',
|
|
|
|
help: 'Want to keep your DB? Use Ghost < 1.0.0 or the "0.11" branch.' +
|
|
|
|
'\n\n\n' +
|
2019-07-22 13:17:50 +03:00
|
|
|
'Want to migrate Ghost 0.11 to 2.0? Please visit https://ghost.org/faq/upgrade-to-ghost-1-0/'
|
2018-07-17 19:10:05 +03:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
// CASE settings table doesn't exists
|
|
|
|
if (err.errno === 1146 || err.errno === 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
};
|