forked from oxen-io/session-desktop
/
sessionMigrations.ts
1239 lines (1085 loc) · 42.3 KB
/
sessionMigrations.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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as BetterSqlite3 from 'better-sqlite3';
import { compact, map, pick } from 'lodash';
import { ConversationAttributes } from '../../models/conversationAttributes';
import {
CLOSED_GROUP_V2_KEY_PAIRS_TABLE,
CONVERSATIONS_TABLE,
dropFtsAndTriggers,
GUARD_NODE_TABLE,
jsonToObject,
LAST_HASHES_TABLE,
MESSAGES_TABLE,
NODES_FOR_PUBKEY_TABLE,
objectToJSON,
OPEN_GROUP_ROOMS_V2_TABLE,
rebuildFtsTable,
} from '../database_utility';
import { sqlNode } from '../sql';
// tslint:disable: no-console quotemark one-variable-per-declaration
function getSessionSchemaVersion(db: BetterSqlite3.Database) {
const result = db
.prepare(
`
SELECT MAX(version) as version FROM loki_schema;
`
)
.get();
if (!result || !result.version) {
return 0;
}
return result.version;
}
function createSessionSchemaTable(db: BetterSqlite3.Database) {
db.transaction(() => {
db.exec(`
CREATE TABLE loki_schema(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
version INTEGER
);
INSERT INTO loki_schema (
version
) values (
0
);
`);
})();
}
const LOKI_SCHEMA_VERSIONS = [
updateToSessionSchemaVersion1,
updateToSessionSchemaVersion2,
updateToSessionSchemaVersion3,
updateToSessionSchemaVersion4,
updateToSessionSchemaVersion5,
updateToSessionSchemaVersion6,
updateToSessionSchemaVersion7,
updateToSessionSchemaVersion8,
updateToSessionSchemaVersion9,
updateToSessionSchemaVersion10,
updateToSessionSchemaVersion11,
updateToSessionSchemaVersion12,
updateToSessionSchemaVersion13,
updateToSessionSchemaVersion14,
updateToSessionSchemaVersion15,
updateToSessionSchemaVersion16,
updateToSessionSchemaVersion17,
updateToSessionSchemaVersion18,
updateToSessionSchemaVersion19,
updateToSessionSchemaVersion20,
updateToSessionSchemaVersion21,
updateToSessionSchemaVersion22,
updateToSessionSchemaVersion23,
updateToSessionSchemaVersion24,
updateToSessionSchemaVersion25,
updateToSessionSchemaVersion26,
updateToSessionSchemaVersion27,
updateToSessionSchemaVersion28,
updateToSessionSchemaVersion29,
];
function updateToSessionSchemaVersion1(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 1;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
ALTER TABLE ${MESSAGES_TABLE}
ADD COLUMN serverId INTEGER;
CREATE TABLE servers(
serverUrl STRING PRIMARY KEY ASC,
token TEXT
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion2(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 2;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
CREATE TABLE pairingAuthorisations(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
primaryDevicePubKey VARCHAR(255),
secondaryDevicePubKey VARCHAR(255),
isGranted BOOLEAN,
json TEXT,
UNIQUE(primaryDevicePubKey, secondaryDevicePubKey)
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion3(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 3;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
CREATE TABLE ${GUARD_NODE_TABLE}(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
ed25519PubKey VARCHAR(64)
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion4(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 4;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
DROP TABLE ${LAST_HASHES_TABLE};
CREATE TABLE ${LAST_HASHES_TABLE}(
id TEXT,
snode TEXT,
hash TEXT,
expiresAt INTEGER,
PRIMARY KEY (id, snode)
);
-- Add senderIdentity field to unprocessed needed for medium size groups
ALTER TABLE unprocessed ADD senderIdentity TEXT;
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion5(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 5;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
CREATE TABLE ${NODES_FOR_PUBKEY_TABLE} (
pubkey TEXT PRIMARY KEY,
json TEXT
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion6(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 6;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
-- Remove RSS Feed conversations
DELETE FROM ${CONVERSATIONS_TABLE} WHERE
type = 'group' AND
id LIKE 'rss://%';
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion7(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 7;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
-- Remove multi device data
DELETE FROM pairingAuthorisations;
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion8(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 8;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
ALTER TABLE ${MESSAGES_TABLE}
ADD COLUMN serverTimestamp INTEGER;
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion9(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 9;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
const rows = db
.prepare(
`SELECT * FROM ${CONVERSATIONS_TABLE} WHERE
type = 'group' AND
id LIKE '__textsecure_group__!%';
`
)
.all();
const conversationIdRows = db
.prepare(`SELECT id FROM ${CONVERSATIONS_TABLE} ORDER BY id ASC;`)
.all();
const allOldConversationIds = map(conversationIdRows, row => row.id);
rows.forEach(o => {
const oldId = o.id;
const newId = oldId.replace('__textsecure_group__!', '');
console.log(`migrating conversation, ${oldId} to ${newId}`);
if (allOldConversationIds.includes(newId)) {
console.log(
'Found a duplicate conversation after prefix removing. We need to take care of it'
);
// We have another conversation with the same future name.
// We decided to keep only the conversation with the higher number of messages
const countMessagesOld = sqlNode.getMessagesCountByConversation(oldId, db);
const countMessagesNew = sqlNode.getMessagesCountByConversation(newId, db);
console.log(`countMessagesOld: ${countMessagesOld}, countMessagesNew: ${countMessagesNew}`);
const deleteId = countMessagesOld > countMessagesNew ? newId : oldId;
db.prepare(`DELETE FROM ${CONVERSATIONS_TABLE} WHERE id = $deleteId;`).run({ deleteId });
}
const morphedObject = {
...o,
id: newId,
};
db.prepare(
`UPDATE ${CONVERSATIONS_TABLE} SET
id = $newId,
json = $json
WHERE id = $oldId;`
).run({
newId,
json: objectToJSON(morphedObject),
oldId,
});
});
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion10(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 10;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
CREATE TABLE ${CLOSED_GROUP_V2_KEY_PAIRS_TABLE} (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
groupPublicKey TEXT,
timestamp NUMBER,
json TEXT
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion11(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 11;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
function remove05PrefixFromStringIfNeeded(str: string) {
if (str.length === 66 && str.startsWith('05')) {
return str.substr(2);
}
return str;
}
db.transaction(() => {
// the migration is called only once, so all current groups not being open groups are v1 closed group.
const allClosedGroupV1Ids = db
.prepare(
`SELECT id FROM ${CONVERSATIONS_TABLE} WHERE
type = 'group' AND
id NOT LIKE 'publicChat:%';`
)
.all()
.map(m => m.id) as Array<string>;
allClosedGroupV1Ids.forEach(groupV1Id => {
try {
console.log('Migrating closed group v1 to v2: pubkey', groupV1Id);
const groupV1IdentityKey = sqlNode.getIdentityKeyById(groupV1Id, db);
if (!groupV1IdentityKey) {
return;
}
const encryptionPubKeyWithoutPrefix = remove05PrefixFromStringIfNeeded(
groupV1IdentityKey.id
);
// Note:
// this is what we get from getIdentityKeyById:
// {
// id: string;
// secretKey?: string;
// }
// and this is what we want saved in db:
// {
// publicHex: string; // without prefix
// privateHex: string;
// }
const keyPair = {
publicHex: encryptionPubKeyWithoutPrefix,
privateHex: groupV1IdentityKey.secretKey,
};
sqlNode.addClosedGroupEncryptionKeyPair(groupV1Id, keyPair, db);
} catch (e) {
console.error(e);
}
});
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion12(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 12;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
CREATE TABLE ${OPEN_GROUP_ROOMS_V2_TABLE} (
serverUrl TEXT NOT NULL,
roomId TEXT NOT NULL,
conversationId TEXT,
json TEXT,
PRIMARY KEY (serverUrl, roomId)
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion13(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 13;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
// Clear any already deleted db entries.
// secure_delete = ON will make sure next deleted entries are overwritten with 0 right away
db.transaction(() => {
db.pragma('secure_delete = ON');
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion14(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 14;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
DROP TABLE IF EXISTS servers;
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS preKeys;
DROP TABLE IF EXISTS contactPreKeys;
DROP TABLE IF EXISTS contactSignedPreKeys;
DROP TABLE IF EXISTS signedPreKeys;
DROP TABLE IF EXISTS senderKeys;
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion15(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 15;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
DROP TABLE pairingAuthorisations;
DROP TRIGGER messages_on_delete;
DROP TRIGGER messages_on_update;
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion16(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 16;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
ALTER TABLE ${MESSAGES_TABLE} ADD COLUMN serverHash TEXT;
ALTER TABLE ${MESSAGES_TABLE} ADD COLUMN isDeleted BOOLEAN;
CREATE INDEX messages_serverHash ON ${MESSAGES_TABLE} (
serverHash
) WHERE serverHash IS NOT NULL;
CREATE INDEX messages_isDeleted ON ${MESSAGES_TABLE} (
isDeleted
) WHERE isDeleted IS NOT NULL;
ALTER TABLE unprocessed ADD serverHash TEXT;
CREATE INDEX messages_messageHash ON unprocessed (
serverHash
) WHERE serverHash IS NOT NULL;
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion17(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 17;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
UPDATE ${CONVERSATIONS_TABLE} SET
json = json_set(json, '$.isApproved', 1)
`);
// remove the moderators field. As it was only used for opengroups a long time ago and whatever is there is probably unused
db.exec(`
UPDATE ${CONVERSATIONS_TABLE} SET
json = json_remove(json, '$.moderators', '$.dataMessage', '$.accessKey', '$.profileSharing', '$.sessionRestoreSeen')
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion18(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 18;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
// Dropping all pre-existing schema relating to message searching.
// Recreating the full text search and related triggers
db.transaction(() => {
dropFtsAndTriggers(db);
rebuildFtsTable(db);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion19(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 19;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
DROP INDEX messages_schemaVersion;
ALTER TABLE ${MESSAGES_TABLE} DROP COLUMN schemaVersion;
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion20(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 20;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
// First we want to drop the column friendRequestStatus if it is there, otherwise the transaction fails
const rows = db.pragma(`table_info(${CONVERSATIONS_TABLE});`);
if (rows.some((m: any) => m.name === 'friendRequestStatus')) {
console.info('found column friendRequestStatus. Dropping it');
db.exec(`ALTER TABLE ${CONVERSATIONS_TABLE} DROP COLUMN friendRequestStatus;`);
}
// looking for all private conversations, with a nickname set
const rowsToUpdate = db
.prepare(
`SELECT * FROM ${CONVERSATIONS_TABLE} WHERE type = 'private' AND (name IS NULL or name = '') AND json_extract(json, '$.nickname') <> '';`
)
.all();
// tslint:disable-next-line: no-void-expression
(rowsToUpdate || []).forEach(r => {
const obj = jsonToObject(r.json);
// obj.profile.displayName is the display as this user set it.
if (obj?.nickname?.length && obj?.profile?.displayName?.length) {
// this one has a nickname set, but name is unset, set it to the displayName in the lokiProfile if it's existing
obj.name = obj.profile.displayName;
sqlNode.saveConversation(obj as ConversationAttributes, db);
}
});
writeSessionSchemaVersion(targetVersion, db);
});
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion21(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 21;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
UPDATE ${CONVERSATIONS_TABLE} SET
json = json_set(json, '$.didApproveMe', 1, '$.isApproved', 1)
WHERE type = 'private';
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion22(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 22;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`DROP INDEX messages_duplicate_check;`);
db.exec(`
ALTER TABLE ${MESSAGES_TABLE} DROP sourceDevice;
`);
db.exec(`
ALTER TABLE unprocessed DROP sourceDevice;
`);
db.exec(`
CREATE INDEX messages_duplicate_check ON ${MESSAGES_TABLE} (
source,
sent_at
);
`);
dropFtsAndTriggers(db);
// we also want to remove the read_by it could have 20 times the same value set in the array
// we do this once, and updated the code to not allow multiple entries in read_by as we do not care about multiple entries
// (read_by is only used in private chats)
db.exec(`
UPDATE ${MESSAGES_TABLE} SET
json = json_remove(json, '$.schemaVersion', '$.recipients', '$.decrypted_at', '$.sourceDevice', '$.read_by')
`);
rebuildFtsTable(db);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion23(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 23;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(
`
ALTER TABLE ${LAST_HASHES_TABLE} RENAME TO ${LAST_HASHES_TABLE}_old;
CREATE TABLE ${LAST_HASHES_TABLE}(
id TEXT,
snode TEXT,
hash TEXT,
expiresAt INTEGER,
namespace INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (id, snode, namespace)
);`
);
db.exec(
`INSERT INTO ${LAST_HASHES_TABLE}(id, snode, hash, expiresAt) SELECT id, snode, hash, expiresAt FROM ${LAST_HASHES_TABLE}_old;`
);
db.exec(`DROP TABLE ${LAST_HASHES_TABLE}_old;`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
// tslint:disable-next-line: max-func-body-length
function updateToSessionSchemaVersion24(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 24;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
// it's unlikely there is still a publicChat v1 convo in the db, but run this in a migration to be 100% sure (previously, run on app start instead)
db.prepare(
`DELETE FROM ${CONVERSATIONS_TABLE} WHERE
type = 'group' AND
id LIKE 'publicChat:1@%';`
).run();
db.exec(`
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN zombies TEXT DEFAULT "[]";
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN left INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN expireTimer INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN mentionedUs INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN unreadCount INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN lastMessageStatus TEXT;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN lastMessage TEXT;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN lastJoinedTimestamp INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN groupAdmins TEXT DEFAULT "[]";
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN isKickedFromGroup INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN subscriberCount INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN is_medium_group INTEGER;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN avatarPointer TEXT; -- this is the url of the avatar for that conversation
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN avatarHash TEXT; -- only used for opengroup avatar.
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN nickname TEXT;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN profileKey TEXT;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN triggerNotificationsFor TEXT DEFAULT "all";
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN isTrustedForAttachmentDownload INTEGER DEFAULT "FALSE";
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN isPinned INTEGER DEFAULT "FALSE";
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN isApproved INTEGER DEFAULT "FALSE";
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN didApproveMe INTEGER DEFAULT "FALSE";
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN avatarInProfile TEXT;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN avatarPathInAvatar TEXT; -- this is very temporary, removed right below
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN displayNameInProfile TEXT;
UPDATE ${CONVERSATIONS_TABLE} SET
zombies = json_extract(json, '$.zombies'),
members = json_extract(json, '$.members'),
left = json_extract(json, '$.left'),
expireTimer = json_extract(json, '$.expireTimer'),
mentionedUs = json_extract(json, '$.mentionedUs'),
unreadCount = json_extract(json, '$.unreadCount'),
lastMessageStatus = json_extract(json, '$.lastMessageStatus'),
lastMessage = json_extract(json, '$.lastMessage'),
lastJoinedTimestamp = json_extract(json, '$.lastJoinedTimestamp'),
groupAdmins = json_extract(json, '$.groupAdmins'),
isKickedFromGroup = json_extract(json, '$.isKickedFromGroup'),
subscriberCount = json_extract(json, '$.subscriberCount'),
is_medium_group = json_extract(json, '$.is_medium_group'),
avatarPointer = json_extract(json, '$.avatarPointer'),
avatarHash = json_extract(json, '$.avatarHash'),
nickname = json_extract(json, '$.nickname'),
profileKey = json_extract(json, '$.profileKey'),
triggerNotificationsFor = json_extract(json, '$.triggerNotificationsFor'),
isTrustedForAttachmentDownload = json_extract(json, '$.isTrustedForAttachmentDownload'),
isPinned = json_extract(json, '$.isPinned'),
isApproved = json_extract(json, '$.isApproved'),
didApproveMe = json_extract(json, '$.didApproveMe'),
avatarInProfile = json_extract(json, '$.profile.avatar'),-- profile.avatar is no longer used. We rely on avatarInProfile only (for private chats and opengroups )
avatarPathInAvatar = json_extract(json, '$.avatar.path'),-- this is very temporary
displayNameInProfile = json_extract(json, '$.profile.displayName');
UPDATE ${CONVERSATIONS_TABLE} SET json = json_remove(json,
'$.zombies',
'$.members',
'$.left',
'$.expireTimer',
'$.mentionedUs',
'$.unreadCount',
'$.lastMessageStatus',
'$.lastJoinedTimestamp',
'$.lastMessage',
'$.groupAdmins',
'$.isKickedFromGroup',
'$.subscriberCount',
'$.is_medium_group',
'$.avatarPointer',
'$.avatarHash',
'$.nickname',
'$.profileKey',
'$.triggerNotificationsFor',
'$.isTrustedForAttachmentDownload',
'$.isPinned',
'$.isApproved',
'$.type',
'$.version',
'$.isMe',
'$.didApproveMe',
'$.active_at',
'$.id',
'$.moderators',
'$.sessionRestoreSeen',
'$.profileName',
'$.timestamp',
'$.profile',
'$.name',
'$.profileAvatar',
'$.avatarPath
');
ALTER TABLE ${CONVERSATIONS_TABLE} DROP COLUMN json;
UPDATE ${CONVERSATIONS_TABLE} SET displayNameInProfile = name WHERE
type = 'group' AND
id NOT LIKE 'publicChat:%';
ALTER TABLE ${CONVERSATIONS_TABLE} DROP COLUMN profileName;
ALTER TABLE ${CONVERSATIONS_TABLE} DROP COLUMN name;
-- we want to rely on avatarInProfile only, but it can be set either in avatarInProfile or in avatarPathInAvatar.
-- make sure to override avatarInProfile with the value from avatarPathInAvatar if avatarInProfile is unset
UPDATE ${CONVERSATIONS_TABLE} SET avatarInProfile = avatarPathInAvatar WHERE avatarInProfile IS NULL;
ALTER TABLE ${CONVERSATIONS_TABLE} DROP COLUMN avatarPathInAvatar;
CREATE INDEX conversation_nickname ON ${CONVERSATIONS_TABLE} (
nickname
);
CREATE INDEX conversation_displayNameInProfile ON ${CONVERSATIONS_TABLE} (
displayNameInProfile
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion25(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 25;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
// mark all conversation as read/write/upload capability to be true on migration.
// the next batch poll will update them if needed
db.exec(`
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN readCapability INTEGER DEFAULT 1;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN writeCapability INTEGER DEFAULT 1;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN uploadCapability INTEGER DEFAULT 1;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN conversationIdOrigin TEXT;
ALTER TABLE ${CONVERSATIONS_TABLE} DROP COLUMN avatarHash;
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN avatarImageId INTEGER;
CREATE INDEX messages_convo_serverID ON ${MESSAGES_TABLE} (
serverId,
conversationId
);
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function updateToSessionSchemaVersion26(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 26;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
db.transaction(() => {
db.exec(`
ALTER TABLE ${CONVERSATIONS_TABLE} ADD COLUMN groupModerators TEXT DEFAULT "[]"; -- those are for sogs only (for closed groups we only need the groupAdmins)
`);
writeSessionSchemaVersion(targetVersion, db);
})();
console.log(`updateToSessionSchemaVersion${targetVersion}: success!`);
}
function getRoomIdFromConversationAttributes(attributes?: ConversationAttributes | null) {
if (!attributes) {
return null;
}
const indexSemiColon = attributes.id.indexOf(':');
const indexAt = attributes.id.indexOf('@');
if (indexSemiColon < 0 || indexAt < 0 || indexSemiColon >= indexAt) {
return null;
}
const roomId = attributes.id.substring(indexSemiColon, indexAt);
if (roomId.length <= 0) {
return null;
}
return roomId;
}
function updateToSessionSchemaVersion27(currentVersion: number, db: BetterSqlite3.Database) {
const targetVersion = 27;
if (currentVersion >= targetVersion) {
return;
}
console.log(`updateToSessionSchemaVersion${targetVersion}: starting...`);
const domainNameToUse = 'open.getsession.org';
const urlToUse = `https://${domainNameToUse}`;
const ipToRemove = '116.203.70.33';
// defining this function here as this is very specific to this migration and used in a few places
function getNewConvoId(oldConvoId?: string) {
if (!oldConvoId) {
return null;
}
return (
oldConvoId
?.replace(`https://${ipToRemove}`, urlToUse)
// tslint:disable-next-line: no-http-string
?.replace(`http://${ipToRemove}`, urlToUse)
?.replace(ipToRemove, urlToUse)
);
}
getNewConvoId('test');
// tslint:disable-next-line: max-func-body-length
db.transaction(() => {
// First we want to drop the column friendRequestStatus if it is there, otherwise the transaction fails
const rows = db.pragma(`table_info(${CONVERSATIONS_TABLE});`);
if (rows.some((m: any) => m.name === 'friendRequestStatus')) {
console.info('found column friendRequestStatus. Dropping it');
db.exec(`ALTER TABLE ${CONVERSATIONS_TABLE} DROP COLUMN friendRequestStatus;`);
}
// We want to replace all the occurrences of the sogs server ip url (116.203.70.33 || http://116.203.70.33 || https://116.203.70.33) by its hostname: https://open.getsession.org
// This includes change the conversationTable, the openGroupRooms tables and every single message associated with them.
// Because the conversationId is used to link messages to conversation includes the ip/url in it...
/**
* First, remove duplicates for the v2 opengroup table, and replace the one without duplicates with their dns name syntax
*/
// rooms to rename are: crypto, lokinet, oxen, session, session-updates
const allSessionV2RoomsIp = sqlNode
.getAllV2OpenGroupRooms(db)
.filter(m => m.serverUrl.includes(ipToRemove));
const allSessionV2RoomsDns = sqlNode
.getAllV2OpenGroupRooms(db)
.filter(m => m.serverUrl.includes(domainNameToUse));
const duplicatesRoomsIpAndDns = allSessionV2RoomsIp.filter(ip =>
allSessionV2RoomsDns.some(dns => dns.roomId === ip.roomId)
);
const withIpButNotDuplicateRoom = allSessionV2RoomsIp.filter(ip => {
return !duplicatesRoomsIpAndDns.some(dns => dns.roomId === ip.roomId);
});
console.info(
'allSessionV2RoomsIp',
allSessionV2RoomsIp.map(m => pick(m, ['serverUrl', 'roomId']))
);
console.info(
'allSessionV2RoomsDns',
allSessionV2RoomsDns.map(m => pick(m, ['serverUrl', 'roomId']))
);
console.info(
'duplicatesRoomsIpAndDns',
duplicatesRoomsIpAndDns.map(m => pick(m, ['serverUrl', 'roomId']))
);
console.info(
'withIpButNotDuplicateRoom',
withIpButNotDuplicateRoom.map(m => pick(m, ['serverUrl', 'roomId']))
);
console.info(
'========> before room update:',
sqlNode
.getAllV2OpenGroupRooms(db)
.filter(m => m.serverUrl.includes(domainNameToUse) || m.serverUrl.includes(ipToRemove))
.map(m => pick(m, ['conversationId', 'serverUrl', 'roomId']))
);
// for those with duplicates, delete the one with the IP as we want to rely on the one with the DNS only now
// remove the ip ones completely which are duplicated.
// Note: this also removes the ones not duplicated, but we are recreating them just below with `saveV2OpenGroupRoom`
db.exec(`DELETE FROM ${OPEN_GROUP_ROOMS_V2_TABLE} WHERE serverUrl LIKE '%${ipToRemove}%';`);
// for those without duplicates, override the value with the Domain Name
withIpButNotDuplicateRoom.forEach(r => {
const newConvoId = getNewConvoId(r.conversationId);
if (!newConvoId) {
return;
}
console.info(
`withIpButNotDuplicateRoom: renaming room old:${r.conversationId} with saveV2OpenGroupRoom() new- conversationId:${newConvoId}: serverUrl:${urlToUse}`
);
sqlNode.saveV2OpenGroupRoom(
{
...r,
serverUrl: urlToUse,
conversationId: newConvoId,