forked from rickbergfalk/postgrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgrator.js
283 lines (270 loc) · 8.36 KB
/
postgrator.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const EventEmitter = require('events')
const commonClient = require('./lib/commonClient.js')
const {
fileChecksum,
checksum,
sortMigrationsAsc,
sortMigrationsDesc
} = require('./lib/utils.js')
const DEFAULT_CONFIG = {
schemaTable: 'schemaversion',
validateChecksums: true
}
class Postgrator extends EventEmitter {
constructor(config) {
super()
this.config = Object.assign({}, DEFAULT_CONFIG, config)
this.migrations = []
this.commonClient = commonClient(this.config)
}
/**
* Reads all migrations from directory
*
* @returns {Promise} array of migration objects
*/
getMigrations() {
const { migrationDirectory, migrationPattern, newline } = this.config
this.migrations = []
return new Promise((resolve, reject) => {
const loader = (err, files) => {
if (err) {
return reject(err)
}
resolve(files)
}
if (migrationPattern) {
glob(migrationPattern, loader)
} else if (migrationDirectory) {
fs.readdir(migrationDirectory, loader)
} else {
resolve([])
}
}).then(migrationFiles => {
migrationFiles.forEach(file => {
const m = file
.split('/')
.pop()
.split('.')
const name = m.length >= 3 ? m.slice(2, m.length - 1).join('.') : file
const filename = migrationPattern
? file
: path.join(migrationDirectory, file)
if (m[m.length - 1] === 'sql') {
this.migrations.push({
version: Number(m[0]),
action: m[1],
filename: file,
name: name,
md5: fileChecksum(filename, newline),
getSql: () => fs.readFileSync(filename, 'utf8')
})
} else if (m[m.length - 1] === 'js') {
const jsModule = require(filename)
const sql = jsModule.generateSql()
this.migrations.push({
version: Number(m[0]),
action: m[1],
filename: file,
name: name,
md5: checksum(sql, newline),
getSql: () => sql
})
}
})
this.migrations = this.migrations.filter(
migration => !isNaN(migration.version)
)
return this.migrations
})
}
/**
* Executes sql query using the common client and ends connection afterwards
*
* @returns {Promise} result of query
* @param {String} query sql query to execute
*/
runQuery(query) {
const { commonClient } = this
return commonClient.runQuery(query).then(results => {
return commonClient.endConnection().then(() => results)
})
}
/**
* Gets the database version of the schema from the database.
* Otherwise 0 if no version has been run
*
* @returns {Promise} database schema version
*/
getDatabaseVersion() {
const { runQuery, endConnection, queries } = this.commonClient
return runQuery(queries.getDatabaseVersion).then(result => {
const version = result.rows.length > 0 ? result.rows[0].version : 0
return endConnection().then(() => version)
})
}
/**
* Returns an object with max version of migration available
*
* @returns {Promise}
*/
getMaxVersion() {
const { migrations } = this
return Promise.resolve()
.then(() => {
if (migrations.length) {
return migrations
} else {
return this.getMigrations()
}
})
.then(migrations => {
const versions = migrations.map(migration => migration.version)
return Math.max.apply(null, versions)
})
}
/**
* Validate md5 checksums for applied migrations
*
* @returns {Promise}
* @param {Number} databaseVersion
*/
validateMigrations(databaseVersion) {
return this.getMigrations().then(migrations => {
const validateMigrations = migrations.filter(
migration =>
migration.action === 'do' &&
migration.version > 0 &&
migration.version <= databaseVersion
)
let sequence = Promise.resolve()
validateMigrations.forEach(migration => {
sequence = sequence
.then(() => this.emit('validation-started', migration))
.then(() => {
const sql = this.commonClient.queries.getMd5(migration)
return this.commonClient.runQuery(sql)
})
.then(results => {
const md5 = results.rows && results.rows[0] && results.rows[0].md5
if (md5 !== migration.md5) {
const msg = `MD5 checksum failed for migration [${
migration.version
}]`
throw new Error(msg)
}
})
.then(() => this.emit('validation-finished', migration))
})
return sequence.then(() => validateMigrations)
})
}
/**
* Runs the migrations in the order to reach target version
*
* @returns {Promise} - Array of migration objects to appled to database
* @param {Array} migrations - Array of migration objects to apply to database
*/
runMigrations(migrations = []) {
const { commonClient } = this
let sequence = Promise.resolve()
const appliedMigrations = []
migrations.forEach(migration => {
sequence = sequence
.then(() => this.emit('migration-started', migration))
.then(() => migration.getSql())
.then(sql => commonClient.runQuery(sql))
.then(() =>
commonClient.runQuery(commonClient.persistActionSql(migration))
)
.then(() => appliedMigrations.push(migration))
.then(() => this.emit('migration-finished', migration))
})
return sequence.then(() => appliedMigrations).catch(error => {
error.appliedMigrations = appliedMigrations
throw error
})
}
/**
* returns an array of relevant migrations based on the target and database version passed.
* returned array is sorted in the order it needs to be run
*
* @returns {Array} Sorted array of relevant migration objects
* @param {Number} databaseVersion
* @param {Number} targetVersion
*/
getRunnableMigrations(databaseVersion, targetVersion) {
const { migrations } = this
if (targetVersion >= databaseVersion) {
return migrations
.filter(
migration =>
migration.action === 'do' &&
migration.version > databaseVersion &&
migration.version <= targetVersion
)
.sort(sortMigrationsAsc)
}
if (targetVersion < databaseVersion) {
return migrations
.filter(
migration =>
migration.action === 'undo' &&
migration.version <= databaseVersion &&
migration.version > targetVersion
)
.sort(sortMigrationsDesc)
}
return []
}
/**
* Main method to move a schema to a particular version.
* A target must be specified, otherwise nothing is run.
*
* @returns {Promise}
* @param {String} target - version to migrate as string or number (handled as numbers internally)
*/
migrate(target = '') {
const { commonClient, config } = this
const data = {}
return commonClient
.ensureTable()
.then(() => this.getMigrations())
.then(() => {
const cleaned = target.toLowerCase().trim()
if (cleaned === 'max' || cleaned === '') {
return this.getMaxVersion()
}
return Number(target)
})
.then(targetVersion => {
data.targetVersion = targetVersion
if (target === undefined) {
throw new Error('targetVersion undefined')
}
return this.getDatabaseVersion()
})
.then(databaseVersion => {
data.databaseVersion = databaseVersion
if (config.validateChecksums && data.targetVersion >= databaseVersion) {
return this.validateMigrations(databaseVersion)
}
})
.then(() =>
this.getRunnableMigrations(data.databaseVersion, data.targetVersion)
)
.then(runnableMigrations => this.runMigrations(runnableMigrations))
.then(migrations => commonClient.endConnection().then(() => migrations))
.catch(error => {
// Decorate error with empty appliedMigrations if not yet exist
// Rethrow error to module user
if (!error.appliedMigrations) {
error.appliedMigrations = []
}
throw error
})
}
}
module.exports = Postgrator