Associate receipts with blocks instead of transactions

- Allows us to fetch receipts by block and persist even when
associated transaction is unknown. (The associated transaction
can still be derived from the tx_hash column at query time, but
is an expensive operation to require for inserts).
This commit is contained in:
Rob Mulholand
2018-05-04 10:52:28 -05:00
parent 4711708397
commit 462f94d84a
4 changed files with 62 additions and 25 deletions
@@ -0,0 +1,23 @@
BEGIN;
ALTER TABLE receipts
ADD COLUMN transaction_id INT;
UPDATE receipts
SET transaction_id = (
SELECT id FROM transactions WHERE transactions.hash = receipts.tx_hash
);
ALTER TABLE receipts
ALTER COLUMN transaction_id SET NOT NULL;
ALTER TABLE receipts
ADD CONSTRAINT transaction_fk
FOREIGN KEY (transaction_id)
REFERENCES transactions (id)
ON DELETE CASCADE;
ALTER TABLE receipts
DROP COLUMN block_id;
COMMIT;
@@ -0,0 +1,23 @@
BEGIN;
ALTER TABLE receipts
ADD COLUMN block_id INT;
UPDATE receipts
SET block_id = (
SELECT block_id FROM transactions WHERE transactions.id = receipts.transaction_id
);
ALTER TABLE receipts
ALTER COLUMN block_id SET NOT NULL;
ALTER TABLE receipts
ADD CONSTRAINT blocks_fk
FOREIGN KEY (block_id)
REFERENCES blocks (id)
ON DELETE CASCADE;
ALTER TABLE receipts
DROP COLUMN transaction_id;
COMMIT;
+10 -17
View File
@@ -206,13 +206,13 @@ ALTER SEQUENCE public.nodes_id_seq OWNED BY public.eth_nodes.id;
CREATE TABLE public.receipts (
id integer NOT NULL,
transaction_id integer NOT NULL,
contract_address character varying(42),
cumulative_gas_used numeric,
gas_used numeric,
state_root character varying(66),
status integer,
tx_hash character varying(66)
tx_hash character varying(66),
block_id integer NOT NULL
);
@@ -496,13 +496,6 @@ CREATE INDEX block_number_index ON public.blocks USING btree (number);
CREATE INDEX node_id_index ON public.blocks USING btree (eth_node_id);
--
-- Name: transaction_id_index; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX transaction_id_index ON public.receipts USING btree (transaction_id);
--
-- Name: tx_from_index; Type: INDEX; Schema: public; Owner: -
--
@@ -525,6 +518,14 @@ ALTER TABLE ONLY public.transactions
ADD CONSTRAINT blocks_fk FOREIGN KEY (block_id) REFERENCES public.blocks(id) ON DELETE CASCADE;
--
-- Name: receipts blocks_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.receipts
ADD CONSTRAINT blocks_fk FOREIGN KEY (block_id) REFERENCES public.blocks(id) ON DELETE CASCADE;
--
-- Name: blocks node_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@@ -541,14 +542,6 @@ ALTER TABLE ONLY public.logs
ADD CONSTRAINT receipts_fk FOREIGN KEY (receipt_id) REFERENCES public.receipts(id) ON DELETE CASCADE;
--
-- Name: receipts transaction_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.receipts
ADD CONSTRAINT transaction_fk FOREIGN KEY (transaction_id) REFERENCES public.transactions(id) ON DELETE CASCADE;
--
-- PostgreSQL database dump complete
--