HHH-10158: SchemaUpdate does not properly support formatter and delimiter anymore
This commit is contained in:
parent
3229bbd883
commit
3d92590eb3
|
@ -27,6 +27,8 @@ import org.hibernate.boot.spi.MetadataImplementor;
|
|||
import org.hibernate.engine.config.spi.ConfigurationService;
|
||||
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
|
||||
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
||||
import org.hibernate.engine.jdbc.internal.Formatter;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.internal.CoreLogging;
|
||||
import org.hibernate.internal.CoreMessageLogger;
|
||||
|
@ -56,6 +58,7 @@ public class SchemaUpdate {
|
|||
private final List<Exception> exceptions = new ArrayList<Exception>();
|
||||
private String outputFile;
|
||||
private String delimiter;
|
||||
private Formatter formatter;
|
||||
|
||||
/**
|
||||
* Creates a SchemaUpdate object. This form is intended for use from tooling
|
||||
|
@ -134,7 +137,7 @@ public class SchemaUpdate {
|
|||
List<org.hibernate.tool.schema.spi.Target> toolTargets = new ArrayList<org.hibernate.tool.schema.spi.Target>();
|
||||
|
||||
if ( target.doScript() ) {
|
||||
toolTargets.add( new TargetStdoutImpl( delimiter ) );
|
||||
toolTargets.add( new TargetStdoutImpl( delimiter, formatter ) );
|
||||
}
|
||||
|
||||
if ( target.doExport() ) {
|
||||
|
@ -143,7 +146,7 @@ public class SchemaUpdate {
|
|||
|
||||
if ( outputFile != null ) {
|
||||
LOG.writingGeneratedSchemaToFile( outputFile );
|
||||
toolTargets.add( new TargetFileImpl( outputFile, delimiter ) );
|
||||
toolTargets.add( new TargetFileImpl( outputFile, delimiter, formatter ) );
|
||||
}
|
||||
|
||||
return toolTargets;
|
||||
|
@ -162,6 +165,7 @@ public class SchemaUpdate {
|
|||
}
|
||||
|
||||
public void setFormat(boolean format) {
|
||||
formatter = (format ? FormatStyle.DDL : FormatStyle.NONE).getFormatter();
|
||||
}
|
||||
|
||||
public void setOutputFile(String outputFile) {
|
||||
|
|
|
@ -9,6 +9,8 @@ package org.hibernate.tool.schema.internal;
|
|||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
||||
import org.hibernate.engine.jdbc.internal.Formatter;
|
||||
import org.hibernate.tool.schema.spi.SchemaManagementException;
|
||||
import org.hibernate.tool.schema.spi.Target;
|
||||
|
||||
|
@ -18,11 +20,17 @@ import org.hibernate.tool.schema.spi.Target;
|
|||
public class TargetFileImpl implements Target {
|
||||
final private FileWriter fileWriter;
|
||||
final private String delimiter;
|
||||
final private Formatter formatter;
|
||||
|
||||
public TargetFileImpl(String outputFile, String delimter) {
|
||||
public TargetFileImpl(String outputFile, String delimiter) {
|
||||
this( outputFile, delimiter, FormatStyle.NONE.getFormatter());
|
||||
}
|
||||
|
||||
public TargetFileImpl(String outputFile, String delimiter, Formatter formatter) {
|
||||
try {
|
||||
this.delimiter = delimter;
|
||||
this.delimiter = delimiter;
|
||||
this.fileWriter = new FileWriter( outputFile );
|
||||
this.formatter = formatter;
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new SchemaManagementException( "Unable to open FileWriter [" + outputFile + "]", e );
|
||||
|
@ -41,6 +49,9 @@ public class TargetFileImpl implements Target {
|
|||
@Override
|
||||
public void accept(String action) {
|
||||
try {
|
||||
if (formatter != null) {
|
||||
action = formatter.format(action);
|
||||
}
|
||||
fileWriter.write( action );
|
||||
if ( delimiter != null ) {
|
||||
fileWriter.write( delimiter );
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
*/
|
||||
package org.hibernate.tool.schema.internal;
|
||||
|
||||
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
||||
import org.hibernate.engine.jdbc.internal.Formatter;
|
||||
import org.hibernate.tool.schema.spi.Target;
|
||||
|
||||
/**
|
||||
|
@ -13,12 +15,18 @@ import org.hibernate.tool.schema.spi.Target;
|
|||
*/
|
||||
public class TargetStdoutImpl implements Target {
|
||||
final private String delimiter;
|
||||
final private Formatter formatter;
|
||||
|
||||
public TargetStdoutImpl() {
|
||||
this( null );
|
||||
}
|
||||
|
||||
public TargetStdoutImpl(String delimiter) {
|
||||
this( delimiter, FormatStyle.NONE.getFormatter());
|
||||
}
|
||||
|
||||
public TargetStdoutImpl(String delimiter, Formatter formatter) {
|
||||
this.formatter = formatter;
|
||||
this.delimiter = delimiter;
|
||||
}
|
||||
|
||||
|
@ -33,6 +41,9 @@ public class TargetStdoutImpl implements Target {
|
|||
|
||||
@Override
|
||||
public void accept(String action) {
|
||||
if (formatter != null) {
|
||||
action = formatter.format(action);
|
||||
}
|
||||
if ( delimiter != null ) {
|
||||
action += delimiter;
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ public class SchemaUpdateDelimiterTest {
|
|||
su.setHaltOnError( true );
|
||||
su.setOutputFile( output.getAbsolutePath() );
|
||||
su.setDelimiter( EXPECTED_DELIMITER );
|
||||
su.setFormat( true );
|
||||
su.setFormat( false );
|
||||
su.execute( Target.SCRIPT );
|
||||
|
||||
List<String> sqlLines = Files.readAllLines( output.toPath() );
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
package org.hibernate.test.schemaupdate;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.boot.spi.MetadataImplementor;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
|
||||
import org.hibernate.tool.hbm2ddl.Target;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Koen Aers
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-10158")
|
||||
public class SchemaUpdateFormatterTest {
|
||||
|
||||
private static final String AFTER_FORMAT =
|
||||
System.lineSeparator() +
|
||||
" create table test_entity (" + System.lineSeparator() +
|
||||
" field varchar(255) not null," + System.lineSeparator() +
|
||||
" primary key (field)" + System.lineSeparator() +
|
||||
" );" + System.lineSeparator();
|
||||
private static final String DELIMITER = ";";
|
||||
|
||||
@Test
|
||||
public void testSetFormat() throws Exception {
|
||||
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
|
||||
.applySetting( Environment.HBM2DDL_AUTO, "none" )
|
||||
.build();
|
||||
try {
|
||||
File output = File.createTempFile( "update_script", ".sql" );
|
||||
output.deleteOnExit();
|
||||
|
||||
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
|
||||
.addAnnotatedClass( TestEntity.class )
|
||||
.buildMetadata();
|
||||
metadata.validate();
|
||||
|
||||
SchemaUpdate su = new SchemaUpdate( ssr, metadata );
|
||||
su.setHaltOnError( true );
|
||||
su.setOutputFile( output.getAbsolutePath() );
|
||||
su.setDelimiter( DELIMITER );
|
||||
su.setFormat( true );
|
||||
su.execute( Target.SCRIPT );
|
||||
|
||||
Assert.assertEquals(
|
||||
AFTER_FORMAT,
|
||||
new String(Files.readAllBytes(output.toPath())));
|
||||
}
|
||||
finally {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "test_entity")
|
||||
public static class TestEntity {
|
||||
@Id
|
||||
private String field;
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public void setField(String field) {
|
||||
this.field = field;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.hibernate.test.tool.schema;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
||||
import org.hibernate.engine.jdbc.internal.Formatter;
|
||||
import org.hibernate.tool.schema.internal.TargetFileImpl;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Koen Aers
|
||||
*/
|
||||
public class TargetFileImplTest {
|
||||
|
||||
private static final Formatter FORMATTER = FormatStyle.DDL.getFormatter();
|
||||
private static final String BEFORE_FORMAT =
|
||||
"create table test_entity (field varchar(255) not null, primary key (field))";
|
||||
private static final String AFTER_FORMAT = FORMATTER.format(BEFORE_FORMAT);
|
||||
private static final String DELIMITER = "---";
|
||||
|
||||
private File file = null;
|
||||
private String path = null;
|
||||
private TargetFileImpl target = null;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
file = File.createTempFile("temp", "sql");
|
||||
file.deleteOnExit();
|
||||
path = file.getAbsolutePath();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptWithFormatter() throws Exception {
|
||||
target = new TargetFileImpl(path, DELIMITER, FORMATTER);
|
||||
target.prepare();
|
||||
target.accept(BEFORE_FORMAT);
|
||||
target.release();
|
||||
Assert.assertEquals(
|
||||
AFTER_FORMAT + DELIMITER + System.lineSeparator(),
|
||||
new String(Files.readAllBytes(file.toPath())));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package org.hibernate.test.tool.schema;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
||||
import org.hibernate.engine.jdbc.internal.Formatter;
|
||||
import org.hibernate.tool.schema.internal.TargetStdoutImpl;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Koen Aers
|
||||
*/
|
||||
public class TargetStdoutImplTest {
|
||||
|
||||
private static final Formatter FORMATTER = FormatStyle.DDL.getFormatter();
|
||||
private static final String BEFORE_FORMAT =
|
||||
"create table test_entity (field varchar(255) not null, primary key (field))";
|
||||
private static final String AFTER_FORMAT = FORMATTER.format(BEFORE_FORMAT);
|
||||
private static final String DELIMITER = "---";
|
||||
|
||||
private PrintStream savedSystemOut = null;
|
||||
private ByteArrayOutputStream baos = null;
|
||||
private TargetStdoutImpl target = null;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
savedSystemOut = System.out;
|
||||
baos = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(baos));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.setOut(savedSystemOut);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAcceptWithFormatter() {
|
||||
target = new TargetStdoutImpl(DELIMITER, FORMATTER);
|
||||
target.accept(BEFORE_FORMAT);
|
||||
Assert.assertEquals(
|
||||
AFTER_FORMAT + DELIMITER + System.lineSeparator(),
|
||||
baos.toString());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue