@@ -411,9 +411,11 @@ public class DatabaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts or updates an auction record (typically called by external scraper)
|
* Inserts or updates an auction record (typically called by external scraper)
|
||||||
|
* Handles both auction_id conflicts and url uniqueness constraints
|
||||||
*/
|
*/
|
||||||
synchronized void upsertAuction(AuctionInfo auction) throws SQLException {
|
synchronized void upsertAuction(AuctionInfo auction) throws SQLException {
|
||||||
var sql = """
|
// First try to INSERT with ON CONFLICT on auction_id
|
||||||
|
var insertSql = """
|
||||||
INSERT INTO auctions (auction_id, title, location, city, country, url, type, lot_count, closing_time, discovered_at)
|
INSERT INTO auctions (auction_id, title, location, city, country, url, type, lot_count, closing_time, discovered_at)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
ON CONFLICT(auction_id) DO UPDATE SET
|
ON CONFLICT(auction_id) DO UPDATE SET
|
||||||
@@ -427,7 +429,8 @@ public class DatabaseService {
|
|||||||
closing_time = excluded.closing_time
|
closing_time = excluded.closing_time
|
||||||
""";
|
""";
|
||||||
|
|
||||||
try (var conn = DriverManager.getConnection(url); var ps = conn.prepareStatement(sql)) {
|
try (var conn = DriverManager.getConnection(url)) {
|
||||||
|
try (var ps = conn.prepareStatement(insertSql)) {
|
||||||
ps.setLong(1, auction.auctionId());
|
ps.setLong(1, auction.auctionId());
|
||||||
ps.setString(2, auction.title());
|
ps.setString(2, auction.title());
|
||||||
ps.setString(3, auction.location());
|
ps.setString(3, auction.location());
|
||||||
@@ -439,6 +442,41 @@ public class DatabaseService {
|
|||||||
ps.setString(9, auction.firstLotClosingTime() != null ? auction.firstLotClosingTime().toString() : null);
|
ps.setString(9, auction.firstLotClosingTime() != null ? auction.firstLotClosingTime().toString() : null);
|
||||||
ps.setLong(10, Instant.now().getEpochSecond());
|
ps.setLong(10, Instant.now().getEpochSecond());
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
// If it fails due to UNIQUE constraint on url, try updating by url instead
|
||||||
|
if (e.getMessage().contains("UNIQUE constraint failed: auctions.url")) {
|
||||||
|
var updateByUrlSql = """
|
||||||
|
UPDATE auctions SET
|
||||||
|
auction_id = ?,
|
||||||
|
title = ?,
|
||||||
|
location = ?,
|
||||||
|
city = ?,
|
||||||
|
country = ?,
|
||||||
|
type = ?,
|
||||||
|
lot_count = ?,
|
||||||
|
closing_time = ?
|
||||||
|
WHERE url = ?
|
||||||
|
""";
|
||||||
|
try (var ps = conn.prepareStatement(updateByUrlSql)) {
|
||||||
|
ps.setLong(1, auction.auctionId());
|
||||||
|
ps.setString(2, auction.title());
|
||||||
|
ps.setString(3, auction.location());
|
||||||
|
ps.setString(4, auction.city());
|
||||||
|
ps.setString(5, auction.country());
|
||||||
|
ps.setString(6, auction.typePrefix());
|
||||||
|
ps.setInt(7, auction.lotCount());
|
||||||
|
ps.setString(8, auction.firstLotClosingTime() != null ? auction.firstLotClosingTime().toString() : null);
|
||||||
|
ps.setString(9, auction.url());
|
||||||
|
|
||||||
|
int updated = ps.executeUpdate();
|
||||||
|
if (updated == 0) {
|
||||||
|
log.warn("Could not insert or update auction with url={}, auction_id={}", auction.url(), auction.auctionId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user