aboutsummaryrefslogtreecommitdiff
path: root/src/exchangedb/common-0002.sql
blob: 388d5547ec0640bd9562473d0bd74cf9cab843a9 (plain)
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
--
-- This file is part of TALER
-- Copyright (C) 2014--2022 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
--

--------------------------------

CREATE TABLE IF NOT EXISTS exchange_tables
  (table_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY
  ,name VARCHAR NOT NULL
  ,version VARCHAR NOT NULL
  ,action VARCHAR NOT NULL
  ,partitioned BOOL NOT NULL
  ,by_range BOOL NOT NULL);
COMMENT ON TABLE exchange_tables
  IS 'Tables of the exchange and their status';
COMMENT ON COLUMN exchange_tables.name
  IS 'Base name of the table (without partition/shard)';
COMMENT ON COLUMN exchange_tables.version
  IS 'Version of the DB in which the given action happened';
COMMENT ON COLUMN exchange_tables.action
  IS 'Action to take on the table (e.g. create, alter, constrain, unconstrain, or drop)';
COMMENT ON COLUMN exchange_tables.partitioned
  IS 'TRUE if the table is partitioned';
COMMENT ON COLUMN exchange_tables.by_range
  IS 'TRUE if the table is partitioned by range';


INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('denominations', 'exchange-0001', 'create', FALSE, FALSE)
   ,('denomination_revocations', 'exchange-0001', 'create', FALSE, FALSE)
   ,('kyc_alerts', 'exchange-0001', 'create', FALSE, FALSE)
   ,('profit_drains', 'exchange-0001', 'create', FALSE, FALSE)
   ,('auditors', 'exchange-0001', 'create', FALSE, FALSE)
   ,('auditor_denom_sigs', 'exchange-0001', 'create', FALSE, FALSE)
   ,('exchange_sign_keys', 'exchange-0001', 'create', FALSE, FALSE)
   ,('signkey_revocations', 'exchange-0001', 'create', FALSE, FALSE)
   ,('extensions', 'exchange-0001', 'create', FALSE, FALSE)
   ,('wire_fee', 'exchange-0001', 'create', FALSE, FALSE)
   ,('global_fee', 'exchange-0001', 'create', FALSE, FALSE)
   ,('wire_accounts', 'exchange-0001', 'create', FALSE, FALSE)
   ,('work_shards', 'exchange-0001', 'create', FALSE, FALSE)
   ,('revolving_work_shards', 'exchange-0001', 'create', FALSE, FALSE)
   ,('partners', 'exchange-0001', 'create', FALSE, FALSE)
   ,('partner_accounts', 'exchange-0001', 'create', FALSE, FALSE)
   ,('purse_actions', 'exchange-0001', 'create', FALSE, FALSE)
   ,('policy_fulfillments', 'exchange-0001', 'create', FALSE, FALSE) -- bad!
   ,('policy_details', 'exchange-0001', 'create', FALSE, FALSE) -- bad!
   ,('wire_targets''exchange-0001', 'create', TRUE, FALSE)
   ,('legitimization_processes', 'exchange-0001', 'create', TRUE, FALSE)
   ,('legitimization_requirements', 'exchange-0001', 'create', TRUE, FALSE)
   ,('reserves', 'exchange-0001', 'create', TRUE, FALSE)
   ,('reserves_in', 'exchange-0001', 'create', TRUE, FALSE)
   ,('reserves_close', 'exchange-0001', 'create', TRUE, FALSE)
   ,('reserves_open_requests', 'exchange-0001', 'create', TRUE, FALSE)
   ,('reserves_open_deposits', 'exchange-0001', 'create', TRUE, FALSE)
   ,('reserves_out', 'exchange-0001', 'create', TRUE, FALSE)
   ,('reserves_out_by_reserve', 'exchange-0001', 'create', TRUE, FALSE)
   ,('known_coins', 'exchange-0001', 'create', TRUE, FALSE)
   ,('refresh_commitments', 'exchange-0001', 'create', TRUE, FALSE)
   ,('refresh_revealed_coins', 'exchange-0001', 'create', TRUE, FALSE)
   ,('refresh_transfer_keys', 'exchange-0001', 'create', TRUE, FALSE)
   ,('refunds', 'exchange-0001', 'create', TRUE, FALSE)
   ,('deposits', 'exchange-0001', 'create', TRUE, FALSE)
   ,('deposits_by_ready', 'exchange-0001', 'create', TRUE, TRUE)
   ,('deposits_for_matching', 'exchange-0001', 'create', TRUE, TRUE)
   ,('wire_out', 'exchange-0001', 'create', TRUE, FALSE)
   ,('aggregation_transient', 'exchange-0001', 'create', TRUE, FALSE)
   ,('aggregation_tracking', 'exchange-0001', 'create', TRUE, FALSE)
   ,('recoup', 'exchange-0001', 'create', TRUE, FALSE)
   ,('recoup_by_reserve', 'exchange-0001', 'create', TRUE, FALSE)
   ,('recoup_refresh', 'exchange-0001', 'create', TRUE, FALSE)
   ,('prewire', 'exchange-0001', 'create', TRUE, FALSE)
   ,('cs_nonce_locks', 'exchange-0001', 'create', TRUE, FALSE)
   ,('purse_requests', 'exchange-0001', 'create', TRUE, FALSE)
   ,('purse_decision', 'exchange-0001', 'create', TRUE, FALSE)
   ,('purse_merges', 'exchange-0001', 'create', TRUE, FALSE)
   ,('account_merges', 'exchange-0001', 'create', TRUE, FALSE)
   ,('contracts', 'exchange-0001', 'create', TRUE, FALSE)
   ,('history_requests', 'exchange-0001', 'create', TRUE, FALSE)
   ,('close_requests', 'exchange-0001', 'create', TRUE, FALSE)
   ,('purse_deposists', 'exchange-0001', 'create', TRUE, FALSE)
   ,('wads_out', 'exchange-0001', 'create', TRUE, FALSE)
   ,('wads_out_entries', 'exchange-0001', 'create', TRUE, FALSE)
   ,('wads_in', 'exchange-0001', 'create', TRUE, FALSE)
   ,('wads_in_entries', 'exchange-0001', 'create', TRUE, FALSE)
 ON CONFLICT DO NOTHING;



-------------------- Tables ----------------------------

CREATE OR REPLACE FUNCTION create_table_purse_deletion(
  IN shard_suffix VARCHAR DEFAULT NULL
)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
  PERFORM create_partitioned_table(
    'CREATE TABLE IF NOT EXISTS %I'
      '(purse_deletion_serial_id BIGINT GENERATED BY DEFAULT AS IDENTITY' -- UNIQUE'
      ',purse_sig BYTEA CHECK (LENGTH(purse_sig)=64)'
      ',XXX VARCHAR NOT NULL'
    ') %s ;'
    ,'purse_deletion'
    ,'PARTITION BY HASH (XXX)'
    ,shard_suffix
  );
  COMMENT ON TABLE purse_deletion
    IS 'signatures affirming explicit purse deletions';
  COMMENT ON COLUMN purse_deletion.purse_sig
    IS 'signature of type XXX';
END
$$;
COMMENT ON FUNCTION create_table_purse_deletion
  IS 'Creates the purse_deletion table';

CREATE OR REPLACE FUNCTION constrain_table_purse_deletion(
  IN partition_suffix VARCHAR
)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE purse_deletion_' || partition_suffix || ' '
      'ADD CONSTRAINT purse_deletion_' || partition_suffix || '_XXX '
        'UNIQUE (XXX)'
  );
END
$$;

CREATE OR REPLACE FUNCTION alter_create_was_deleted_table_purse_requests (
  IN partition_suffix VARCHAR
)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
  EXECUTE FORMAT (
    'ALTER TABLE purse_requests_' || partition_suffix ||
    ' ADD COLUMN'
    ' was_deleted BOOLEAN NOT NULL DEFAULT(FALSE)'
  );
END
$$;

INSERT INTO exchange_tables
    (name
    ,version
    ,action
    ,partitioned
    ,by_range)
  VALUES
    ('purse_deletion'
    ,'exchange-0002'
    ,'create'
    ,TRUE
    ,FALSE),
    ('purse_deletion'
    ,'exchange-0002'
    ,'constrain'
    ,TRUE
    ,FALSE),
    ('purse_requests'
    ,'exchange-0002'
    ,'alter_create_was_deleted'
    ,TRUE
    ,FALSE);