Replace INSERT OR REPLACE with INSERT ON CONFLICT DO UPDATE

The latter doesn't cause foreign key delete cascades
This commit is contained in:
AsamK 2025-09-14 15:28:55 +02:00
parent f469a4d166
commit edd52a97b4
6 changed files with 16 additions and 25 deletions

View File

@ -58,9 +58,10 @@ public class UnknownStorageIdStore {
}
public void addUnknownStorageIds(Connection connection, Collection<StorageId> storageIds) throws SQLException {
deleteUnknownStorageIds(connection, storageIds);
final var sql = (
"""
INSERT OR REPLACE INTO %s (type, storage_id)
INSERT INTO %s (type, storage_id)
VALUES (?, ?)
"""
).formatted(TABLE_STORAGE_ID);

View File

@ -165,8 +165,9 @@ public class GroupStore {
if (endorsements != null) {
final var sqlInsertMember = """
INSERT OR REPLACE INTO %s (group_id, recipient_id, endorsement)
INSERT INTO %s (group_id, recipient_id, endorsement)
VALUES (?, ?, ?)
ON CONFLICT (group_id, recipient_id) DO UPDATE SET endorsement=excluded.endorsement
""".formatted(TABLE_GROUP_V2_MEMBER);
try (final var statement = connection.prepareStatement(sqlInsertMember)) {
for (final var entry : endorsements.entrySet()) {
@ -612,8 +613,9 @@ public class GroupStore {
}
}
final var sql = """
INSERT OR REPLACE INTO %s (_id, group_id, group_id_v2, name, color, expiration_time, blocked, archived, storage_id)
INSERT INTO %s (_id, group_id, group_id_v2, name, color, expiration_time, blocked, archived, storage_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (_id) DO UPDATE SET group_id=excluded.group_id, group_id_v2=excluded.group_id_v2, name=excluded.name, color=excluded.color, expiration_time=excluded.expiration_time, blocked=excluded.blocked, archived=excluded.archived, storage_id=excluded.storage_id
RETURNING _id
""".formatted(TABLE_GROUP_V1);
try (final var statement = connection.prepareStatement(sql)) {
@ -641,8 +643,9 @@ public class GroupStore {
}
}
final var sqlInsertMember = """
INSERT OR REPLACE INTO %s (group_id, recipient_id)
INSERT INTO %s (group_id, recipient_id)
VALUES (?, ?)
ON CONFLICT (group_id, recipient_id) DO NOTHING
""".formatted(TABLE_GROUP_V1_MEMBER);
try (final var statement = connection.prepareStatement(sqlInsertMember)) {
for (final var recipient : groupV1.getMembers()) {

View File

@ -280,8 +280,9 @@ public class IdentityKeyStore {
identityInfo.getDateAddedTimestamp());
final var sql = (
"""
INSERT OR REPLACE INTO %s (address, identity_key, added_timestamp, trust_level)
INSERT INTO %s (address, identity_key, added_timestamp, trust_level)
VALUES (?, ?, ?, ?)
ON CONFLICT (address) DO UPDATE SET identity_key=excluded.identity_key, added_timestamp=excluded.added_timestamp, trust_level=excluded.trust_level
"""
).formatted(TABLE_IDENTITY);
try (final var statement = connection.prepareStatement(sql)) {

View File

@ -171,27 +171,11 @@ public class SenderKeyRecordStore implements SenderKeyStore {
final Key key,
final SenderKeyRecord senderKeyRecord
) throws SQLException {
final var sqlUpdate = """
UPDATE %s
SET record = ?
WHERE address = ? AND device_id = ? and distribution_id = ?
""".formatted(TABLE_SENDER_KEY);
try (final var statement = connection.prepareStatement(sqlUpdate)) {
statement.setBytes(1, senderKeyRecord.serialize());
statement.setString(2, key.address());
statement.setLong(3, key.deviceId());
statement.setBytes(4, UuidUtil.toByteArray(key.distributionId()));
final var rows = statement.executeUpdate();
if (rows > 0) {
return;
}
}
// Record doesn't exist yet, creating a new one
final var sqlInsert = (
"""
INSERT OR REPLACE INTO %s (address, device_id, distribution_id, record, created_timestamp)
INSERT INTO %s (address, device_id, distribution_id, record, created_timestamp)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT (address, device_id, distribution_id) DO UPDATE SET record=excluded.record
"""
).formatted(TABLE_SENDER_KEY);
try (final var statement = connection.prepareStatement(sqlInsert)) {

View File

@ -197,8 +197,9 @@ public class SenderKeySharedStore {
) throws SQLException {
final var sql = (
"""
INSERT OR REPLACE INTO %s (address, device_id, distribution_id, timestamp)
INSERT INTO %s (address, device_id, distribution_id, timestamp)
VALUES (?, ?, ?, ?)
ON CONFLICT (address, device_id, distribution_id) DO UPDATE SET timestamp=excluded.timestamp
"""
).formatted(TABLE_SENDER_KEY_SHARED);
try (final var statement = connection.prepareStatement(sql)) {

View File

@ -351,8 +351,9 @@ public class SessionStore implements SignalServiceSessionStore {
}
final var sql = """
INSERT OR REPLACE INTO %s (account_id_type, address, device_id, record)
INSERT INTO %s (account_id_type, address, device_id, record)
VALUES (?, ?, ?, ?)
ON CONFLICT (account_id_type, address, device_id) DO UPDATE SET record=excluded.record
""".formatted(TABLE_SESSION);
try (final var statement = connection.prepareStatement(sql)) {
statement.setInt(1, accountIdType);