|
1 | 1 | do $$ |
2 | | - declare |
3 | | - _offset integer := 0; |
4 | | - _batch_size integer := 1000; |
5 | 2 | begin |
6 | | - set search_path = '{{ .Schema }}'; |
7 | | - |
8 | | --- drop table if exists moves_view; |
9 | | --- |
10 | | --- create table moves_view as |
11 | | --- select transactions_seq, public.aggregate_objects(jsonb_build_object(accounts_address, volumes)) as volumes |
12 | | --- from ( |
13 | | --- select transactions_seq, accounts_address, public.aggregate_objects(json_build_object(asset, json_build_object('input', (post_commit_volumes).inputs, 'output', (post_commit_volumes).outputs))::jsonb) as volumes |
14 | | --- from ( |
15 | | --- SELECT DISTINCT ON (moves.transactions_seq, accounts_address, asset) moves.transactions_seq, accounts_address, asset, |
16 | | --- first_value(post_commit_volumes) OVER ( |
17 | | --- PARTITION BY moves.transactions_seq, accounts_address, asset |
18 | | --- ORDER BY seq DESC |
19 | | --- ) AS post_commit_volumes |
20 | | --- FROM moves |
21 | | --- where insertion_date < ( |
22 | | --- select tstamp from goose_db_version where version_id = 12 |
23 | | --- ) |
24 | | --- ) moves |
25 | | --- group by transactions_seq, accounts_address |
26 | | --- ) data |
27 | | --- group by transactions_seq; |
28 | | --- |
29 | | --- create index moves_view_idx on moves_view(transactions_seq); |
30 | | --- -- speed up hash join when updating rows later |
31 | | --- alter table moves_view add foreign key(transactions_seq) references transactions(seq); |
32 | | --- |
33 | | --- if (select count(*) from moves_view) = 0 then |
34 | | --- drop table moves_view; |
35 | | --- return; |
36 | | --- end if; |
37 | | --- |
38 | | --- perform pg_notify('migrations-{{ .Schema }}', 'init: ' || (select count(*) from moves_view)); |
39 | | --- |
40 | | --- loop |
41 | | --- with data as ( |
42 | | --- select transactions_seq, volumes |
43 | | --- from moves_view |
44 | | --- -- play better than offset/limit |
45 | | --- where transactions_seq >= _offset and transactions_seq < _offset + _batch_size |
46 | | --- ) |
47 | | --- update transactions |
48 | | --- set post_commit_volumes = data.volumes |
49 | | --- from data |
50 | | --- where transactions.seq = data.transactions_seq; |
51 | | --- |
52 | | --- if not found then |
53 | | --- drop table moves_view; |
54 | | --- exit; |
55 | | --- end if; |
56 | | --- |
57 | | --- _offset = _offset + _batch_size; |
58 | | --- |
59 | | --- perform pg_notify('migrations-{{ .Schema }}', 'continue: ' || _batch_size); |
60 | | --- |
61 | | --- commit; |
62 | | --- end loop; |
63 | | --- |
64 | | --- drop table if exists moves_view; |
| 3 | + raise notice 'Migration superseded by next migration'; |
65 | 4 | end |
66 | 5 | $$; |
67 | 6 |
|
0 commit comments