@@ -216,6 +216,7 @@ async def _store_funding_rates(
216216 ]
217217
218218 # Bulk upsert using asyncpg
219+ # Use COALESCE to preserve existing data when backfill provides NULL
219220 query = """
220221 INSERT INTO funding_rates (
221222 starlisting_id, time, funding_rate, premium,
@@ -226,13 +227,13 @@ async def _store_funding_rates(
226227 $7::numeric[], $8::numeric[], $9::timestamptz[])
227228 ON CONFLICT (time, starlisting_id)
228229 DO UPDATE SET
229- funding_rate = EXCLUDED.funding_rate,
230- premium = EXCLUDED.premium,
231- mark_price = EXCLUDED.mark_price,
232- index_price = EXCLUDED.index_price,
233- oracle_price = EXCLUDED.oracle_price,
234- mid_price = EXCLUDED.mid_price,
235- next_funding_time = EXCLUDED.next_funding_time
230+ funding_rate = COALESCE( EXCLUDED.funding_rate, funding_rates.funding_rate) ,
231+ premium = COALESCE( EXCLUDED.premium, funding_rates.premium) ,
232+ mark_price = COALESCE( EXCLUDED.mark_price, funding_rates.mark_price) ,
233+ index_price = COALESCE( EXCLUDED.index_price, funding_rates.index_price) ,
234+ oracle_price = COALESCE( EXCLUDED.oracle_price, funding_rates.oracle_price) ,
235+ mid_price = COALESCE( EXCLUDED.mid_price, funding_rates.mid_price) ,
236+ next_funding_time = COALESCE( EXCLUDED.next_funding_time, funding_rates.next_funding_time)
236237 """
237238
238239 # Transpose records for UNNEST
0 commit comments