Skip to content

Commit f052df6

Browse files
committed
fix(ui): extract database migrations into a task
no issue - hide migration output - extract migration code into a task
1 parent da3bac6 commit f052df6

File tree

2 files changed

+59
-42
lines changed

2 files changed

+59
-42
lines changed

lib/commands/start.js

+10-42
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22
const fs = require('fs');
33
const path = require('path');
4-
const KnexMigrator = require('knex-migrator');
54

65
const Config = require('../utils/config');
76
const Command = require('../command');
8-
const errors = require('../errors');
97
const startupChecks = require('./doctor/checks/startup');
8+
const runMigrations = require('../tasks/migrate');
109

1110
class StartCommand extends Command {
1211
run(argv) {
@@ -21,50 +20,19 @@ class StartCommand extends Command {
2120
process.env.NODE_ENV = this.environment = 'development';
2221
}
2322

24-
return this.ui.listr(startupChecks, {environment: this.environment}).then(() => {
25-
config = Config.load(this.environment);
26-
cliConfig = Config.load('.ghost-cli');
23+
config = Config.load(this.environment);
24+
cliConfig = Config.load('.ghost-cli');
2725

28-
if (cliConfig.has('running')) {
29-
return Promise.reject(new Error('Ghost is already running.'));
30-
}
26+
if (cliConfig.has('running')) {
27+
return Promise.reject(new Error('Ghost is already running.'));
28+
}
3129

30+
return this.ui.listr(startupChecks.concat({
31+
title: 'Running database migrations',
32+
task: runMigrations
33+
}), {environment: this.environment, config: config}).then(() => {
3234
this.service.setConfig(config);
3335

34-
process.env.paths__contentPath = path.join(process.cwd(), 'content');
35-
36-
let knexMigrator = new KnexMigrator({
37-
knexMigratorFilePath: path.join(process.cwd(), 'current')
38-
});
39-
40-
return knexMigrator.isDatabaseOK().catch((error) => {
41-
if (error.code === 'DB_NOT_INITIALISED' ||
42-
error.code === 'MIGRATION_TABLE_IS_MISSING') {
43-
return knexMigrator.init();
44-
} else if (error.code === 'DB_NEEDS_MIGRATION') {
45-
return knexMigrator.migrate();
46-
}
47-
48-
if (error.code === 'ENOTFOUND') {
49-
// Database not found
50-
error = new errors.ConfigError({
51-
configKey: 'database.connection.host',
52-
configValue: config.get('database.connection.host'),
53-
message: 'Invalid database host',
54-
environment: this.environment
55-
});
56-
} else if (error.code === 'ER_ACCESS_DENIED_ERROR') {
57-
error = new errors.ConfigError({
58-
configKey: 'database.connection.user',
59-
configValue: config.get('database.connection.user'),
60-
message: 'Invalid database username or password',
61-
environment: this.environment
62-
});
63-
}
64-
65-
return Promise.reject(error);
66-
});
67-
}).then(() => {
6836
let start = () => Promise.resolve(this.service.process.start(process.cwd(), this.environment)).then(() => {
6937
let systemConfig = Config.load('system');
7038
let instance = systemConfig.get(`instances.${config.get('pname')}`, {});

lib/tasks/migrate.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
const path = require('path');
3+
const KnexMigrator = require('knex-migrator');
4+
5+
const errors = require('../errors');
6+
7+
module.exports = function runMigrations(context) {
8+
process.env.paths__contentPath = path.join(process.cwd(), 'content');
9+
let config = context.config;
10+
11+
let transports = config.get('logging.transports', null);
12+
config.set('logging.transports', ['file']).save();
13+
14+
let knexMigrator = new KnexMigrator({
15+
knexMigratorFilePath: path.join(process.cwd(), 'current')
16+
});
17+
18+
return knexMigrator.isDatabaseOK().catch((error) => {
19+
if (error.code === 'DB_NOT_INITIALISED' ||
20+
error.code === 'MIGRATION_TABLE_IS_MISSING') {
21+
return knexMigrator.init();
22+
} else if (error.code === 'DB_NEEDS_MIGRATION') {
23+
return knexMigrator.migrate();
24+
}
25+
26+
if (error.code === 'ENOTFOUND') {
27+
// Database not found
28+
error = new errors.ConfigError({
29+
configKey: 'database.connection.host',
30+
configValue: config.get('database.connection.host'),
31+
message: 'Invalid database host',
32+
environment: context.environment
33+
});
34+
} else if (error.code === 'ER_ACCESS_DENIED_ERROR') {
35+
error = new errors.ConfigError({
36+
configKey: 'database.connection.user',
37+
configValue: config.get('database.connection.user'),
38+
message: 'Invalid database username or password',
39+
environment: context.environment
40+
});
41+
}
42+
43+
config.set('logging.transports', transports).save();
44+
return Promise.reject(error);
45+
}).then(() => {
46+
config.set('logging.transports', transports).save();
47+
});
48+
}
49+

0 commit comments

Comments
 (0)