File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1466,17 +1466,6 @@ def save_daily_data(
14661466 batch_dates = list (records_by_date .keys ())
14671467
14681468 def _write (session : Session ) -> int :
1469- existing_dates = set (
1470- session .execute (
1471- select (StockDaily .date ).where (
1472- and_ (
1473- StockDaily .code == code ,
1474- StockDaily .date .in_ (batch_dates ),
1475- )
1476- )
1477- ).scalars ().all ()
1478- )
1479-
14801469 if self ._is_sqlite_engine :
14811470 stmt = sqlite_insert (StockDaily ).values (records )
14821471 excluded = stmt .excluded
@@ -1531,7 +1520,20 @@ def _write(session: Session) -> int:
15311520 existing .data_source = record ['data_source' ]
15321521 existing .updated_at = record ['updated_at' ]
15331522
1534- return len (batch_dates ) - len (existing_dates )
1523+ # Derive actual insert count from upsert result: created_at is
1524+ # NOT in the ON CONFLICT SET clause, so only genuinely new rows
1525+ # carry the current `now` timestamp.
1526+ session .flush ()
1527+ new_count = session .execute (
1528+ select (func .count ()).select_from (StockDaily ).where (
1529+ and_ (
1530+ StockDaily .code == code ,
1531+ StockDaily .date .in_ (batch_dates ),
1532+ StockDaily .created_at == now ,
1533+ )
1534+ )
1535+ ).scalar ()
1536+ return new_count or 0
15351537
15361538 try :
15371539 saved_count = self ._run_write_transaction (
You can’t perform that action at this time.
0 commit comments