improve error reporting for DDL execution

This commit is contained in:
Gavin 2022-12-18 12:56:19 +01:00 committed by Gavin King
parent d49b568d7b
commit ff9a60601c
5 changed files with 26 additions and 18 deletions

View File

@ -6,18 +6,21 @@
*/
package org.hibernate.tool.schema.internal.exec;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import org.hibernate.engine.jdbc.internal.DDLFormatterImpl;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
import org.hibernate.tool.schema.spi.CommandAcceptanceException;
/**
* GenerationTarget implementation for handling generation directly to the database
* A {@link GenerationTarget} which exports DDL directly to the database.
*
* @author Steve Ebersole
*/
@ -28,7 +31,7 @@ public class GenerationTargetToDatabase implements GenerationTarget {
private final boolean releaseAfterUse;
private Statement jdbcStatement;
private boolean autocommit;
private final boolean autocommit;
public GenerationTargetToDatabase(DdlTransactionIsolator ddlTransactionIsolator) {
this( ddlTransactionIsolator, true );
@ -44,16 +47,25 @@ public class GenerationTargetToDatabase implements GenerationTarget {
this.autocommit = autocommit;
}
private SqlStatementLogger getSqlStatementLogger() {
return ddlTransactionIsolator.getJdbcContext().getSqlStatementLogger();
}
private SqlExceptionHelper getSqlExceptionHelper() {
return ddlTransactionIsolator.getJdbcContext().getSqlExceptionHelper();
}
private Connection getIsolatedConnection() {
return ddlTransactionIsolator.getIsolatedConnection( autocommit );
}
@Override
public void prepare() {
}
@Override
public void accept(String command) {
ddlTransactionIsolator.getJdbcContext().getSqlStatementLogger().logStatement(
command,
DDLFormatterImpl.INSTANCE
);
getSqlStatementLogger().logStatement( command, DDLFormatterImpl.INSTANCE );
try {
final Statement jdbcStatement = jdbcStatement();
@ -62,7 +74,7 @@ public class GenerationTargetToDatabase implements GenerationTarget {
try {
SQLWarning warnings = jdbcStatement.getWarnings();
if ( warnings != null) {
ddlTransactionIsolator.getJdbcContext().getSqlExceptionHelper().logAndClearWarnings( jdbcStatement );
getSqlExceptionHelper().logAndClearWarnings( jdbcStatement );
}
}
catch( SQLException e ) {
@ -71,7 +83,7 @@ public class GenerationTargetToDatabase implements GenerationTarget {
}
catch (SQLException e) {
throw new CommandAcceptanceException(
"Error executing DDL \"" + command + "\" via JDBC Statement",
"Error executing DDL \"" + command + "\" via JDBC [" + e.getMessage() + "]",
e
);
}
@ -80,10 +92,10 @@ public class GenerationTargetToDatabase implements GenerationTarget {
private Statement jdbcStatement() {
if ( jdbcStatement == null ) {
try {
jdbcStatement = ddlTransactionIsolator.getIsolatedConnection( autocommit ).createStatement();
jdbcStatement = getIsolatedConnection().createStatement();
}
catch (SQLException e) {
throw ddlTransactionIsolator.getJdbcContext().getSqlExceptionHelper().convert( e, "Unable to create JDBC Statement for DDL execution" );
throw getSqlExceptionHelper().convert( e, "Unable to create JDBC Statement for DDL execution" );
}
}
@ -98,7 +110,7 @@ public class GenerationTargetToDatabase implements GenerationTarget {
jdbcStatement = null;
}
catch (SQLException e) {
throw ddlTransactionIsolator.getJdbcContext().getSqlExceptionHelper().convert( e, "Unable to close JDBC Statement after DDL execution" );
throw getSqlExceptionHelper().convert( e, "Unable to close JDBC Statement after DDL execution" );
}
}
if ( releaseAfterUse ) {

View File

@ -10,7 +10,7 @@ import org.hibernate.tool.schema.spi.SchemaManagementException;
import org.hibernate.tool.schema.spi.ScriptTargetOutput;
/**
* GenerationTarget implementation for handling generation to scripts
* A {@link GenerationTarget} that writes DDL to scripts.
*
* @author Steve Ebersole
*/

View File

@ -9,7 +9,7 @@ package org.hibernate.tool.schema.internal.exec;
import org.hibernate.internal.build.AllowSysOut;
/**
* GenerationTarget implementation for handling generation to System.out
* A {@link GenerationTarget} that writed DDL to {@link System#out}.
*
* @author Steve Ebersole
*/

View File

@ -55,7 +55,6 @@ public class SchemaMigratorHaltOnErrorTest extends BaseEntityManagerFunctionalTe
catch ( Exception e ) {
SchemaManagementException cause = (SchemaManagementException) e.getCause();
assertTrue( cause.getMessage().startsWith( "Halting on error : Error executing DDL" ) );
assertTrue( cause.getMessage().endsWith( "via JDBC Statement" ) );
}
}

View File

@ -44,13 +44,12 @@ import org.junit.runner.RunWith;
@RunWith(CustomRunner.class)
public class SchemaUpdateHaltOnErrorTest {
private File output;
private StandardServiceRegistry ssr;
private MetadataImplementor metadata;
@Before
public void setUp() throws IOException {
output = File.createTempFile( "update_script", ".sql" );
File output = File.createTempFile("update_script", ".sql");
output.deleteOnExit();
ssr = new StandardServiceRegistryBuilder().build();
@ -74,9 +73,7 @@ public class SchemaUpdateHaltOnErrorTest {
}
catch ( Exception e ) {
SchemaManagementException cause = (SchemaManagementException) e;
assertTrue( cause.getMessage().startsWith( "Halting on error : Error executing DDL" ) );
assertTrue( cause.getMessage().endsWith( "via JDBC Statement" ) );
}
}