Repository navigation
Expand file tree
/
Copy pathmigrations.js
More file actions
157 lines (147 loc) · 3.45 KB
/
Copy pathmigrations.js
File metadata and controls
157 lines (147 loc) · 3.45 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const errors = require('@tryghost/errors');
/**
* @param {(connection: import('knex').Knex) => Promise<void>} up
* @param {(connection: import('knex').Knex) => Promise<void>} down
*
* @returns {Migration}
*/
function createNonTransactionalMigration(up, down) {
return {
config: {
transaction: false,
},
async up(config) {
await up(config.connection);
},
async down(config) {
await down(config.connection);
},
};
}
/**
* @param {(connection: import('knex').Knex) => Promise<void>} up
*
* @returns {Migration}
*/
function createIrreversibleMigration(up) {
return {
config: {
irreversible: true,
},
async up(config) {
await up(config.connection);
},
async down() {
return Promise.reject();
},
};
}
/**
* @param {(connection: import('knex').Knex) => Promise<void>} up
* @param {(connection: import('knex').Knex) => Promise<void>} down
*
* @returns {Migration}
*/
function createTransactionalMigration(up, down) {
return {
config: {
transaction: true,
},
async up(config) {
await up(config.transacting);
},
async down(config) {
await down(config.transacting);
},
};
}
/**
* @param {Migration[]} migrations
*
* @returns {Migration}
*/
function combineTransactionalMigrations(...migrations) {
return {
config: {
transaction: true,
},
async up(config) {
for (const migration of migrations) {
await migration.up(config);
}
},
async down(config) {
// Down migrations must be run backwards!!
const reverseMigrations = migrations.slice().reverse();
for (const migration of reverseMigrations) {
await migration.down(config);
}
},
};
}
/**
* @param {Migration[]} migrations
*
* @returns {Migration}
*/
function combineNonTransactionalMigrations(...migrations) {
return {
config: {
transaction: false,
},
async up(config) {
for (const migration of migrations) {
await migration.up(config);
}
},
async down(config) {
// Down migrations must be run backwards!!
const reverseMigrations = migrations.slice().reverse();
for (const migration of reverseMigrations) {
await migration.down(config);
}
},
};
}
/**
* @param {number} major
*/
function createFinalMigration(major) {
return createTransactionalMigration(
async function up() {
throw new errors.InternalServerError({
message: `Unable to run migrations`,
context: `You must be on the latest v${major}.x to update across major versions - https://docs.ghost.org/update/`,
help: `Run 'ghost update v${major}' to get the latest v${major}.x version, then run 'ghost update' to get to the latest.`,
});
},
async function down() {
// no-op
},
);
}
module.exports = {
createFinalMigration,
createTransactionalMigration,
createNonTransactionalMigration,
createIrreversibleMigration,
combineTransactionalMigrations,
combineNonTransactionalMigrations,
};
/**
* @typedef {Object} TransactionalMigrationFunctionOptions
*
* @prop {import('knex').Knex} transacting
*/
/**
* @typedef {(options: TransactionalMigrationFunctionOptions) => Promise<void>} TransactionalMigrationFunction
*/
/**
* @typedef {Object} Migration
*
* @prop {Object} config
* @prop {boolean} config.transaction
*
* @prop {TransactionalMigrationFunction} up
* @prop {TransactionalMigrationFunction} down
*/