Skip to content

Commit

Permalink
Prevent duplicate key exception from being thrown entirely
Browse files Browse the repository at this point in the history
Affects issues:
- Fixed #3543
  • Loading branch information
AuroraLS3 committed Mar 30, 2024
1 parent 49269d3 commit 8c36f31
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public static String concat(DBType dbType, String one, String two) {

public abstract String dateToHour(String sql);

public abstract String insertOrIgnore();

// https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
public static class MySQL extends Sql {

Expand Down Expand Up @@ -150,6 +152,11 @@ public String dateToDayOfWeek(String sql) {
public String dateToHour(String sql) {
return "HOUR(" + sql + ") % 24";
}

@Override
public String insertOrIgnore() {
return "INSERT IGNORE INTO ";
}
}

// https://sqlite.org/lang_datefunc.html
Expand Down Expand Up @@ -184,5 +191,10 @@ public String dateToDayOfWeek(String sql) {
public String dateToHour(String sql) {
return "strftime('%H'," + sql + ')';
}

@Override
public String insertOrIgnore() {
return "INSERT OR IGNORE INTO ";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ private WebPermissionTable() {
/* Static information class */
}

public static String safeInsertSQL(DBType dbType) {
return dbType.getSql().insertOrIgnore() + TABLE_NAME + " (" + PERMISSION + ") VALUES (?)";
}

public static String createTableSQL(DBType dbType) {
return CreateTableBuilder.create(TABLE_NAME, dbType)
.column(ID, Sql.INT).primaryKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.djrapitops.plan.storage.database.transactions.patches;

import com.djrapitops.plan.delivery.domain.auth.WebPermission;
import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries;
import com.djrapitops.plan.storage.database.sql.tables.webuser.WebPermissionTable;
import com.djrapitops.plan.storage.database.transactions.ExecBatchStatement;
Expand Down Expand Up @@ -56,19 +55,11 @@ public boolean hasBeenApplied() {

@Override
protected void applyPatch() {
try {
storeMissing();
} catch (DBOpException failed) {
if (failed.isDuplicateKeyViolation()) {
retry(failed);
} else {
throw failed;
}
}
storeMissing();
}

private void storeMissing() {
execute(new ExecBatchStatement(WebPermissionTable.INSERT_STATEMENT) {
execute(new ExecBatchStatement(WebPermissionTable.safeInsertSQL(dbType)) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (String permission : missingPermissions) {
Expand All @@ -78,14 +69,4 @@ public void prepare(PreparedStatement statement) throws SQLException {
}
});
}

private void retry(DBOpException failed) {
try {
if (hasBeenApplied()) return;
storeMissing();
} catch (DBOpException anotherFail) {
anotherFail.addSuppressed(failed);
throw anotherFail;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void performOperations() {
missingPermissions.add(permission);
}
}
execute(new ExecBatchStatement(WebPermissionTable.INSERT_STATEMENT) {
execute(new ExecBatchStatement(WebPermissionTable.safeInsertSQL(dbType)) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (String permission : missingPermissions) {
Expand Down

0 comments on commit 8c36f31

Please sign in to comment.