TSK-1748: Consider default database schemas in SqlConnectionRunner

This commit is contained in:
Joerg Heffner 2021-10-07 09:41:01 +02:00 committed by gitgoodjhe
parent 69ee0235e4
commit 1c7fdea0ce
2 changed files with 46 additions and 16 deletions

View File

@ -2,8 +2,6 @@ package pro.taskana.task.internal.jobs.helper;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;
import javax.sql.DataSource;
import pro.taskana.common.api.TaskanaEngine;
import pro.taskana.common.api.exceptions.SystemException;
@ -12,17 +10,10 @@ import pro.taskana.common.internal.util.CheckedConsumer;
/** Run low level SQL Statements reusing the taskana datasource. */
public class SqlConnectionRunner {
private final DataSource dataSource;
private final TaskanaEngine taskanaEngine;
public SqlConnectionRunner(TaskanaEngine taskanaEngine) {
this(
Objects.requireNonNull(taskanaEngine, "Taskana engine may not be null")
.getConfiguration()
.getDatasource());
}
public SqlConnectionRunner(DataSource dataSource) {
this.dataSource = Objects.requireNonNull(dataSource, "Datasource may not be null.");
this.taskanaEngine = taskanaEngine;
}
/**
@ -32,14 +23,22 @@ public class SqlConnectionRunner {
* @throws SystemException will pass on any checked SQLException as a runtime SystemException
*/
public void runWithConnection(CheckedConsumer<Connection, SQLException> consumer) {
try (Connection connection = getConnection()) {
consumer.accept(connection);
String originalSchema = connection.getSchema();
try {
connection.setSchema(taskanaEngine.getConfiguration().getSchemaName());
consumer.accept(connection);
} finally {
connection.setSchema(originalSchema);
}
} catch (SQLException e) {
throw new SystemException("SQL error while running low level SQL", e);
}
}
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
return taskanaEngine.getConfiguration().getDatasource().getConnection();
}
}

View File

@ -1,16 +1,16 @@
package acceptance.jobs.helper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import acceptance.AbstractAccTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import pro.taskana.common.api.exceptions.NotAuthorizedException;
import pro.taskana.common.api.exceptions.TaskanaRuntimeException;
import pro.taskana.common.test.security.JaasExtension;
import pro.taskana.common.test.security.WithAccessId;
import pro.taskana.task.api.exceptions.TaskNotFoundException;
import pro.taskana.task.api.models.Task;
import pro.taskana.task.internal.jobs.helper.SqlConnectionRunner;
import pro.taskana.task.internal.jobs.helper.TaskUpdatePriorityBatchStatement;
@ -28,7 +28,38 @@ class TaskUpdatePriorityBatchStatementAccTest extends AbstractAccTest {
@Test
@WithAccessId(user = "admin")
void should_updatePriority() throws TaskNotFoundException, NotAuthorizedException {
void should_SetOriginalSchema_When_ExceptionThrown() throws Exception {
taskanaEngine.getConfiguration().setSchemaName("NotExisting");
SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine);
// TSK-1749
assertThat(runner.getConnection().getSchema()).matches("TASKANA\\s|TASKANA|taskana");
String taskId = "TKI:000000000000000000000000000000000050";
final int priorityUpdate = 25;
assertThatThrownBy(
() ->
runner.runWithConnection(
connection -> {
final TaskUpdatePriorityBatchStatement batchStatement =
new TaskUpdatePriorityBatchStatement(connection);
batchStatement.addPriorityUpdate(taskId, priorityUpdate);
batchStatement.executeBatch();
if (!connection.getAutoCommit()) {
connection.commit();
}
}))
.isInstanceOf(TaskanaRuntimeException.class);
// TSK-1749
assertThat(runner.getConnection().getSchema()).matches("TASKANA\\s|TASKANA|taskana");
}
@Test
@WithAccessId(user = "admin")
void should_updatePriority() throws Exception {
// given
SqlConnectionRunner runner = new SqlConnectionRunner(taskanaEngine);
String taskId = "TKI:000000000000000000000000000000000050";