1+ DO $$
2+ DECLARE
3+ r record;
4+ BEGIN
5+ FOR r IN
6+ SELECT
7+ p .oid ::regprocedure AS sig
8+ FROM pg_proc p
9+ JOIN pg_namespace n ON n .oid = p .pronamespace
10+ WHERE p .proname = ' process_piece_deal'
11+ AND n .nspname = current_schema()
12+ LOOP
13+ EXECUTE format(' DROP FUNCTION %s;' , r .sig );
14+ END LOOP;
15+ END
16+ $$;
17+
18+ CREATE OR REPLACE FUNCTION process_piece_deal (
19+ _id TEXT ,
20+ _piece_cid TEXT ,
21+ _boost_deal BOOLEAN ,
22+ _sp_id BIGINT ,
23+ _sector_num BIGINT ,
24+ _piece_offset BIGINT ,
25+ _piece_length BIGINT , -- padded length
26+ _raw_size BIGINT ,
27+ _indexed BOOLEAN ,
28+ _piece_ref BIGINT DEFAULT NULL ,
29+ _legacy_deal BOOLEAN DEFAULT FALSE,
30+ _chain_deal_id BIGINT DEFAULT 0
31+ )
32+ RETURNS VOID AS $$
33+ BEGIN
34+ -- Insert or update the market_piece_metadata table
35+ INSERT INTO market_piece_metadata (piece_cid, piece_size, indexed)
36+ VALUES (_piece_cid, _piece_length, _indexed)
37+ ON CONFLICT (piece_cid, piece_size) DO UPDATE SET
38+ indexed = CASE
39+ WHEN market_piece_metadata .indexed = FALSE THEN EXCLUDED .indexed
40+ ELSE market_piece_metadata .indexed
41+ END;
42+
43+ -- Insert into the market_piece_deal table
44+ INSERT INTO market_piece_deal (
45+ id, piece_cid, boost_deal, legacy_deal, chain_deal_id,
46+ sp_id, sector_num, piece_offset, piece_length, raw_size, piece_ref
47+ ) VALUES (
48+ _id, _piece_cid, _boost_deal, _legacy_deal, _chain_deal_id,
49+ _sp_id, _sector_num, _piece_offset, _piece_length, _raw_size, _piece_ref
50+ ) ON CONFLICT (id, sp_id, piece_cid, piece_length) DO NOTHING;
51+
52+ END;
53+ $$ LANGUAGE plpgsql;
0 commit comments