Skip to content

Commit

Permalink
feat: move long migrations after the minimal migration
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Nov 4, 2024
1 parent d6e7e09 commit 6bddcd1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
24 changes: 0 additions & 24 deletions internal/storage/bucket/migrations/11-make-stateless/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ add column inserted_at timestamp without time zone default (transaction_date() a
alter column timestamp set default (transaction_date() at time zone 'utc'),
alter column id type bigint;

drop index transactions_reference;
create unique index transactions_reference on transactions (ledger, reference);
create index transactions_sequences on transactions (id, seq);

alter table logs
Expand Down Expand Up @@ -482,10 +480,6 @@ $do$
vsql = 'select setval(''"log_id_' || ledger.id || '"'', coalesce((select max(id) + 1 from logs where ledger = ''' || ledger.name || '''), 1)::bigint, false)';
execute vsql;

-- enable post commit effective volumes synchronously
vsql = 'create index "pcev_' || ledger.id || '" on moves (accounts_address, asset, effective_date desc) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create trigger "set_effective_volumes_' || ledger.id || '" before insert on moves for each row when (new.ledger = ''' || ledger.name || ''') execute procedure set_effective_volumes()';
execute vsql;

Expand All @@ -508,30 +502,12 @@ $do$
vsql = 'create trigger "insert_transaction_metadata_history_' || ledger.id || '" after insert on "transactions" for each row when (new.ledger = ''' || ledger.name || ''') execute procedure insert_transaction_metadata_history()';
execute vsql;

vsql = 'create index "transactions_sources_' || ledger.id || '" on transactions using gin (sources jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "transactions_destinations_' || ledger.id || '" on transactions using gin (destinations jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create trigger "transaction_set_addresses_' || ledger.id || '" before insert on transactions for each row when (new.ledger = ''' || ledger.name || ''') execute procedure set_transaction_addresses()';
execute vsql;

vsql = 'create index "accounts_address_array_' || ledger.id || '" on accounts using gin (address_array jsonb_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "accounts_address_array_length_' || ledger.id || '" on accounts (jsonb_array_length(address_array)) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create trigger "accounts_set_address_array_' || ledger.id || '" before insert on accounts for each row when (new.ledger = ''' || ledger.name || ''') execute procedure set_address_array_for_account()';
execute vsql;

vsql = 'create index "transactions_sources_arrays_' || ledger.id || '" on transactions using gin (sources_arrays jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "transactions_destinations_arrays_' || ledger.id || '" on transactions using gin (destinations_arrays jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create trigger "transaction_set_addresses_segments_' || ledger.id || '" before insert on "transactions" for each row when (new.ledger = ''' || ledger.name || ''') execute procedure set_transaction_addresses_segments()';
execute vsql;
end loop;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: Create transaction reference index concurrently
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create unique index concurrently transactions_reference2 on "{{.Schema}}".transactions (ledger, reference);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: Create ledger indexes
36 changes: 36 additions & 0 deletions internal/storage/bucket/migrations/13-create-ledger-indexes/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
set search_path = '{{.Schema}}';

drop index transactions_reference;
alter index transactions_reference2 rename to transactions_reference;

DO
$do$
declare
ledger record;
vsql text;
BEGIN
for ledger in select * from _system.ledgers where bucket = current_schema loop
-- enable post commit effective volumes synchronously
vsql = 'create index "pcev_' || ledger.id || '" on moves (accounts_address, asset, effective_date desc) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "transactions_sources_' || ledger.id || '" on transactions using gin (sources jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "transactions_destinations_' || ledger.id || '" on transactions using gin (destinations jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "accounts_address_array_' || ledger.id || '" on accounts using gin (address_array jsonb_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "accounts_address_array_length_' || ledger.id || '" on accounts (jsonb_array_length(address_array)) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "transactions_sources_arrays_' || ledger.id || '" on transactions using gin (sources_arrays jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;

vsql = 'create index "transactions_destinations_arrays_' || ledger.id || '" on transactions using gin (destinations_arrays jsonb_path_ops) where ledger = ''' || ledger.name || '''';
execute vsql;
end loop;
END
$do$;

0 comments on commit 6bddcd1

Please sign in to comment.