Repository navigation
Expand file tree
/
Copy pathsettings.js
More file actions
124 lines (105 loc) · 3.83 KB
/
Copy pathsettings.js
File metadata and controls
124 lines (105 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const ObjectId = require('bson-objectid').default;
const logging = require('@tryghost/logging');
const { createTransactionalMigration } = require('./migrations');
const MIGRATION_USER = 1;
/**
* Creates a migration which will insert a new setting in settings table
* @param {object} settingSpec - setting type and group
* @param {string} settingSpec.key - settings key
* @param {*} settingSpec.value - settings value
* @param {'array' | 'string' | 'number' | 'boolean' | 'object'} settingSpec.type - settings type
* @param {string} settingSpec.group - settings group
* @param {'PUBLIC' | 'RO' | 'PUBLIC,RO'} [settingSpec.flags] - settings flag
* @returns {Object} migration object returning config/up/down properties
*/
function addSetting({ key, value, type, group, flags = null }) {
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');
const data = {
id: ObjectId().toHexString(),
key,
value,
group,
type,
flags,
created_at: now,
};
if (await connection.schema.hasColumn('settings', 'created_by')) {
data.created_by = MIGRATION_USER;
}
if (await connection.schema.hasColumn('settings', 'updated_by')) {
data.updated_by = MIGRATION_USER;
}
return connection('settings').insert(data);
},
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();
},
);
}
/**
* @param {string} key - The key of the setting to remove
* @returns {Object} - A migration object with up and down functions
*/
function removeSetting(key) {
let originalSetting = null;
return createTransactionalMigration(
async function up(connection) {
const settingExists = await connection('settings').where('key', '=', key).first();
if (!settingExists) {
logging.warn(`Skipping removing setting: ${key} - setting does not exist`);
return;
}
// Store the original setting data for the down migration
originalSetting = settingExists;
logging.info(`Removing setting: ${key}`);
return connection('settings').where('key', '=', key).del();
},
async function down(connection) {
const settingExists = await connection('settings').where('key', '=', key).first();
if (settingExists) {
logging.warn(`Skipping restoring setting: ${key} - setting already exists`);
return;
}
if (!originalSetting) {
logging.warn(`Skipping restoring setting: ${key} - no original setting data found`);
return;
}
logging.info(`Restoring setting: ${key}`);
const now = connection.raw('CURRENT_TIMESTAMP');
const data = {
id: ObjectId().toHexString(),
key,
value: originalSetting.value,
group: originalSetting.group,
type: originalSetting.type,
flags: originalSetting.flags,
created_at: now,
};
if (await connection.schema.hasColumn('settings', 'created_by')) {
data.created_by = MIGRATION_USER;
}
if (await connection.schema.hasColumn('settings', 'updated_by')) {
data.updated_by = MIGRATION_USER;
}
return connection('settings').insert(data);
},
);
}
module.exports = {
addSetting,
removeSetting,
};