fixed migration and setup

more info: https://github.com/knex/knex/issues/2820
This commit is contained in:
Gergo Torcsvari 2020-07-19 16:35:13 +02:00
parent e2e9835d01
commit f8edeb2775
2 changed files with 137 additions and 119 deletions

View File

@ -22,22 +22,6 @@ exports.up = function (knex/*, Promise*/) {
}) })
.then(() => { .then(() => {
logger.info('[' + migrate_name + '] setting Table created'); logger.info('[' + migrate_name + '] setting Table created');
// TODO: add settings
let settingModel = require('../models/setting');
return settingModel
.query()
.insert({
id: 'default-site',
name: 'Default Site',
description: 'What to show when Nginx is hit with an unknown Host',
value: 'congratulations',
meta: {}
});
})
.then(() => {
logger.info('[' + migrate_name + '] Default settings added');
}); });
}; };

View File

@ -5,10 +5,10 @@ const logger = require('./logger').setup;
const userModel = require('./models/user'); const userModel = require('./models/user');
const userPermissionModel = require('./models/user_permission'); const userPermissionModel = require('./models/user_permission');
const authModel = require('./models/auth'); const authModel = require('./models/auth');
const settingModel = require('./models/setting');
const debug_mode = process.env.NODE_ENV !== 'production' || !!process.env.DEBUG; const debug_mode = process.env.NODE_ENV !== 'production' || !!process.env.DEBUG;
module.exports = function () { function setupJwt(resolve, reject) {
return new Promise((resolve, reject) => {
// Now go and check if the jwt gpg keys have been created and if not, create them // Now go and check if the jwt gpg keys have been created and if not, create them
if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) { if (!config.has('jwt') || !config.has('jwt.key') || !config.has('jwt.pub')) {
logger.info('Creating a new JWT key pair...'); logger.info('Creating a new JWT key pair...');
@ -56,15 +56,15 @@ module.exports = function () {
resolve(); resolve();
} }
}) }
.then(() => {
return userModel function setupDefaultUser() {
(userModel
.query() .query()
.select(userModel.raw('COUNT(`id`) as `count`')) .select(userModel.raw('COUNT(`id`) as `count`'))
.where('is_deleted', 0) .where('is_deleted', 0)
.first(); .first()
}) ).then( (row) => {
.then((row) => {
if (!row.count) { if (!row.count) {
// Create a new user and set password // Create a new user and set password
logger.info('Creating a new user: admin@example.com with password: changeme'); logger.info('Creating a new user: admin@example.com with password: changeme');
@ -106,10 +106,44 @@ module.exports = function () {
}); });
}) })
.then(() => { .then(() => {
logger.info('Initial setup completed'); logger.info('Initial admin setup completed');
}); });
} else if (debug_mode) { } else if (debug_mode) {
logger.debug('Admin user setup not required'); logger.debug('Admin user setup not required');
} }
}); });
}
function setupDefaultSettings() {
return settingModel
.query()
.select(userModel.raw('COUNT(`id`) as `count`'))
.first()
.then( (row) => {
if (!row.count) {
settingModel
.query()
.insert({
id: 'default-site',
name: 'Default Site',
description: 'What to show when Nginx is hit with an unknown Host',
value: 'congratulations',
meta: {}
}).then(() => {
logger.info('Default settings added');
});
} if (debug_mode) {
logger.debug('Default setting setup not required');
}
});
}
module.exports = function () {
return new Promise((resolve, reject) => {
return setupJwt(resolve, reject);
}).then(() => {
return setupDefaultUser();
}).then(() => {
return setupDefaultSettings();
});
}; };