-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
sql_migrations_test.ts
800 lines (680 loc) · 20.8 KB
/
sql_migrations_test.ts
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import type { Database } from 'better-sqlite3';
import SQL from 'better-sqlite3';
import { v4 as generateGuid } from 'uuid';
import { SCHEMA_VERSIONS } from '../sql/migrations';
import { consoleLogger } from '../util/consoleLogger';
const OUR_UUID = generateGuid();
describe('SQL migrations test', () => {
let db: Database;
const updateToVersion = (version: number) => {
const startVersion = db.pragma('user_version', { simple: true });
for (const run of SCHEMA_VERSIONS) {
run(startVersion, db, consoleLogger);
const currentVersion = db.pragma('user_version', { simple: true });
if (currentVersion === version) {
return;
}
}
throw new Error(`Migration to ${version} not found`);
};
const addOurUuid = () => {
const value = {
id: 'uuid_id',
value: `${OUR_UUID}.1`,
};
db.exec(
`
INSERT INTO items (id, json) VALUES
('uuid_id', '${JSON.stringify(value)}');
`
);
};
const parseItems = (
items: ReadonlyArray<{ json: string }>
): Array<unknown> => {
return items.map(item => {
return {
...item,
json: JSON.parse(item.json),
};
});
};
const insertSession = (
conversationId: string,
deviceId: number,
data: Record<string, unknown> = {}
): void => {
const id = `${conversationId}.${deviceId}`;
db.prepare(
`
INSERT INTO sessions (id, conversationId, json)
VALUES ($id, $conversationId, $json)
`
).run({
id,
conversationId,
json: JSON.stringify({
...data,
id,
conversationId,
}),
});
};
beforeEach(() => {
db = new SQL(':memory:');
});
afterEach(() => {
db.close();
});
describe('updateToSchemaVersion41', () => {
const THEIR_UUID = generateGuid();
const THEIR_CONVO = generateGuid();
const ANOTHER_CONVO = generateGuid();
const THIRD_CONVO = generateGuid();
it('clears sessions and keys if UUID is not available', () => {
updateToVersion(40);
db.exec(
`
INSERT INTO senderKeys
(id, senderId, distributionId, data, lastUpdatedDate)
VALUES
('1', '1', '1', '1', 1);
INSERT INTO sessions (id, conversationId, json) VALUES
('1', '1', '{}');
INSERT INTO signedPreKeys (id, json) VALUES
('1', '{}');
INSERT INTO preKeys (id, json) VALUES
('1', '{}');
INSERT INTO items (id, json) VALUES
('identityKey', '{}'),
('registrationId', '{}');
`
);
const senderKeyCount = db
.prepare('SELECT COUNT(*) FROM senderKeys')
.pluck();
const sessionCount = db.prepare('SELECT COUNT(*) FROM sessions').pluck();
const signedPreKeyCount = db
.prepare('SELECT COUNT(*) FROM signedPreKeys')
.pluck();
const preKeyCount = db.prepare('SELECT COUNT(*) FROM preKeys').pluck();
const itemCount = db.prepare('SELECT COUNT(*) FROM items').pluck();
assert.strictEqual(senderKeyCount.get(), 1);
assert.strictEqual(sessionCount.get(), 1);
assert.strictEqual(signedPreKeyCount.get(), 1);
assert.strictEqual(preKeyCount.get(), 1);
assert.strictEqual(itemCount.get(), 2);
updateToVersion(41);
assert.strictEqual(senderKeyCount.get(), 0);
assert.strictEqual(sessionCount.get(), 0);
assert.strictEqual(signedPreKeyCount.get(), 0);
assert.strictEqual(preKeyCount.get(), 0);
assert.strictEqual(itemCount.get(), 0);
});
it('adds prefix to preKeys/signedPreKeys', () => {
updateToVersion(40);
addOurUuid();
const signedKeyItem = { id: 1 };
const preKeyItem = { id: 2 };
db.exec(
`
INSERT INTO signedPreKeys (id, json) VALUES
(1, '${JSON.stringify(signedKeyItem)}');
INSERT INTO preKeys (id, json) VALUES
(2, '${JSON.stringify(preKeyItem)}');
`
);
updateToVersion(41);
assert.deepStrictEqual(
parseItems(db.prepare('SELECT * FROM signedPreKeys').all()),
[
{
id: `${OUR_UUID}:1`,
json: {
id: `${OUR_UUID}:1`,
keyId: 1,
ourUuid: OUR_UUID,
},
},
]
);
assert.deepStrictEqual(
parseItems(db.prepare('SELECT * FROM preKeys').all()),
[
{
id: `${OUR_UUID}:2`,
json: {
id: `${OUR_UUID}:2`,
keyId: 2,
ourUuid: OUR_UUID,
},
},
]
);
});
it('migrates senderKeys', () => {
updateToVersion(40);
addOurUuid();
db.exec(
`
INSERT INTO conversations (id, uuid) VALUES
('${THEIR_CONVO}', '${THEIR_UUID}');
INSERT INTO senderKeys
(id, senderId, distributionId, data, lastUpdatedDate)
VALUES
('${THEIR_CONVO}.1--234', '${THEIR_CONVO}.1', '234', '1', 1);
`
);
updateToVersion(41);
assert.deepStrictEqual(db.prepare('SELECT * FROM senderKeys').all(), [
{
id: `${OUR_UUID}:${THEIR_UUID}.1--234`,
distributionId: '234',
data: '1',
lastUpdatedDate: 1,
senderId: `${THEIR_UUID}.1`,
},
]);
});
it('removes senderKeys that do not have conversation uuid', () => {
updateToVersion(40);
addOurUuid();
db.exec(
`
INSERT INTO conversations (id) VALUES
('${THEIR_CONVO}');
INSERT INTO senderKeys
(id, senderId, distributionId, data, lastUpdatedDate)
VALUES
('${THEIR_CONVO}.1--234', '${THEIR_CONVO}.1', '234', '1', 1),
('${ANOTHER_CONVO}.1--234', '${ANOTHER_CONVO}.1', '234', '1', 1);
`
);
updateToVersion(41);
assert.strictEqual(
db.prepare('SELECT COUNT(*) FROM senderKeys').pluck().get(),
0
);
});
it('correctly merges senderKeys for conflicting conversations', () => {
updateToVersion(40);
addOurUuid();
const fullA = generateGuid();
const fullB = generateGuid();
const fullC = generateGuid();
const partial = generateGuid();
// When merging two keys for different conversations with the same uuid
// only the most recent key would be kept in the database. We prefer keys
// with either:
//
// 1. more recent lastUpdatedDate column
// 2. conversation with both e164 and uuid
// 3. conversation with more recent active_at
db.exec(
`
INSERT INTO conversations (id, uuid, e164, active_at) VALUES
('${fullA}', '${THEIR_UUID}', '+12125555555', 1),
('${fullB}', '${THEIR_UUID}', '+12125555555', 2),
('${fullC}', '${THEIR_UUID}', '+12125555555', 3),
('${partial}', '${THEIR_UUID}', NULL, 3);
INSERT INTO senderKeys
(id, senderId, distributionId, data, lastUpdatedDate)
VALUES
('${fullA}.1--234', '${fullA}.1', 'fullA', '1', 1),
('${fullC}.1--234', '${fullC}.1', 'fullC', '2', 2),
('${fullB}.1--234', '${fullB}.1', 'fullB', '3', 2),
('${partial}.1--234', '${partial}.1', 'partial', '4', 2);
`
);
updateToVersion(41);
assert.deepStrictEqual(db.prepare('SELECT * FROM senderKeys').all(), [
{
id: `${OUR_UUID}:${THEIR_UUID}.1--234`,
senderId: `${THEIR_UUID}.1`,
distributionId: 'fullC',
lastUpdatedDate: 2,
data: '2',
},
]);
});
it('migrates sessions', () => {
updateToVersion(40);
addOurUuid();
db.exec(
`
INSERT INTO conversations (id, uuid) VALUES
('${THEIR_CONVO}', '${THEIR_UUID}');
`
);
insertSession(THEIR_CONVO, 1);
updateToVersion(41);
assert.deepStrictEqual(
parseItems(db.prepare('SELECT * FROM sessions').all()),
[
{
conversationId: THEIR_CONVO,
id: `${OUR_UUID}:${THEIR_UUID}.1`,
uuid: THEIR_UUID,
ourUuid: OUR_UUID,
json: {
id: `${OUR_UUID}:${THEIR_UUID}.1`,
conversationId: THEIR_CONVO,
uuid: THEIR_UUID,
ourUuid: OUR_UUID,
},
},
]
);
});
it('removes sessions that do not have conversation id', () => {
updateToVersion(40);
addOurUuid();
insertSession(THEIR_CONVO, 1);
updateToVersion(41);
assert.strictEqual(
db.prepare('SELECT COUNT(*) FROM sessions').pluck().get(),
0
);
});
it('removes sessions that do not have conversation uuid', () => {
updateToVersion(40);
addOurUuid();
db.exec(
`
INSERT INTO conversations (id) VALUES ('${THEIR_CONVO}');
`
);
insertSession(THEIR_CONVO, 1);
updateToVersion(41);
assert.strictEqual(
db.prepare('SELECT COUNT(*) FROM sessions').pluck().get(),
0
);
});
it('correctly merges sessions for conflicting conversations', () => {
updateToVersion(40);
addOurUuid();
const fullA = generateGuid();
const fullB = generateGuid();
const partial = generateGuid();
// Similar merging logic to senderkeys above. We prefer sessions with
// either:
//
// 1. conversation with both e164 and uuid
// 2. conversation with more recent active_at
db.exec(
`
INSERT INTO conversations (id, uuid, e164, active_at) VALUES
('${fullA}', '${THEIR_UUID}', '+12125555555', 1),
('${fullB}', '${THEIR_UUID}', '+12125555555', 2),
('${partial}', '${THEIR_UUID}', NULL, 3);
`
);
insertSession(fullA, 1, { name: 'A' });
insertSession(fullB, 1, { name: 'B' });
insertSession(partial, 1, { name: 'C' });
updateToVersion(41);
assert.deepStrictEqual(
parseItems(db.prepare('SELECT * FROM sessions').all()),
[
{
id: `${OUR_UUID}:${THEIR_UUID}.1`,
conversationId: fullB,
ourUuid: OUR_UUID,
uuid: THEIR_UUID,
json: {
id: `${OUR_UUID}:${THEIR_UUID}.1`,
conversationId: fullB,
ourUuid: OUR_UUID,
uuid: THEIR_UUID,
name: 'B',
},
},
]
);
});
it('moves identity key and registration id into a map', () => {
updateToVersion(40);
addOurUuid();
const items = [
{ id: 'identityKey', value: 'secret' },
{ id: 'registrationId', value: 42 },
];
for (const item of items) {
db.prepare(
`
INSERT INTO items (id, json) VALUES ($id, $json);
`
).run({
id: item.id,
json: JSON.stringify(item),
});
}
updateToVersion(41);
assert.deepStrictEqual(
parseItems(db.prepare('SELECT * FROM items ORDER BY id').all()),
[
{
id: 'identityKeyMap',
json: {
id: 'identityKeyMap',
value: { [OUR_UUID]: 'secret' },
},
},
{
id: 'registrationIdMap',
json: {
id: 'registrationIdMap',
value: { [OUR_UUID]: 42 },
},
},
{
id: 'uuid_id',
json: {
id: 'uuid_id',
value: `${OUR_UUID}.1`,
},
},
]
);
});
it("migrates other users' identity keys", () => {
updateToVersion(40);
addOurUuid();
db.exec(
`
INSERT INTO conversations (id, uuid) VALUES
('${THEIR_CONVO}', '${THEIR_UUID}'),
('${ANOTHER_CONVO}', NULL);
`
);
const identityKeys = [
{ id: THEIR_CONVO },
{ id: ANOTHER_CONVO },
{ id: THIRD_CONVO },
];
for (const key of identityKeys) {
db.prepare(
`
INSERT INTO identityKeys (id, json) VALUES ($id, $json);
`
).run({
id: key.id,
json: JSON.stringify(key),
});
}
updateToVersion(41);
assert.deepStrictEqual(
parseItems(db.prepare('SELECT * FROM identityKeys ORDER BY id').all()),
[
{
id: THEIR_UUID,
json: {
id: THEIR_UUID,
},
},
{
id: `conversation:${ANOTHER_CONVO}`,
json: {
id: `conversation:${ANOTHER_CONVO}`,
},
},
{
id: `conversation:${THIRD_CONVO}`,
json: {
id: `conversation:${THIRD_CONVO}`,
},
},
].sort((a, b) => {
if (a.id === b.id) {
return 0;
}
if (a.id < b.id) {
return -1;
}
return 1;
})
);
});
});
describe('updateToSchemaVersion42', () => {
const MESSAGE_ID_1 = generateGuid();
const MESSAGE_ID_2 = generateGuid();
const MESSAGE_ID_3 = generateGuid();
const MESSAGE_ID_4 = generateGuid();
const CONVERSATION_ID = generateGuid();
it('deletes orphaned reactions', () => {
updateToVersion(41);
db.exec(
`
INSERT INTO messages
(id, conversationId, body)
VALUES
('${MESSAGE_ID_1}', '${CONVERSATION_ID}', 'message number 1'),
('${MESSAGE_ID_2}', '${CONVERSATION_ID}', 'message number 2');
INSERT INTO reactions (messageId, conversationId) VALUES
('${MESSAGE_ID_1}', '${CONVERSATION_ID}'),
('${MESSAGE_ID_2}', '${CONVERSATION_ID}'),
('${MESSAGE_ID_3}', '${CONVERSATION_ID}'),
('${MESSAGE_ID_4}', '${CONVERSATION_ID}');
`
);
const reactionCount = db
.prepare('SELECT COUNT(*) FROM reactions;')
.pluck();
const messageCount = db.prepare('SELECT COUNT(*) FROM messages;').pluck();
assert.strictEqual(reactionCount.get(), 4);
assert.strictEqual(messageCount.get(), 2);
updateToVersion(42);
assert.strictEqual(reactionCount.get(), 2);
assert.strictEqual(messageCount.get(), 2);
const reactionMessageIds = db
.prepare('SELECT messageId FROM reactions;')
.pluck()
.all();
assert.sameDeepMembers(reactionMessageIds, [MESSAGE_ID_1, MESSAGE_ID_2]);
});
it('new message delete trigger deletes reactions as well', () => {
updateToVersion(41);
db.exec(
`
INSERT INTO messages
(id, conversationId, body)
VALUES
('${MESSAGE_ID_1}', '${CONVERSATION_ID}', 'message number 1'),
('${MESSAGE_ID_2}', '${CONVERSATION_ID}', 'message number 2'),
('${MESSAGE_ID_3}', '${CONVERSATION_ID}', 'message number 3');
INSERT INTO reactions (messageId, conversationId) VALUES
('${MESSAGE_ID_1}', '${CONVERSATION_ID}'),
('${MESSAGE_ID_2}', '${CONVERSATION_ID}'),
('${MESSAGE_ID_3}', '${CONVERSATION_ID}');
`
);
const reactionCount = db
.prepare('SELECT COUNT(*) FROM reactions;')
.pluck();
const messageCount = db.prepare('SELECT COUNT(*) FROM messages;').pluck();
assert.strictEqual(reactionCount.get(), 3);
assert.strictEqual(messageCount.get(), 3);
updateToVersion(42);
assert.strictEqual(reactionCount.get(), 3);
assert.strictEqual(messageCount.get(), 3);
db.exec(
`
DELETE FROM messages WHERE id = '${MESSAGE_ID_1}';
`
);
assert.strictEqual(reactionCount.get(), 2);
assert.strictEqual(messageCount.get(), 2);
const reactionMessageIds = db
.prepare('SELECT messageId FROM reactions;')
.pluck()
.all();
assert.sameDeepMembers(reactionMessageIds, [MESSAGE_ID_2, MESSAGE_ID_3]);
});
});
describe('updateToSchemaVersion43', () => {
it('remaps conversation ids to UUIDs in groups and messages', () => {
updateToVersion(42);
const UUID_A = generateGuid();
const UUID_B = generateGuid();
const UUID_C = generateGuid();
const rawConvoC = {
id: 'c',
uuid: UUID_C,
membersV2: [
{ conversationId: 'a', joinedAtVersion: 1 },
{ conversationId: 'b', joinedAtVersion: 2 },
{ conversationId: 'z', joinedAtVersion: 3 },
],
pendingMembersV2: [
{ conversationId: 'a', addedByUserId: 'b', timestamp: 4 },
{ conversationId: 'b', addedByUserId: UUID_A, timestamp: 5 },
{ conversationId: 'z', timestamp: 6 },
],
pendingAdminApprovalV2: [
{ conversationId: 'a', timestamp: 6 },
{ conversationId: 'b', timestamp: 7 },
{ conversationId: 'z', timestamp: 8 },
],
};
const CHANGE_TYPES = [
'member-add',
'member-add-from-link',
'member-add-from-admin-approval',
'member-privilege',
'member-remove',
'pending-add-one',
'admin-approval-add-one',
];
const CHANGE_TYPES_WITH_INVITER = [
'member-add-from-invite',
'pending-remove-one',
'pending-remove-many',
'admin-approval-remove-one',
];
db.exec(
`
INSERT INTO conversations
(id, uuid, json)
VALUES
('a', '${UUID_A}', '${JSON.stringify({ id: 'a', uuid: UUID_A })}'),
('b', '${UUID_B}', '${JSON.stringify({ id: 'b', uuid: UUID_B })}'),
('c', '${UUID_C}', '${JSON.stringify(rawConvoC)}');
INSERT INTO messages
(id, json)
VALUES
('m', '${JSON.stringify({
id: 'm',
groupV2Change: {
from: 'a',
details: [
...CHANGE_TYPES.map(type => ({ type, conversationId: 'b' })),
...CHANGE_TYPES_WITH_INVITER.map(type => {
return { type, conversationId: 'c', inviter: 'a' };
}),
],
},
sourceUuid: 'a',
invitedGV2Members: [
{
conversationId: 'b',
addedByUserId: 'c',
},
],
})}'),
('n', '${JSON.stringify({
id: 'n',
groupV2Change: {
from: 'not-found',
details: [],
},
sourceUuid: 'a',
})}');
`
);
updateToVersion(43);
const { members, json: convoJSON } = db
.prepare('SELECT members, json FROM conversations WHERE id = "c"')
.get();
assert.strictEqual(members, `${UUID_A} ${UUID_B}`);
assert.deepStrictEqual(JSON.parse(convoJSON), {
id: 'c',
uuid: UUID_C,
membersV2: [
{ uuid: UUID_A, joinedAtVersion: 1 },
{ uuid: UUID_B, joinedAtVersion: 2 },
],
pendingMembersV2: [
{ uuid: UUID_A, addedByUserId: UUID_B, timestamp: 4 },
{ uuid: UUID_B, addedByUserId: UUID_A, timestamp: 5 },
],
pendingAdminApprovalV2: [
{ uuid: UUID_A, timestamp: 6 },
{ uuid: UUID_B, timestamp: 7 },
],
});
const { json: messageMJSON } = db
.prepare('SELECT json FROM messages WHERE id = "m"')
.get();
assert.deepStrictEqual(JSON.parse(messageMJSON), {
id: 'm',
groupV2Change: {
from: UUID_A,
details: [
...CHANGE_TYPES.map(type => ({ type, uuid: UUID_B })),
...CHANGE_TYPES_WITH_INVITER.map(type => {
return {
type,
uuid: UUID_C,
inviter: UUID_A,
};
}),
],
},
sourceUuid: UUID_A,
invitedGV2Members: [
{
uuid: UUID_B,
addedByUserId: UUID_C,
},
],
});
const { json: messageNJSON } = db
.prepare('SELECT json FROM messages WHERE id = "n"')
.get();
assert.deepStrictEqual(JSON.parse(messageNJSON), {
id: 'n',
groupV2Change: {
details: [],
},
sourceUuid: UUID_A,
});
});
it('should not fail on invalid UUIDs', () => {
updateToVersion(42);
db.exec(
`
INSERT INTO messages
(id, json)
VALUES
('m', '${JSON.stringify({
id: 'm',
sourceUuid: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
})}');
`
);
updateToVersion(43);
const { json: messageMJSON } = db
.prepare('SELECT json FROM messages WHERE id = "m"')
.get();
assert.deepStrictEqual(JSON.parse(messageMJSON), {
id: 'm',
sourceUuid: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
});
});
});
});