0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00
ghost/core/server/data/migrations/utils/settings.js
Daniel Lockyer bf28721844 De-duped MIGRATION_USER constant across migration utils
- we shouldn't declare this in more than one place, even if it is a
  weird concept anyway
2022-05-10 13:05:47 +01:00

59 lines
1.9 KiB
JavaScript

const ObjectId = require('bson-objectid').default;
const logging = require('@tryghost/logging');
const {createTransactionalMigration} = require('./migrations');
const {MIGRATION_USER} = require('./constants');
/**
* Creates a migration which will insert a new setting in settings table
* @param {object} settingSpec - setting key, value, group and type
*
* @returns {Object} migration object returning config/up/down properties
*/
function addSetting({key, value, type, group}) {
return createTransactionalMigration(
async function up(connection) {
const settingExists = await connection('settings')
.where('key', '=', key)
.first();
if (settingExists) {
logging.warn(`Skipping adding setting: ${key} - setting already exists`);
return;
}
logging.info(`Adding setting: ${key}`);
const now = connection.raw('CURRENT_TIMESTAMP');
return connection('settings')
.insert({
id: ObjectId().toHexString(),
key,
value,
group,
type,
created_at: now,
created_by: MIGRATION_USER,
updated_at: now,
updated_by: MIGRATION_USER
});
},
async function down(connection) {
const settingExists = await connection('settings')
.where('key', '=', key)
.first();
if (!settingExists) {
logging.warn(`Skipping dropping setting: ${key} - setting does not exist`);
return;
}
logging.info(`Dropping setting: ${key}`);
return connection('settings')
.where('key', '=', key)
.del();
}
);
}
module.exports = {
addSetting
};