-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
Copy pathbackup.test.js
279 lines (260 loc) · 10.5 KB
/
backup.test.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
var sqlite3 = require('..');
var assert = require('assert');
var fs = require('fs');
var helper = require('./support/helper');
// Check that the number of rows in two tables matches.
function assertRowsMatchDb(db1, table1, db2, table2, done) {
db1.get("SELECT COUNT(*) as count FROM " + table1, function(err, row) {
if (err) throw err;
db2.get("SELECT COUNT(*) as count FROM " + table2, function(err, row2) {
if (err) throw err;
assert.equal(row.count, row2.count);
done();
});
});
}
// Check that the number of rows in the table "foo" is preserved in a backup.
function assertRowsMatchFile(db, backupName, done) {
var db2 = new sqlite3.Database(backupName, sqlite3.OPEN_READONLY, function(err) {
if (err) throw err;
assertRowsMatchDb(db, 'foo', db2, 'foo', function() {
db2.close(done);
});
});
}
describe('backup', function() {
before(function() {
helper.ensureExists('test/tmp');
});
var db;
beforeEach(function(done) {
helper.deleteFile('test/tmp/backup.db');
helper.deleteFile('test/tmp/backup2.db');
db = new sqlite3.Database('test/support/prepare.db', sqlite3.OPEN_READONLY, done);
});
afterEach(function(done) {
if (!db) { done(); }
db.close(done);
});
it ('output db created once step is called', function(done) {
var backup = db.backup('test/tmp/backup.db', function(err) {
if (err) throw err;
backup.step(1, function(err) {
if (err) throw err;
assert.fileExists('test/tmp/backup.db');
backup.finish(done);
});
});
});
it ('copies source fully with step(-1)', function(done) {
var backup = db.backup('test/tmp/backup.db');
backup.step(-1, function(err) {
if (err) throw err;
assert.fileExists('test/tmp/backup.db');
backup.finish(function(err) {
if (err) throw err;
assertRowsMatchFile(db, 'test/tmp/backup.db', done);
});
});
});
it ('backup db not created if finished immediately', function(done) {
var backup = db.backup('test/tmp/backup.db');
backup.finish(function(err) {
if (err) throw err;
assert.fileDoesNotExist('test/tmp/backup.db');
done();
});
});
it ('error closing db if backup not finished', function(done) {
var backup = db.backup('test/tmp/backup.db');
db.close(function(err) {
db = null;
if (!err) throw new Error('should have an error');
if (err.errno == sqlite3.BUSY) {
done();
}
else throw err;
});
});
it ('using the backup after finished is an error', function(done) {
var backup = db.backup('test/tmp/backup.db');
backup.finish(function(err) {
if (err) throw err;
backup.step(1, function(err) {
if (!err) throw new Error('should have an error');
if (err.errno == sqlite3.MISUSE &&
err.message === 'SQLITE_MISUSE: Backup is already finished') {
done();
}
else throw err;
});
});
});
it ('remaining/pageCount are available after call to step', function(done) {
var backup = db.backup('test/tmp/backup.db');
backup.step(0, function(err) {
if (err) throw err;
assert.equal(typeof this.pageCount, 'number');
assert.equal(typeof this.remaining, 'number');
assert.equal(this.remaining, this.pageCount);
var prevRemaining = this.remaining;
var prevPageCount = this.pageCount;
backup.step(1, function(err) {
if (err) throw err;
assert.notEqual(this.remaining, prevRemaining);
assert.equal(this.pageCount, prevPageCount);
backup.finish(done);
});
});
});
it ('backup works if database is modified half-way through', function(done) {
var backup = db.backup('test/tmp/backup.db');
backup.step(-1, function(err) {
if (err) throw err;
backup.finish(function(err) {
if (err) throw err;
var db2 = new sqlite3.Database('test/tmp/backup.db', function(err) {
if (err) throw err;
var backup2 = db2.backup('test/tmp/backup2.db');
backup2.step(1, function(err, completed) {
if (err) throw err;
assert.equal(completed, false); // Page size for the test db
// should not be raised to high.
db2.exec("insert into foo(txt) values('hello')", function(err) {
if (err) throw err;
backup2.step(-1, function(err, completed) {
if (err) throw err;
assert.equal(completed, true);
assertRowsMatchFile(db2, 'test/tmp/backup2.db', function() {
backup2.finish(function(err) {
if (err) throw err;
db2.close(done);
});
});
});
});
});
});
});
});
});
(sqlite3.VERSION_NUMBER < 3026000 ? it.skip : it) ('can backup from temp to main', function(done) {
db.exec("CREATE TEMP TABLE space (txt TEXT)", function(err) {
if (err) throw err;
db.exec("INSERT INTO space(txt) VALUES('monkey')", function(err) {
if (err) throw err;
var backup = db.backup('test/tmp/backup.db', 'temp', 'main', true, function(err) {
if (err) throw err;
backup.step(-1, function(err) {
if (err) throw err;
backup.finish(function(err) {
if (err) throw err;
var db2 = new sqlite3.Database('test/tmp/backup.db', function(err) {
if (err) throw err;
db2.get("SELECT * FROM space", function(err, row) {
if (err) throw err;
assert.equal(row.txt, 'monkey');
db2.close(done);
});
});
});
});
});
});
});
});
(sqlite3.VERSION_NUMBER < 3026000 ? it.skip : it) ('can backup from main to temp', function(done) {
var backup = db.backup('test/support/prepare.db', 'main', 'temp', false, function(err) {
if (err) throw err;
backup.step(-1, function(err) {
if (err) throw err;
backup.finish(function(err) {
if (err) throw err;
assertRowsMatchDb(db, 'temp.foo', db, 'main.foo', done);
});
});
});
});
it ('cannot backup to a locked db', function(done) {
var db2 = new sqlite3.Database('test/tmp/backup.db', function(err) {
db2.exec("PRAGMA locking_mode = EXCLUSIVE");
db2.exec("BEGIN EXCLUSIVE", function(err) {
if (err) throw err;
var backup = db.backup('test/tmp/backup.db');
backup.step(-1, function(stepErr) {
db2.close(function(err) {
if (err) throw err;
if (stepErr.errno == sqlite3.BUSY) {
backup.finish(done);
}
else throw stepErr;
});
});
});
});
});
it ('fuss-free incremental backups work', function(done) {
var backup = db.backup('test/tmp/backup.db');
var timer;
function makeProgress() {
if (backup.idle) {
backup.step(1);
}
if (backup.completed || backup.failed) {
clearInterval(timer);
assert.equal(backup.completed, true);
assert.equal(backup.failed, false);
done();
}
}
timer = setInterval(makeProgress, 2);
});
it ('setting retryErrors to empty disables automatic finishing', function(done) {
var backup = db.backup('test/tmp/backup.db');
backup.retryErrors = [];
backup.step(-1, function(err) {
if (err) throw err;
db.close(function(err) {
db = null;
if (!err) throw new Error('should have an error');
assert.equal(err.errno, sqlite3.BUSY);
done();
});
});
});
it ('setting retryErrors enables automatic finishing', function(done) {
var backup = db.backup('test/tmp/backup.db');
backup.retryErrors = [sqlite3.OK];
backup.step(-1, function(err) {
if (err) throw err;
db.close(function(err) {
if (err) throw err;
db = null;
done();
});
});
});
it ('default retryErrors will retry on a locked/busy db', function(done) {
var db2 = new sqlite3.Database('test/tmp/backup.db', function(err) {
db2.exec("PRAGMA locking_mode = EXCLUSIVE");
db2.exec("BEGIN EXCLUSIVE", function(err) {
if (err) throw err;
var backup = db.backup('test/tmp/backup.db');
backup.step(-1, function(stepErr) {
db2.close(function(err) {
if (err) throw err;
assert.equal(stepErr.errno, sqlite3.BUSY);
assert.equal(backup.completed, false);
assert.equal(backup.failed, false);
backup.step(-1, function(err) {
if (err) throw err;
assert.equal(backup.completed, true);
assert.equal(backup.failed, false);
done();
});
});
});
});
});
});
});