forked from LiskArchive/lisk-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrations.js
154 lines (144 loc) · 4.19 KB
/
migrations.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
/*
* Copyright © 2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
'use strict';
const path = require('path');
const fs = require('fs-extra');
const Promise = require('bluebird');
const sql = require('../sql').migrations;
const { sqlRoot } = require('../sql/config');
/**
* Database migrations interaction class.
*
* @class
* @memberof db.repos
* @requires fs-extra
* @requires path
* @requires db/sql/config
* @requires db/sql/index.migrations
* @see Parent: {@link db.repos}
* @param {Database} db - Instance of database object from pg-promise
* @param {Object} pgp - pg-promise instance to utilize helpers
* @returns {Object} An instance of a MigrationsRepository
*/
class MigrationsRepository {
constructor(db, pgp) {
this.db = db;
this.pgp = pgp;
this.inTransaction = !!(db.ctx && db.ctx.inTransaction);
}
/**
* Verifies presence of the 'migrations' OID named relation.
*
* @returns {Promise<boolean>} Promise object that resolves with a boolean.
*/
hasMigrations() {
return this.db.proc(
'to_regclass',
'migrations',
a => (a ? !!a.to_regclass : false)
);
}
/**
* Gets id of the last migration record, or 0, if none exist.
*
* @returns {Promise<number>}
* Promise object that resolves with either 0 or id of the last migration record.
*/
getLastId() {
return this.db.oneOrNone(sql.getLastId, [], a => (a ? +a.id : 0));
}
/**
* Executes 'migrations/runtime.sql' file, to set peers clock to null and state to 1.
*
* @returns {Promise<null>} Promise object that resolves with `null`.
*/
applyRuntime() {
// Must use a transaction here when not in one:
const job = t => t.none(sql.runtime);
return this.inTransaction ? job(this.db) : this.db.tx('applyRuntime', job);
}
/**
* Executes 'migrations/memoryTables.sql' file, to create and configure all memory tables.
*
* @returns {Promise<null>} Promise object that resolves with `null`.
*/
createMemoryTables() {
// Must use a transaction here when not in one:
const job = t => t.none(sql.memoryTables);
return this.inTransaction
? job(this.db)
: this.db.tx('createMemoryTables', job);
}
/**
* Reads 'sql/migrations/updates' folder and returns an array of objects for further processing.
*
* @param {number} lastMigrationId
* @returns {Promise<Array<Object>>}
* Promise object that resolves with an array of objects `{id, name, path, file}`.
*/
readPending(lastMigrationId) {
const updatesPath = path.join(sqlRoot, 'migrations/updates');
return fs.readdir(updatesPath).then(files =>
files
.map(f => {
const m = f.match(/(\d+)_(.+).sql/);
return (
m && {
id: m[1],
name: m[2],
path: path.join(updatesPath, f),
}
);
})
.sort((a, b) => a.id - b.id) // Sort by migration ID, ascending
.filter(
f =>
f &&
fs.statSync(f.path).isFile() &&
(!lastMigrationId || +f.id > lastMigrationId)
)
.map(f => {
f.file = new this.pgp.QueryFile(f.path, {
minify: true,
noWarnings: true,
});
return f;
})
);
}
/**
* Applies a cumulative update: all pending migrations + runtime.
* Each update+insert execute within their own SAVEPOINT, to ensure data integrity on the updates level.
*
* @returns {Promise} Promise object that resolves with `undefined`.
*/
applyAll() {
return this.db.tx('migrations:applyAll', t1 =>
t1.migrations
.hasMigrations()
.then(hasMigrations => (hasMigrations ? t1.migrations.getLastId() : 0))
.then(lastId => t1.migrations.readPending(lastId))
.then(updates =>
Promise.mapSeries(updates, u => {
const tag = `update:${u.name}`;
return t1.tx(tag, t2 =>
t2.none(u.file).then(() => t2.none(sql.add, u))
);
})
)
.then(() => t1.migrations.applyRuntime())
);
}
}
module.exports = MigrationsRepository;