Skip to content

Commit

Permalink
HHH-10126 - Table-backed sequences are not populated on creation usin…
Browse files Browse the repository at this point in the history
…g SchemaUpdate

(cherry picked from commit 7354c7b)
  • Loading branch information
sebersole committed Sep 25, 2015
1 parent 1c9976d commit 9c2170a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
Expand Up @@ -285,7 +285,7 @@ public void registerExportables(Database database) {
);
table.addColumn( valueColumn );

database.addInitCommand(
table.addInitCommand(
new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
);
}
Expand Down
18 changes: 18 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/mapping/Table.java
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -60,6 +61,8 @@ public class Table implements RelationalModel, Serializable, Exportable {
private boolean hasDenormalizedTables;
private String comment;

private List<InitCommand> initCommands;

public Table() {
}

Expand Down Expand Up @@ -857,4 +860,19 @@ public String toString() {
}
}

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 );
}
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -143,6 +145,8 @@ public String[] getSqlCreateStrings(Table table, Metadata metadata) {

applyComments( table, sqlStrings );

applyInitCommands( table, sqlStrings );

return sqlStrings.toArray( new String[ sqlStrings.size() ] );
}

Expand All @@ -152,6 +156,12 @@ protected void applyComments(Table table, List<String> sqlStrings) {
}
}

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() );
}
Expand Down
@@ -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 );
}
}

0 comments on commit 9c2170a

Please sign in to comment.