-
Notifications
You must be signed in to change notification settings - Fork 225
/
custom-state-storage.js
66 lines (55 loc) · 1.58 KB
/
custom-state-storage.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
'use strict'
const migrate = require('migrate')
const { Client } = require('pg')
const pg = new Client()
/**
* Stores and loads the executed migrations in the database. The table
* migrations is only one row and stores a JSON of the data that the
* migrate package uses to know which migrations have been executed.
*/
const customStateStorage = {
load: async function (fn) {
await pg.connect()
// Load the single row of migration data from the database
const { rows } = await pg.query('SELECT data FROM schema.migrations')
if (rows.length !== 1) {
console.log('Cannot read migrations from database. If this is the first time you run migrations, then this is normal.')
return fn(null, {})
}
// Call callback with new migration data object
await pg.end()
fn(null, rows[0].data)
},
save: async function (set, fn) {
await pg.connect()
// Check if table 'migrations' exists and if not, create it.
await pg.query('CREATE TABLE IF NOT EXISTS schema.migrations (id integer PRIMARY KEY, data jsonb NOT NULL)')
await pg.query(`
INSERT INTO schema.migrations (id, data)
VALUES (1, $1)
ON CONFLICT (id) DO UPDATE SET data = $1
`, [{
lastRun: set.lastRun,
migrations: set.migrations
}])
await pg.end()
fn()
}
}
/**
* Main application code
*/
migrate.load({
// Set class as custom stateStore
stateStore: customStateStorage
}, function (err, set) {
if (err) {
throw err
}
set.up((err2) => {
if (err2) {
throw err2
}
console.log('Migrations successfully ran')
})
})