Skip to content

Commit

Permalink
fix: sequences migration from v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 19, 2024
1 parent a176231 commit b3772f5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
11 changes: 0 additions & 11 deletions internal/api/v1/controllers_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ func mapTransactionToV1(tx ledger.Transaction) any {
}
}

func mapExpandedTransactionToV1(tx ledger.Transaction) any {
return struct {
ledger.Transaction
TxID int `json:"txid"`
ID int `json:"-"`
}{
Transaction: tx,
TxID: tx.ID,
}
}

func buildGetTransactionsQuery(r *http.Request) query.Builder {
clauses := make([]query.Builder, 0)
if after := r.URL.Query().Get("after"); after != "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1/controllers_transactions_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ func listTransactions(w http.ResponseWriter, r *http.Request) {
return
}

Check warning on line 34 in internal/api/v1/controllers_transactions_list.go

View check run for this annotation

Codecov / codecov/patch

internal/api/v1/controllers_transactions_list.go#L32-L34

Added lines #L32 - L34 were not covered by tests

api.RenderCursor(w, *bunpaginate.MapCursor(cursor, mapExpandedTransactionToV1))
api.RenderCursor(w, *bunpaginate.MapCursor(cursor, mapTransactionToV1))
}
2 changes: 1 addition & 1 deletion internal/api/v1/controllers_transactions_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func readTransaction(w http.ResponseWriter, r *http.Request) {
return

Check warning on line 40 in internal/api/v1/controllers_transactions_read.go

View check run for this annotation

Codecov / codecov/patch

internal/api/v1/controllers_transactions_read.go#L40

Added line #L40 was not covered by tests
}

api.Ok(w, mapExpandedTransactionToV1(*tx))
api.Ok(w, mapTransactionToV1(*tx))
}
39 changes: 20 additions & 19 deletions internal/storage/bucket/migrations/11-make-stateless.sql
Original file line number Diff line number Diff line change
Expand Up @@ -445,68 +445,69 @@ $do$
vsql = 'create sequence "transaction_id_' || ledger.id || '" owned by transactions.id';
execute vsql;

vsql = 'select setval("transaction_id_' || ledger.id || '", coalesce((select max(id) + 1 from transactions where ledger = ledger.name), 1)::bigint, false)';
vsql = 'select setval(''"transaction_id_' || ledger.id || '"'', coalesce((select max(id) + 1 from transactions where ledger = ''' || ledger.name || '''), 1)::bigint, false)';
raise info '%', vsql;
execute vsql;

-- create a sequence for logs by ledger instead of a sequence of the table as we want to have contiguous ids
-- notes: we can still have "holes" on id since a sql transaction can be reverted after a usage of the sequence
vsql = 'create sequence "log_id_' || ledger.id || '" owned by logs.id';
execute vsql;

vsql = 'select setval("log_id_' || ledger.id || '", coalesce((select max(id) + 1 from logs where ledger = ledger.name), 1)::bigint, false)';
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';
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()';
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;

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

-- logs hash
vsql = 'create trigger "set_log_hash_' || ledger.id || '" before insert on logs for each row when (new.ledger = ledger.name) execute procedure set_log_hash()';
vsql = 'create trigger "set_log_hash_' || ledger.id || '" before insert on logs for each row when (new.ledger = ''' || ledger.name || ''') execute procedure set_log_hash()';
execute vsql;

vsql = 'create trigger "update_account_metadata_history_' || ledger.id || '" after update on "accounts" for each row when (new.ledger = ledger.name) execute procedure update_account_metadata_history()';
vsql = 'create trigger "update_account_metadata_history_' || ledger.id || '" after update on "accounts" for each row when (new.ledger = ''' || ledger.name || ''') execute procedure update_account_metadata_history()';
execute vsql;

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

vsql = 'create trigger "update_transaction_metadata_history_' || ledger.id || '" after update on "transactions" for each row when (new.ledger = ledger.name) execute procedure update_transaction_metadata_history()';
vsql = 'create trigger "update_transaction_metadata_history_' || ledger.id || '" after update on "transactions" for each row when (new.ledger = ''' || ledger.name || ''') execute procedure update_transaction_metadata_history()';
execute vsql;

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()';
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';
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';
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()';
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';
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';
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()';
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';
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';
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()';
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;
END
Expand Down

0 comments on commit b3772f5

Please sign in to comment.