Use an upsert statement for handling pubsub.

This commit is contained in:
Kavin 2023-07-06 14:54:45 +01:00
parent 6a2c688f50
commit 1dbda14335
No known key found for this signature in database
GPG key ID: 6E4598CA5C92C41F

View file

@ -57,17 +57,14 @@ public class PubSubHelper {
} }
public static void updatePubSub(String channelId) { public static void updatePubSub(String channelId) {
var pubsub = DatabaseHelper.getPubSubFromId(channelId);
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) { try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
s.beginTransaction(); var tr = s.beginTransaction();
if (pubsub == null) { s.createNativeMutationQuery("INSERT INTO pubsub (id, subbed_at) VALUES (?, ?) " +
pubsub = new PubSub(channelId, System.currentTimeMillis()); "ON CONFLICT (id) DO UPDATE SET subbed_at = excluded.subbed_at")
s.insert(pubsub); .setParameter(1, channelId)
} else { .setParameter(2, System.currentTimeMillis())
pubsub.setSubbedAt(System.currentTimeMillis()); .executeUpdate();
s.update(pubsub); tr.commit();
}
s.getTransaction().commit();
} }
} }
} }