Skip to content

Commit

Permalink
do not fail table creation if table already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
pwinckles committed Jan 18, 2022
1 parent 6a17748 commit 47a88b9
Showing 1 changed file with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

Expand All @@ -44,6 +45,11 @@ public class TableCreator {

private static final Logger LOG = LoggerFactory.getLogger(TableCreator.class);

private static final int MYSQL_ACCESS_DENIED_ERROR = 1142;
private static final int MYSQL_NO_TABLE_ERROR = 1146;

private static final String TABLE_TEST_QUERY = "SELECT 1 FROM %s LIMIT 1";

private static final String LOCK_TABLE_FILE = "ocfl_object_lock.ddl.tmpl";
private static final String OBJECT_DETAILS_TABLE_FILE = "ocfl_object_details.ddl.tmpl";

Expand Down Expand Up @@ -72,22 +78,45 @@ public void createObjectDetailsTable(String tableName) {
private void createTable(String tableName, String fileName) {
Enforce.notBlank(tableName, "tableName cannot be blank");
try (var connection = dataSource.getConnection()) {
var filePath = getSqlFilePath(fileName);
LOG.debug("Loading {}", filePath);
if (filePath != null) {
try (var stream = this.getClass().getResourceAsStream("/" + filePath)) {
var ddlTemplate = streamToString(stream);
var ddl = String.format(ddlTemplate, tableName);
try (var statement = connection.prepareStatement(ddl)) {
statement.executeUpdate();
try {
var filePath = getSqlFilePath(fileName);
LOG.debug("Loading {}", filePath);
if (filePath != null) {
try (var stream = this.getClass().getResourceAsStream("/" + filePath)) {
var ddlTemplate = streamToString(stream);
var ddl = String.format(ddlTemplate, tableName);
try (var statement = connection.prepareStatement(ddl)) {
statement.executeUpdate();
}
}
}
} catch (SQLException e) {
// MySQL/MariaDB fail create if not exists queries when the user does not have permission even if the
// table exists
if (e.getErrorCode() == MYSQL_ACCESS_DENIED_ERROR) {
testTableExistence(connection, tableName);
} else {
throw new OcflDbException(e);
}
}
} catch (SQLException | IOException e) {
throw new OcflDbException(e);
}
}

private void testTableExistence(Connection connection, String tableName) {
try (var statement = connection
.prepareStatement(String.format(TABLE_TEST_QUERY, tableName))) {
statement.execute();
} catch (SQLException e) {
if (e.getErrorCode() == MYSQL_NO_TABLE_ERROR) {
throw new OcflDbException(String.format(
"Table %s does not exist and user does not have permission to create it.", tableName));
}
throw new OcflDbException(e);
}
}

private String getSqlFilePath(String fileName) {
var scriptDir = dbScriptDir.get(dbType);

Expand Down

0 comments on commit 47a88b9

Please sign in to comment.