HHH-10126 - Table-backed sequences are not populated on creation using SchemaUpdate
This commit is contained in:
parent
ed892971af
commit
7354c7bfc3
|
@ -285,7 +285,7 @@ public class TableStructure implements DatabaseStructure {
|
|||
);
|
||||
table.addColumn( valueColumn );
|
||||
|
||||
database.addInitCommand(
|
||||
table.addInitCommand(
|
||||
new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
|
||||
);
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import org.hibernate.HibernateException;
|
|||
import org.hibernate.MappingException;
|
||||
import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.relational.Exportable;
|
||||
import org.hibernate.boot.model.relational.InitCommand;
|
||||
import org.hibernate.boot.model.relational.Namespace;
|
||||
import org.hibernate.boot.model.relational.QualifiedTableName;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
|
@ -60,6 +61,8 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
private boolean hasDenormalizedTables;
|
||||
private String comment;
|
||||
|
||||
private List<InitCommand> initCommands;
|
||||
|
||||
public Table() {
|
||||
}
|
||||
|
||||
|
@ -857,4 +860,19 @@ public class Table implements RelationalModel, Serializable, Exportable {
|
|||
}
|
||||
}
|
||||
|
||||
public void addInitCommand(InitCommand command) {
|
||||
if ( initCommands == null ) {
|
||||
initCommands = new ArrayList<InitCommand>();
|
||||
}
|
||||
initCommands.add( command );
|
||||
}
|
||||
|
||||
public List<InitCommand> getInitCommands() {
|
||||
if ( initCommands == null ) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
else {
|
||||
return Collections.unmodifiableList( initCommands );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,11 +7,13 @@
|
|||
package org.hibernate.tool.schema.internal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.relational.InitCommand;
|
||||
import org.hibernate.boot.model.relational.QualifiedName;
|
||||
import org.hibernate.boot.model.relational.QualifiedNameParser;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
|
@ -143,6 +145,8 @@ public class StandardTableExporter implements Exporter<Table> {
|
|||
|
||||
applyComments( table, sqlStrings );
|
||||
|
||||
applyInitCommands( table, sqlStrings );
|
||||
|
||||
return sqlStrings.toArray( new String[ sqlStrings.size() ] );
|
||||
}
|
||||
|
||||
|
@ -152,6 +156,12 @@ public class StandardTableExporter implements Exporter<Table> {
|
|||
}
|
||||
}
|
||||
|
||||
protected void applyInitCommands(Table table, List<String> sqlStrings) {
|
||||
for ( InitCommand initCommand : table.getInitCommands() ) {
|
||||
Collections.addAll( sqlStrings, initCommand.getInitCommands() );
|
||||
}
|
||||
}
|
||||
|
||||
protected void applyTableTypeString(StringBuilder buf) {
|
||||
buf.append( dialect.getTableTypeString() );
|
||||
}
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.schemaupdate;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.model.naming.Identifier;
|
||||
import org.hibernate.boot.model.relational.Database;
|
||||
import org.hibernate.boot.model.relational.QualifiedTableName;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.id.enhanced.TableStructure;
|
||||
import org.hibernate.mapping.Table;
|
||||
import org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl;
|
||||
import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
|
||||
import org.hibernate.tool.schema.internal.TargetDatabaseImpl;
|
||||
import org.hibernate.tool.schema.internal.TargetStdoutImpl;
|
||||
import org.hibernate.tool.schema.spi.SchemaManagementTool;
|
||||
import org.hibernate.tool.schema.spi.Target;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class SchemaUpdateTableBackedSequenceTest extends BaseUnitTestCase {
|
||||
private StandardServiceRegistry ssr;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
ssr = new StandardServiceRegistryBuilder().build();
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
StandardServiceRegistryBuilder.destroy( ssr );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTableOnUpdate() throws SQLException {
|
||||
Metadata metadata = new MetadataSources( ssr ).buildMetadata();
|
||||
|
||||
Database database = metadata.getDatabase();
|
||||
|
||||
TableStructure tableStructure = new TableStructure(
|
||||
database.getJdbcEnvironment(),
|
||||
new QualifiedTableName( null, null, Identifier.toIdentifier( "test_seq" ) ),
|
||||
Identifier.toIdentifier( "nextval" ),
|
||||
20,
|
||||
30,
|
||||
Long.class
|
||||
);
|
||||
tableStructure.registerExportables( database );
|
||||
|
||||
// lets make sure the InitCommand is there
|
||||
assertEquals( 1, database.getDefaultNamespace().getTables().size() );
|
||||
Table table = database.getDefaultNamespace().getTables().iterator().next();
|
||||
assertEquals( 1, table.getInitCommands().size() );
|
||||
|
||||
|
||||
class TargetImpl extends TargetStdoutImpl {
|
||||
boolean found = false;
|
||||
@Override
|
||||
public void accept(String action) {
|
||||
super.accept( action );
|
||||
if ( action.startsWith( "insert into test_seq" ) ) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TargetImpl target = new TargetImpl();
|
||||
|
||||
DatabaseInformation dbInfo = new DatabaseInformationImpl(
|
||||
ssr,
|
||||
database.getJdbcEnvironment(),
|
||||
ssr.getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess(),
|
||||
database.getDefaultNamespace().getPhysicalName().getCatalog(),
|
||||
database.getDefaultNamespace().getPhysicalName().getSchema()
|
||||
);
|
||||
|
||||
ssr.getService( SchemaManagementTool.class ).getSchemaMigrator( Collections.emptyMap() ).doMigration(
|
||||
metadata,
|
||||
dbInfo,
|
||||
true,
|
||||
Arrays.asList( target, new TargetDatabaseImpl( ssr.getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess() ) )
|
||||
);
|
||||
|
||||
assertTrue( target.found );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue