-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb_migrations.go
More file actions
2778 lines (2339 loc) · 86.4 KB
/
db_migrations.go
File metadata and controls
2778 lines (2339 loc) · 86.4 KB
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
package server
import (
"context"
"fmt"
"github.com/urnetwork/glog"
)
/*
Use manual editing to fix or backport changes.
To list all tables in Postgres:
```
SELECT * FROM pg_catalog.pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
```
*/
var DbMigrationVerbose = false
type SqlMigration struct {
sql string
}
func newSqlMigration(sql string) *SqlMigration {
return &SqlMigration{
sql: sql,
}
}
// important these migration functions must be idempotent
type CodeMigration struct {
callback func(context.Context)
}
func newCodeMigration(callback func(context.Context)) *CodeMigration {
return &CodeMigration{
callback: callback,
}
}
func DbVersion(ctx context.Context) int {
MaintenanceTx(ctx, func(tx PgTx) {
RaisePgResult(tx.Exec(
ctx,
`
CREATE TABLE IF NOT EXISTS migration_audit (
migration_time timestamp NOT NULL DEFAULT now(),
start_version_number int NOT NULL,
end_version_number int NULL,
status varchar(32) NOT NULL
)
`,
))
})
var endVersionNumber int
MaintenanceDb(ctx, func(conn PgConn) {
result, err := conn.Query(
ctx,
`
SELECT COALESCE(MAX(end_version_number), 0) AS max_end_version_number
FROM migration_audit
WHERE status = 'success'
`,
)
WithPgResult(result, err, func() {
if result.Next() {
Raise(result.Scan(&endVersionNumber))
}
})
})
return endVersionNumber
}
func ApplyDbMigrations(ctx context.Context) {
for i := DbVersion(ctx); i < len(migrations); i += 1 {
MaintenanceTx(ctx, func(tx PgTx) {
RaisePgResult(tx.Exec(
ctx,
`INSERT INTO migration_audit (start_version_number, status) VALUES ($1, 'start')`,
i,
))
})
switch v := migrations[i].(type) {
case *SqlMigration:
if DbMigrationVerbose {
glog.Infof("[migrate][%d/%d]sql = %s\n", i+1, len(migrations), v.sql)
}
MaintenanceTx(ctx, func(tx PgTx) {
defer func() {
if err := recover(); err != nil {
// print the sql for debugging
glog.Infof("[migrate][%d/%d]err = %s; sql = %s\n", i+1, len(migrations), err, v.sql)
panic(err)
}
}()
RaisePgResult(tx.Exec(ctx, v.sql))
})
case *CodeMigration:
if DbMigrationVerbose {
glog.Infof("[migrate][%d/%d]code = %v\n", i+1, len(migrations), v.callback)
}
v.callback(ctx)
default:
panic(fmt.Errorf("Unknown migration type %T", v))
}
MaintenanceTx(ctx, func(tx PgTx) {
RaisePgResult(tx.Exec(
ctx,
`INSERT INTO migration_audit (start_version_number, end_version_number, status) VALUES ($1, $2, 'success')`,
i,
i+1,
))
})
}
}
// style: use varchar not ENUM
// style: in queries with two+ tables, use fully qualified column names
// FIXME perf: CREATE INDEX should always use CONCURRENTLY - we need to use raw connections for the sql, and the db to mark raw connections as not read only
var migrations = []any{
// newSqlMigration(`CREATE TYPE audit_provider_event_type AS ENUM (
// 'provider_offline',
// 'provider_online_superspeed',
// 'provider_online_not_superspeed'
// )`),
// newSqlMigration(`CREATE TYPE audit_event_type AS varchar(64)`),
// newSqlMigration(`CREATE TYPE audit_event_details_type AS TEXT`),
newSqlMigration(`
CREATE TABLE audit_provider_event (
event_id uuid NOT NULL,
event_time timestamp NOT NULL DEFAULT now(),
network_id uuid NOT NULL,
device_id uuid NOT NULL,
event_type varchar(64) NOT NULL,
event_details text NULL,
country_name varchar(128) NOT NULL,
region_name varchar(128) NOT NULL,
city_name varchar(128) NOT NULL,
PRIMARY KEY (event_id)
)
`),
newSqlMigration(`
CREATE INDEX audit_provider_event_stats_device_id ON audit_provider_event (event_time, device_id, event_id)
`),
// newSqlMigration(`CREATE TYPE audit_extender_event_type AS ENUM (
// 'extender_offline',
// 'extender_online_superspeed',
// 'extender_online_not_superspeed'
// )`),
newSqlMigration(`
CREATE TABLE audit_extender_event (
event_id uuid NOT NULL,
event_time timestamp NOT NULL DEFAULT now(),
network_id uuid NOT NULL,
extender_id uuid NOT NULL,
event_type varchar(64) NOT NULL,
event_details text NULL,
PRIMARY KEY (event_id)
)
`),
newSqlMigration(`
CREATE INDEX audit_extender_event_stats_extender_id ON audit_extender_event (event_time, extender_id, event_id)
`),
// newSqlMigration(`CREATE TYPE audit_network_event_type AS ENUM (
// 'network_created',
// 'network_deleted'
// )`),
newSqlMigration(`
CREATE TABLE audit_network_event (
event_id uuid NOT NULL,
event_time timestamp NOT NULL DEFAULT now(),
network_id uuid NOT NULL,
event_type varchar(64) NOT NULL,
event_details text NULL,
PRIMARY KEY (event_id)
)
`),
newSqlMigration(`
CREATE INDEX audit_network_event_stats_network_id ON audit_network_event (event_time, network_id, event_id)
`),
// newSqlMigration(`CREATE TYPE audit_device_event_type AS ENUM (
// 'device_added',
// 'device_removed'
// )`),
newSqlMigration(`
CREATE TABLE audit_device_event (
event_id uuid NOT NULL,
event_time timestamp NOT NULL DEFAULT now(),
network_id uuid NOT NULL,
device_id uuid NOT NULL,
event_type varchar(64) NOT NULL,
event_details text NULL,
PRIMARY KEY (event_time, device_id, event_id),
UNIQUE (event_id)
)
`),
newSqlMigration(`
CREATE INDEX audit_device_event_stats_device_id ON audit_device_event (event_time, device_id, event_id)
`),
// newSqlMigration(`CREATE TYPE audit_contract_event_type AS ENUM (
// 'contract_closed_success'
// )`),
// RENAMED `transfer_bytes` to `transfer_byte_count`
newSqlMigration(`
CREATE TABLE audit_contract_event (
event_id uuid NOT NULL,
event_time timestamp NOT NULL DEFAULT now(),
contract_id uuid NOT NULL,
client_network_id uuid NOT NULL,
client_device_id uuid NOT NULL,
provider_network_id uuid NOT NULL,
provider_device_id uuid NOT NULL,
extender_network_id uuid NULL,
extender_id uuid NULL,
event_type varchar(64) NOT NULL,
event_details text NULL,
transfer_bytes bigint NOT NULL DEFAULT 0,
transfer_packets bigint NOT NULL DEFAULT 0,
PRIMARY KEY (event_id)
)
`),
newSqlMigration(`
CREATE INDEX audit_contract_event_stats ON audit_contract_event (event_time, transfer_bytes, transfer_packets)
`),
newSqlMigration(`
CREATE INDEX audit_contract_event_stats_extender_id
ON audit_contract_event (event_time, extender_id, transfer_bytes, transfer_packets)
`),
newSqlMigration(`
CREATE TABLE network (
network_id uuid NOT NULL,
network_name varchar(256) NOT NULL,
admin_user_id uuid NOT NULL,
PRIMARY KEY (network_id),
UNIQUE (network_name)
)
`),
newSqlMigration(`
CREATE INDEX network_admin_user_id ON network (admin_user_id, network_id)
`),
// DROPPED
newSqlMigration(`
CREATE TYPE auth_type AS ENUM (
'password',
'apple',
'google'
)
`),
// ALTER auth_type changed to varchar(32)
// RENAME validate to verified
// password_hash: 32-byte argon2 hash digest
// password_salt: 32-byte random
// FIXME add network_id
newSqlMigration(`
CREATE TABLE network_user (
user_id uuid NOT NULL,
user_name varchar(128) NOT NULL,
auth_type auth_type NOT NULL,
user_auth varchar(256) NULL,
password_hash bytea NULL,
password_salt bytea NULL,
auth_jwt text NULL,
validated bool NOT NULL DEFAULT false,
PRIMARY KEY (user_id),
UNIQUE (user_auth)
)
`),
// the index of user_auth is covered by the unique index
// MIGRATED client_ipv4/client_ip moved to client_address_hash/client_port
// an attempt any of:
// - network create
// - login
// - password login
// - reset
// - validation
// client_ipv4: 4 bytes stored in dot notation
newSqlMigration(`
CREATE TABLE user_auth_attempt (
user_auth_attempt_id uuid NOT NULL,
user_auth varchar(256) NULL,
attempt_time timestamp NOT NULL DEFAULT now(),
client_ipv4 varchar(16) NOT NULL,
success bool NOT NULL,
PRIMARY KEY (user_auth_attempt_id)
)
`),
newSqlMigration(`
CREATE INDEX user_auth_attempt_client_ipv4 ON user_auth_attempt (client_ipv4, attempt_time)
`),
newSqlMigration(`
CREATE INDEX user_auth_attempt_user_auth ON user_auth_attempt (user_auth, attempt_time)
`),
// reset_code: 64-byte hex
// reset codes must be globally unique because of the way they are used
newSqlMigration(`
CREATE TABLE user_auth_reset (
user_auth_reset_id uuid NOT NULL,
user_id uuid NOT NULL,
reset_time timestamp NOT NULL DEFAULT now(),
reset_code varchar(256) NOT NULL,
used bool NOT NULL DEFAULT false,
PRIMARY KEY (user_auth_reset_id),
UNIQUE (reset_code)
)
`),
newSqlMigration(`
CREATE INDEX user_auth_reset_user_id ON user_auth_reset (user_id, reset_code)
`),
// DROPPED and recreated as `user_auth_verify` (see below)
newSqlMigration(`
CREATE TABLE user_auth_validate (
user_auth_validate_id uuid NOT NULL,
user_id uuid NOT NULL,
validate_time timestamp NOT NULL DEFAULT now(),
validate_code varchar(16) NOT NULL,
used bool NOT NULL DEFAULT false,
PRIMARY KEY (user_auth_validate_id),
UNIQUE (user_id, validate_code)
)
`),
newSqlMigration(`
CREATE TABLE account_feedback (
feedback_id uuid NOT NULL,
network_id uuid NOT NULL,
user_id uuid NOT NULL,
feedback_time timestamp NOT NULL DEFAULT now(),
uses_personal bool NOT NULL DEFAULT false,
uses_business bool NOT NULL DEFAULT false,
needs_private bool NOT NULL DEFAULT false,
needs_safe bool NOT NULL DEFAULT false,
needs_global bool NOT NULL DEFAULT false,
needs_collaborate bool NOT NULL DEFAULT false,
needs_app_control bool NOT NULL DEFAULT false,
needs_block_data_brokers bool NOT NULL DEFAULT false,
needs_block_ads bool NOT NULL DEFAULT false,
needs_focus bool NOT NULL DEFAULT false,
needs_connect_servers bool NOT NULL DEFAULT false,
needs_run_servers bool NOT NULL DEFAULT false,
needs_prevent_cyber bool NOT NULL DEFAULT false,
needs_audit bool NOT NULL DEFAULT false,
needs_zero_trust bool NOT NULL DEFAULT false,
needs_visualize bool NOT NULL DEFAULT false,
needs_other text NULL,
PRIMARY KEY (feedback_id)
)
`),
newSqlMigration(`
CREATE TABLE account_preferences (
network_id uuid NOT NULL,
product_updates bool NOT NULL DEFAULT false,
PRIMARY KEY (network_id)
)
`),
// DROPPED and re-created
newSqlMigration(`
CREATE TABLE search_value (
realm varchar(16) NOT NULL,
value_id uuid NOT NULL,
value varchar(1024) NOT NULL,
alias int NOT NULL DEFAULT 0,
PRIMARY KEY(value_id, alias)
)
`),
// DROPPED
newSqlMigration(`
CREATE INDEX search_value_realm_value ON search_value (realm, value, value_id, alias)
`),
// DROPPED and re-created
newSqlMigration(`
CREATE TABLE search_projection (
realm varchar(16) NOT NULL,
dim smallint NOT NULL,
elen smallint NOT NULL,
dord smallint NOT NULL,
dlen smallint NOT NULL,
vlen smallint NOT NULL,
value_id uuid NOT NULL,
alias int NOT NULL DEFAULT 0,
PRIMARY KEY (realm, dim, elen, dord, dlen, vlen, value_id, alias)
)
`),
// DROPPED
newSqlMigration(`
CREATE INDEX search_projection_value_id ON search_projection (value_id, alias)
`),
newSqlMigration(`
ALTER TABLE network_user RENAME COLUMN validated TO verified
`),
newSqlMigration(`
DROP TABLE user_auth_validate
`),
// verify_code: 4-byte hex
newSqlMigration(`
CREATE TABLE user_auth_verify (
user_auth_verify_id uuid NOT NULL,
user_id uuid NOT NULL,
verify_time timestamp NOT NULL DEFAULT now(),
verify_code varchar(16) NOT NULL,
used bool NOT NULL DEFAULT false,
PRIMARY KEY (user_auth_verify_id),
UNIQUE (user_id, verify_code)
)
`),
newSqlMigration(`
ALTER TABLE audit_contract_event
RENAME transfer_bytes TO transfer_byte_count
`),
// ADDED device_id
// REMOVED device_spec
// RESIZED device_spec to varchar(256)
newSqlMigration(`
CREATE TABLE network_client (
client_id uuid NOT NULL,
network_id uuid NOT NULL,
active bool NOT NULL DEFAULT true,
description varchar(256) NULL,
device_spec varchar(64) NULL,
create_time timestamp NOT NULL DEFAULT now(),
auth_time timestamp NOT NULL DEFAULT now(),
PRIMARY KEY (client_id)
)
`),
newSqlMigration(`
CREATE INDEX network_client_network_id_active ON network_client (network_id, active)
`),
newSqlMigration(`
CREATE INDEX network_client_network_id_create_time ON network_client (network_id, create_time)
`),
// newSqlMigration(`
// CREATE TYPE provide_mode AS ENUM (
// 'network',
// 'ff',
// 'public',
// 'stream'
// )
// `),
// DROPPED use `provide_key`
newSqlMigration(`
CREATE TABLE client_provide (
client_id uuid NOT NULL,
provide_mode int NOT NULL DEFAULT 0,
PRIMARY KEY (client_id)
)
`),
newSqlMigration(`
CREATE TABLE provide_key (
client_id uuid NOT NULL,
provide_mode int NOT NULL,
secret_key bytea NOT NULL,
PRIMARY KEY (client_id, provide_mode)
)
`),
// `client_address` is ip:port
// ipv6 is at most 45 chars, plus 7 for the :port
// see https://stackoverflow.com/questions/166132/maximum-length-of-the-textual-representation-of-an-ipv6-address
newSqlMigration(`
CREATE TABLE network_client_connection (
client_id uuid NOT NULL,
connection_id uuid NOT NULL,
connected bool NOT NULL DEFAULT true,
connect_time timestamp NOT NULL,
disconnect_time timestamp NULL,
connection_host varchar(128) NOT NULL,
connection_service varchar(128) NOT NULL,
connection_block varchar(128) NOT NULL,
client_address varchar(52) NOT NULL,
PRIMARY KEY (connection_id)
)
`),
newSqlMigration(`
CREATE INDEX network_client_connection_client_id_connected ON network_client_connection (client_id, connected)
`),
// DROPPED
newSqlMigration(`
CREATE TABLE ip_location_lookup (
ip_address varchar(64) NOT NULL,
lookup_time timestamp NOT NULL DEFAULT now(),
result_json text NOT NULL,
PRIMARY KEY (ip_address, lookup_time)
)
`),
// newSqlMigration(`
// CREATE TYPE location_type AS ENUM (
// 'city',
// 'region',
// 'country'
// )
// `),
// DROPPED and recreated
newSqlMigration(`
CREATE TABLE location (
location_id uuid NOT NULL,
location_type varchar(16) NOT NULL,
location_name varchar(128) NOT NULL,
city_location_id uuid NULL,
region_location_id uuid NULL,
country_location_id uuid NULL,
country_code char(2) NOT NULL,
PRIMARY KEY (location_id)
)
`),
// DROPPED and recreated
newSqlMigration(`
CREATE INDEX location_type_country_code_name ON location (location_type, country_code, location_name, country_location_id, region_location_id, location_id)
`),
// DROPPED and recreated
newSqlMigration(`
CREATE TABLE location_group (
location_group_id uuid NOT NULL,
location_group_name varchar(128) NOT NULL,
promoted bool NOT NULL DEFAULT false,
PRIMARY KEY (location_group_id)
)
`),
newSqlMigration(`
CREATE INDEX location_group_name ON location_group (location_group_name, location_group_id)
`),
newSqlMigration(`
CREATE TABLE location_group_member (
location_group_id uuid NOT NULL,
location_id uuid NOT NULL,
PRIMARY KEY (location_group_id, location_id)
)
`),
newSqlMigration(`
CREATE INDEX location_group_member_location_id ON location_group_member (location_id, location_group_id)
`),
// this denormalizes to speed up finding client_ids by location
// (connection_id, client_id)
// (city_location_id, region_location_id, country_location_id)
newSqlMigration(`
CREATE TABLE network_client_location (
connection_id uuid NOT NULL,
client_id uuid NOT NULL,
city_location_id uuid NOT NULL,
region_location_id uuid NOT NULL,
country_location_id uuid NOT NULL,
PRIMARY KEY (connection_id)
)
`),
// 3 separate indexes: city location id, region, country
newSqlMigration(`
CREATE INDEX network_client_location_city_client_id ON network_client_location (city_location_id, client_id)
`),
newSqlMigration(`
CREATE INDEX network_client_location_region_client_id ON network_client_location (region_location_id, client_id)
`),
newSqlMigration(`
CREATE INDEX network_client_location_country_client_id ON network_client_location (country_location_id, client_id)
`),
// ALTERED No columns are allowed null
newSqlMigration(`
CREATE TABLE network_client_resident (
client_id uuid NOT NULL,
instance_id uuid NOT NULL,
resident_id uuid NULL,
resident_host varchar(128) NULL,
resident_service varchar(128) NULL,
resident_block varchar(128) NULL,
PRIMARY KEY (client_id),
UNIQUE (resident_id)
)
`),
newSqlMigration(`
CREATE INDEX network_client_resident_host_port ON network_client_resident (resident_host, resident_id)
`),
newSqlMigration(`
CREATE TABLE network_client_resident_port (
client_id uuid NOT NULL,
resident_id uuid NOT NULL,
resident_internal_port int NOT NULL,
PRIMARY KEY (client_id, resident_id, resident_internal_port)
)
`),
// ADDED subsidy_net_revenue_nano_cents
// ADDED paid
// net_revenue_nano_cents is how much was received for this balance (revenue - intermediary fees)
newSqlMigration(`
CREATE TABLE transfer_balance (
balance_id uuid NOT NULL,
network_id uuid NOT NULL,
start_time timestamp NOT NULL DEFAULT now(),
end_time timestamp NOT NULL,
start_balance_byte_count bigint NOT NULL,
net_revenue_nano_cents bigint NOT NULL,
balance_byte_count bigint NOT NULL,
active bool GENERATED ALWAYS AS (0 < balance_byte_count) STORED,
PRIMARY KEY (balance_id)
)
`),
newSqlMigration(`
CREATE INDEX transfer_balance_network_id_active_end_time ON transfer_balance (network_id, active, end_time)
`),
// newSqlMigration(`
// CREATE TYPE contract_outcome AS ENUM (
// 'settled',
// 'dispute_resolved_to_source',
// 'dispute_resolved_to_destination'
// )
// `),
// ADDED companion_contract_id
// ADDED priority
// ADDED payer_network_id
// ADDED payee_network_id
newSqlMigration(`
CREATE TABLE transfer_contract (
contract_id uuid NOT NULL,
source_network_id uuid NOT NULL,
source_id uuid NOT NULL,
destination_network_id uuid NOT NULL,
destination_id uuid NOT NULL,
open bool GENERATED ALWAYS AS (dispute = false AND outcome IS NULL) STORED,
transfer_byte_count bigint NOT NULL,
create_time timestamp NOT NULL DEFAULT now(),
close_time timestamp NULL,
dispute bool NOT NULL DEFAULT false,
outcome varchar(32) NULL,
PRIMARY KEY (contract_id)
)
`),
newSqlMigration(`
CREATE INDEX transfer_contract_open_source_id ON transfer_contract (open, source_id, destination_id, contract_id)
`),
newSqlMigration(`
CREATE INDEX transfer_contract_open_destination_id ON transfer_contract (open, destination_id, source_id, contract_id)
`),
// newSqlMigration(`
// CREATE TYPE contract_party AS ENUM (
// 'source',
// 'destination'
// )
// `),
newSqlMigration(`
CREATE TABLE contract_close (
contract_id uuid NOT NULL,
close_time timestamp NOT NULL DEFAULT now(),
party varchar(16) NOT NULL,
used_transfer_byte_count bigint NOT NULL,
PRIMARY KEY (contract_id, party)
)
`),
// creating an escrow must deduct the balances in the same transaction
// settling an escrow must put `payout_byte_count` into the target account_balances, and `balance_byte_count - payout_byte_count` back into the origin balance
// primary key is (contract_id, balance_id) because there can be multiple balances used per contract_id
newSqlMigration(`
CREATE TABLE transfer_escrow (
contract_id uuid NOT NULL,
balance_id uuid NOT NULL,
balance_byte_count bigint NOT NULL,
escrow_date timestamp NOT NULL DEFAULT now(),
settled bool NOT NULL DEFAULT false,
settle_time timestamp NULL,
payout_byte_count bigint NULL,
PRIMARY KEY (contract_id, balance_id)
)
`),
// note the unpaid balance is `provided_balance_byte_count - paid_balance_byte_count`
// this equals SUM(transfer_escrow_sweep.payout_byte_count) where payment_id IS NULL
// the cents to pay out is SUM(transfer_escrow_sweep.net_revenue_cents)
newSqlMigration(`
CREATE TABLE account_balance (
network_id uuid NOT NULL,
provided_byte_count bigint NOT NULL DEFAULT 0,
provided_net_revenue_nano_cents bigint NOT NULL DEFAULT 0,
paid_byte_count bigint NOT NULL DEFAULT 0,
paid_net_revenue_nano_cents bigint NOT NULL DEFAULT 0,
PRIMARY KEY (network_id)
)
`),
// sweeps escrow value into an account
// the payment_id is the payment the swept value was paid out using
// the value of the unpaid balance needs to go back to the escrows to where the balance came from
// the cents contribution of each escrow is `(escrow.payout_byte_count / balance.balance_byte_count) * balance.net_revenue_cents`
//
// payment flow:
// 1. create an `account_payment` entry which is `complete=false`
// 2. associate unpaid `transfer_escrow_sweep` to that payment_id
// 3. complete the payment with the final amount, and update the `payment_record` for the pending payment
// 4. when the payment is complete, mark the payment `complete=true`
newSqlMigration(`
CREATE TABLE transfer_escrow_sweep (
contract_id uuid NOT NULL,
balance_id uuid NOT NULL,
network_id uuid NOT NULL,
payout_byte_count bigint NOT NULL,
payout_net_revenue_nano_cents bigint NOT NULL,
sweep_time timestamp NOT NULL DEFAULT now(),
payment_id uuid NULL,
PRIMARY KEY (contract_id, balance_id, network_id)
)
`),
newSqlMigration(`
CREATE INDEX transfer_escrow_sweep_payment_id ON transfer_escrow_sweep (payment_id)
`),
// circle uses the circle sdk for usdc
// newSqlMigration(`
// CREATE TYPE wallet_type AS ENUM (
// 'circle_uc',
// 'xch',
// 'sol'
// )
// `),
newSqlMigration(`
CREATE TABLE account_wallet (
wallet_id uuid NOT NULL,
network_id uuid NOT NULL,
wallet_type varchar(32) NOT NULL,
blockchain varchar(32) NOT NULL,
wallet_address text NOT NULL,
active bool NOT NULL DEFAULT true,
default_token_type varchar(16) NOT NULL,
create_time timestamp NOT NULL DEFAULT now(),
PRIMARY KEY (wallet_id)
)
`),
newSqlMigration(`
CREATE INDEX account_wallet_active_network_id ON account_wallet (active, network_id, wallet_id)
`),
// `circle_uc_user_id` is the user_id for the circle user-controlled platform
// DROPPED and recreated
newSqlMigration(`
CREATE TABLE circle_uc (
network_id uuid NOT NULL,
circle_uc_user_id uuid NOT NULL,
PRIMARY KEY (network_id)
)
`),
// this is the preferred wallet for payout
newSqlMigration(`
CREATE TABLE payout_wallet (
network_id uuid NOT NULL,
wallet_id uuid NOT NULL,
PRIMARY KEY (network_id)
)
`),
// ADDED subsidy_payout_nano_cents
// payment record is what was submitted to the payment processor
// payment receipt is the receipt from the payment processor
newSqlMigration(`
CREATE TABLE account_payment (
payment_id uuid NOT NULL,
payment_plan_id uuid NOT NULL,
wallet_id uuid NOT NULL,
payout_byte_count bigint NOT NULL,
payout_nano_cents bigint NOT NULL,
min_sweep_time timestamp NOT NULL,
create_time timestamp NOT NULL DEFAULT now(),
payment_record text NULL,
token_type varchar(16) NULL,
token_amount double precision NULL,
payment_time timestamp NULL,
payment_receipt text NULL,
completed bool NOT NULL DEFAULT false,
complete_time timestamp NULL,
canceled bool NOT NULL DEFAULT false,
cancel_time timestamp NULL,
PRIMARY KEY (payment_id)
)
`),
newSqlMigration(`
CREATE INDEX account_payment_payment_plan ON account_payment (payment_plan_id, payment_id)
`),
newSqlMigration(`
ALTER TABLE user_auth_attempt ALTER client_ipv4 DROP NOT NULL
`),
newSqlMigration(`
ALTER TABLE user_auth_attempt ADD COLUMN client_ip varchar(64) NULL
`),
newSqlMigration(`
ALTER TABLE user_auth_attempt ADD COLUMN client_port int NULL
`),
newSqlMigration(`
CREATE INDEX user_auth_attempt_client_address ON user_auth_attempt (client_ip, client_port, attempt_time)
`),
newSqlMigration(`
CREATE TABLE transfer_balance_code (
balance_code_id uuid NOT NULL,
create_time timestamp NOT NULL,
start_time timestamp NOT NULL,
end_time timestamp NOT NULL,
balance_byte_count bigint NOT NULL,
net_revenue_nano_cents bigint NOT NULL,
balance_code_secret varchar(128) NOT NULL,
purchase_event_id varchar(128) NOT NULL,
purchase_record text NOT NULL,
purchase_email varchar(256) NOT NULL,
redeem_time timestamp NULL,
redeem_balance_id uuid NULL,
notify_count int NOT NULL DEFAULT 0,
next_notify_time timestamp NULL,
redeemed bool GENERATED ALWAYS AS (redeem_balance_id IS NOT NULL) STORED,
PRIMARY KEY (balance_code_id),
UNIQUE (balance_code_secret),
UNIQUE (purchase_event_id)
)
`),
newSqlMigration(`
CREATE INDEX transfer_balance_code_redeemed_time ON transfer_balance_code (redeemed, end_time, next_notify_time)
`),
newSqlMigration(`
DROP INDEX search_value_realm_value
`),
newSqlMigration(`
DROP TABLE search_value
`),
newSqlMigration(`
CREATE TABLE search_value (
realm varchar(32) NOT NULL,
value_id uuid NOT NULL,
value_variant int NOT NULL,
value varchar(1024) NOT NULL,
alias int NOT NULL DEFAULT 0,
vlen smallint GENERATED ALWAYS AS (LENGTH(value)) STORED,
PRIMARY KEY(realm, value_id, value_variant, alias)
)
`),
newSqlMigration(`
CREATE INDEX search_value_realm_vlen ON search_value (realm, vlen)
`),
newSqlMigration(`
DROP INDEX search_projection_value_id
`),
newSqlMigration(`
DROP TABLE search_projection
`),
newSqlMigration(`
CREATE TABLE search_projection (
realm varchar(32) NOT NULL,
dim smallint NOT NULL,
elen smallint NOT NULL,
dord smallint NOT NULL,
dlen smallint NOT NULL,
vlen smallint NOT NULL,
value_id uuid NOT NULL,
value_variant int NOT NULL,
alias int NOT NULL DEFAULT 0,
PRIMARY KEY (realm, dim, elen, dord, dlen, vlen, value_id, value_variant, alias)
)
`),
newSqlMigration(`
ALTER TABLE network_user ALTER COLUMN auth_type TYPE varchar(32)
`),
newSqlMigration(`
DROP TYPE auth_type
`),
newSqlMigration(`
DROP INDEX location_type_country_code_name
`),
newSqlMigration(`
DROP TABLE location
`),
newSqlMigration(`
CREATE TABLE location (
location_id uuid NOT NULL,
location_type varchar(16) NOT NULL,
location_name varchar(128) NOT NULL,
city_location_id uuid NULL,
region_location_id uuid NULL,
country_location_id uuid NULL,
country_code char(2) NOT NULL,
location_full_name varchar(256) NOT NULL,
PRIMARY KEY (location_id),
UNIQUE(location_full_name)
)
`),
newSqlMigration(`
CREATE INDEX location_type_country_code_name ON location (location_type, country_code, location_name, country_location_id, region_location_id, location_id)
`),
newSqlMigration(`
DROP INDEX location_group_name
`),
newSqlMigration(`
DROP TABLE location_group
`),
newSqlMigration(`
CREATE TABLE location_group (
location_group_id uuid NOT NULL,
location_group_name varchar(128) NOT NULL,
promoted bool NOT NULL DEFAULT false,
PRIMARY KEY (location_group_id),
UNIQUE (location_group_name)
)
`),
newSqlMigration(`
ALTER TABLE network_client ALTER COLUMN device_spec TYPE varchar(256)
`),
newSqlMigration(`
DROP TABLE circle_uc
`),