Skip to content

Commit

Permalink
fix for #826
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszwalacik committed Jul 5, 2019
1 parent 8afba91 commit 3c77c91
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.javers.repository.sql.ConnectionProvider;
import org.polyjdbc.core.PolyJDBC;
import org.polyjdbc.core.dialect.*;
import org.polyjdbc.core.exception.SchemaInspectionException;
import org.polyjdbc.core.schema.SchemaInspector;
import org.polyjdbc.core.schema.SchemaManager;
import org.polyjdbc.core.schema.model.IndexBuilder;
Expand Down Expand Up @@ -215,18 +216,58 @@ private boolean columnExists(String tableName, String colName) {
}

private void ensureTable(String tableName, Schema schema) {

String schemaName = (schema.getSchemaName() == null || schema.getSchemaName().isEmpty())
? "default" : schema.getSchemaName();
if (schemaInspector.relationExists(tableName)) {
logger.info("table {}.{} exists", schemaName, tableName);
? "" : schema.getSchemaName();

if (relationExists(tableName, schema.getSchemaName())) {
logger.debug("table {}.{} exists", schemaName, tableName);
return;
}
logger.info("creating javers table {}.{} ...", schemaName, tableName);
schemaManager.create(schema);

}

boolean relationExists(String name, String schemaName) {
try {
Connection connection = connectionProvider.getConnection();

DatabaseMetaData metadata = connection.getMetaData();
String catalog = connection.getCatalog();

ResultSet resultSet = metadata.getTables(
catalog,
convertCase(schemaName, metadata),
convertCase(name, metadata),
new String[]{"TABLE"});

if (schemaName != null) {
return resultSet.next();
} else {
String tableSchemaName;
do {
if (!resultSet.next()) {
return false;
}

tableSchemaName = resultSet.getString("TABLE_SCHEM");
} while(tableSchemaName != null && !tableSchemaName.equalsIgnoreCase("public") && !tableSchemaName.equals("") && (!(this.dialect instanceof MsSqlDialect) || !tableSchemaName.equalsIgnoreCase("dbo")));

return true;
}
} catch (SQLException var4) {
throw new SchemaInspectionException("RELATION_LOOKUP_ERROR", "Failed to obtain relation list when looking for relation " + name, var4);
}
}

private String convertCase(String identifier, DatabaseMetaData metadata) throws SQLException {
if (metadata.storesLowerCaseIdentifiers()) {
return identifier.toLowerCase();
} else {
return metadata.storesUpperCaseIdentifiers() ? identifier.toUpperCase() : identifier;
}
}

private void addStringColumn(String tableName, String colName, int len) {
logger.warn("column " + tableName + "." + colName + " not exists, running ALTER TABLE ...");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.javers.repository.sql.schema;

import java.util.Optional;

/**
* TODO should be replaced with Java8 interface with default impl
*/
Expand Down Expand Up @@ -57,4 +59,8 @@ protected String getGlobalIdPkSeqWithSchema() {
protected String getSequenceNameWithSchema(String pkColName){
return tableNameProvider.getSequenceNameWithSchema(pkColName);
}

protected Optional<String> getSchemaName() {
return tableNameProvider.getSchemaName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ DBObjectName getCommitPropertyTableName() {
DBObjectName getSnapshotTableName() {
return new DBObjectName(schemaName, SNAPSHOT_TABLE_NAME);
}

Optional<String> getSchemaName() {
return schemaName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import java.sql.DriverManager
class H2SqlRepositoryE2EWithSchemaTest extends H2SqlRepositoryE2ETest {

Connection createConnection() {
DriverManager.getConnection("jdbc:h2:mem:WithSchemaTest")
DriverManager.getConnection(
"jdbc:h2:mem:javers;INIT=create schema if not exists javers")
}

String getSchema() {
return "public"
return "javers"
}
}

This file was deleted.

0 comments on commit 3c77c91

Please sign in to comment.