Skip to content

Commit

Permalink
Merge pull request #3 from alfasoftware/master
Browse files Browse the repository at this point in the history
Pull from master
  • Loading branch information
jonknight73 authored Mar 18, 2021
2 parents fe91a87 + d49862d commit 0dae5c4
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ![Alfa Morf](https://github.com/alfasoftware/morf/wiki/morf.png)

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Build Status](https://travis-ci.org/alfasoftware/morf.svg?branch=master)](https://travis-ci.org/alfasoftware/morf)
[![Build Status](https://travis-ci.com/alfasoftware/morf.svg?branch=master)](https://travis-ci.com/alfasoftware/morf)
[![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=org.alfasoftware%3Amorf-parent&metric=alert_status)](https://sonarcloud.io/dashboard/index/org.alfasoftware:morf-parent)

Morf is a library for cross-platform evolutionary relational database mechanics, database access and database imaging/cloning. It has been developed over many years at Alfa and is used to manage all of our RDBMS-backed applications.
Expand Down
2 changes: 1 addition & 1 deletion morf-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - Core</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.alfasoftware.morf.dataset.TableLoaderBuilder.TableLoaderBuilderImpl;
import org.alfasoftware.morf.jdbc.RuntimeSqlException;
Expand Down Expand Up @@ -89,6 +90,13 @@ public static TableLoaderBuilder builder() {
this.insertingUnderAutonumLimit = insertingUnderAutonumLimit;
this.truncateBeforeLoad = truncateBeforeLoad;
this.batchSize = batchSize;

if (truncateBeforeLoad && !explicitCommit) {
// This is because PostgreSQL needs to be able to commit/rollback, for TRUNCATE fallback to DELETE to work
// It could potentially be relaxed, if the DELETE fallback after a failed TRUNCATE is not needed.
// It also could potentially be relaxed, if running on PostgreSQL is of no concern to the caller.
throw new UnsupportedOperationException("Cannot TRUNCATE before load without permission to commit explicitly");
}
}


Expand All @@ -100,7 +108,7 @@ public static TableLoaderBuilder builder() {
public void load(final Iterable<Record> records) {

if (truncateBeforeLoad) {
truncate(table, connection);
truncate(table);
}

insertOrMergeRecords(records);
Expand All @@ -112,21 +120,50 @@ public void load(final Iterable<Record> records) {
*
* @param table
*/
private void truncate(Table table, Connection connection) {
private void truncate(Table table) {
// Get our own table definition based on the name to avoid case sensitivity bugs on some database vendors
log.debug("Clearing table [" + table.getName() + "]");

// Try to use a truncate
try {
sqlExecutor.execute(sqlDialect.truncateTableStatements(table), connection);
} catch (RuntimeSqlException e) {
runRecoverably(() ->
sqlExecutor.execute(sqlDialect.truncateTableStatements(table), connection)
);
} catch (SQLException | RuntimeSqlException e) {
// If that has failed try to use a delete
log.debug("Failed to truncate table, attempting a delete", e);
sqlExecutor.execute(sqlDialect.deleteAllFromTableStatements(table), connection);
}
}


/**
* PostgreSQL does not like failing commands, and marks connections as "dirty" after errors:
* <q>ERROR: current transaction is aborted, commands ignored until end of transaction block.</q>
*
* <p>To recover a connection, one has to issue a rollback.</p>
*
* @param runnable
*/
private void runRecoverably(Runnable runnable) throws SQLException {
// make sure we commit if we can
if (explicitCommit) {
connection.commit();
}

try {
runnable.run();
}
catch (Exception e) {
// make sure we rollback if we can
if (explicitCommit) {
connection.rollback();
}
throw e;
}
}


/**
* Insert the records into the database using a {@link PreparedStatement}.
*
Expand Down
2 changes: 1 addition & 1 deletion morf-excel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - Excel</name>
Expand Down
2 changes: 1 addition & 1 deletion morf-h2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - H2</name>
Expand Down
2 changes: 1 addition & 1 deletion morf-integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - Integration Test</name>
Expand Down
2 changes: 1 addition & 1 deletion morf-mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - MySQL</name>
Expand Down
2 changes: 1 addition & 1 deletion morf-nuodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - NuoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion morf-oracle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - Oracle</name>
Expand Down
2 changes: 1 addition & 1 deletion morf-postgresql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - PostgreSQL</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@ protected Collection<String> internalTableDeploymentStatements(Table table) {
return ImmutableList.<String>builder()
.addAll(preStatements)
.add(createTableStatement.toString())
.add(addTableComment(table.getName()))
.add(addTableComment(table))
.addAll(postStatements)
.build();
}


private String addTableComment(String tableName) {
return "COMMENT ON TABLE " + schemaNamePrefix() + tableName + " IS 'REALNAME:[" + tableName + "]'";
private String addTableComment(Table table) {
return "COMMENT ON TABLE " + schemaNamePrefix(table) + table.getName() + " IS 'REALNAME:[" + table.getName() + "]'";
}


Expand All @@ -234,7 +234,7 @@ public Collection<String> renameTableStatements(Table from, Table to) {
.addAll(renameTable)
.addAll(renamePk)
.addAll(renameSeq)
.add(addTableComment(to.getName()))
.add(addTableComment(to))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected List<String> expectedCreateTemporaryTableStatements() {
return Arrays
.asList(
"CREATE TEMP TABLE TempTest (id NUMERIC(19) NOT NULL, version INTEGER DEFAULT 0, stringField VARCHAR(3) COLLATE \"POSIX\", intField INTEGER, floatField DECIMAL(13,2) NOT NULL, dateField DATE, booleanField BOOLEAN, charField VARCHAR(1) COLLATE \"POSIX\", blobField BYTEA, bigIntegerField NUMERIC(19) DEFAULT 12345, clobField TEXT, CONSTRAINT TempTest_PK PRIMARY KEY(id))",
"COMMENT ON TABLE testschema.TempTest IS 'REALNAME:[TempTest]'",
"COMMENT ON TABLE TempTest IS 'REALNAME:[TempTest]'",
"COMMENT ON COLUMN TempTest.id IS 'REALNAME:[id]/TYPE:[BIG_INTEGER]'",
"COMMENT ON COLUMN TempTest.version IS 'REALNAME:[version]/TYPE:[INTEGER]'",
"COMMENT ON COLUMN TempTest.stringField IS 'REALNAME:[stringField]/TYPE:[STRING]'",
Expand All @@ -142,14 +142,14 @@ protected List<String> expectedCreateTemporaryTableStatements() {
"CREATE INDEX TempTest_1 ON TempTest (intField,floatField)",
"COMMENT ON INDEX TempTest_1 IS 'REALNAME:[TempTest_1]'",
"CREATE TEMP TABLE TempAlternate (id NUMERIC(19) NOT NULL, version INTEGER DEFAULT 0, stringField VARCHAR(3) COLLATE \"POSIX\", CONSTRAINT TempAlternate_PK PRIMARY KEY(id))",
"COMMENT ON TABLE testschema.TempAlternate IS 'REALNAME:[TempAlternate]'",
"COMMENT ON TABLE TempAlternate IS 'REALNAME:[TempAlternate]'",
"COMMENT ON COLUMN TempAlternate.id IS 'REALNAME:[id]/TYPE:[BIG_INTEGER]'",
"COMMENT ON COLUMN TempAlternate.version IS 'REALNAME:[version]/TYPE:[INTEGER]'",
"COMMENT ON COLUMN TempAlternate.stringField IS 'REALNAME:[stringField]/TYPE:[STRING]'",
"CREATE INDEX TempAlternate_1 ON TempAlternate (stringField)",
"COMMENT ON INDEX TempAlternate_1 IS 'REALNAME:[TempAlternate_1]'",
"CREATE TEMP TABLE TempNonNull (id NUMERIC(19) NOT NULL, version INTEGER DEFAULT 0, stringField VARCHAR(3) COLLATE \"POSIX\" NOT NULL, intField DECIMAL(8,0) NOT NULL, booleanField BOOLEAN NOT NULL, dateField DATE NOT NULL, blobField BYTEA NOT NULL, CONSTRAINT TempNonNull_PK PRIMARY KEY(id))",
"COMMENT ON TABLE testschema.TempNonNull IS 'REALNAME:[TempNonNull]'",
"COMMENT ON TABLE TempNonNull IS 'REALNAME:[TempNonNull]'",
"COMMENT ON COLUMN TempNonNull.id IS 'REALNAME:[id]/TYPE:[BIG_INTEGER]'",
"COMMENT ON COLUMN TempNonNull.version IS 'REALNAME:[version]/TYPE:[INTEGER]'",
"COMMENT ON COLUMN TempNonNull.stringField IS 'REALNAME:[stringField]/TYPE:[STRING]'",
Expand Down
2 changes: 1 addition & 1 deletion morf-sqlserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - SQL Server</name>
Expand Down
2 changes: 1 addition & 1 deletion morf-testsupport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
</parent>

<name>Morf - Test Support</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.alfasoftware</groupId>
<artifactId>morf-parent</artifactId>
<version>1.3.5-SNAPSHOT</version>
<version>1.3.7-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Morf</name>
Expand Down

0 comments on commit 0dae5c4

Please sign in to comment.