Skip to content

Commit 9c2170a

Browse files
committed
HHH-10126 - Table-backed sequences are not populated on creation using SchemaUpdate
(cherry picked from commit 7354c7b)
1 parent 1c9976d commit 9c2170a

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

hibernate-core/src/main/java/org/hibernate/id/enhanced/TableStructure.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public void registerExportables(Database database) {
285285
);
286286
table.addColumn( valueColumn );
287287

288-
database.addInitCommand(
288+
table.addInitCommand(
289289
new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
290290
);
291291
}

hibernate-core/src/main/java/org/hibernate/mapping/Table.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.hibernate.MappingException;
2020
import org.hibernate.boot.model.naming.Identifier;
2121
import org.hibernate.boot.model.relational.Exportable;
22+
import org.hibernate.boot.model.relational.InitCommand;
2223
import org.hibernate.boot.model.relational.Namespace;
2324
import org.hibernate.boot.model.relational.QualifiedTableName;
2425
import org.hibernate.dialect.Dialect;
@@ -60,6 +61,8 @@ public class Table implements RelationalModel, Serializable, Exportable {
6061
private boolean hasDenormalizedTables;
6162
private String comment;
6263

64+
private List<InitCommand> initCommands;
65+
6366
public Table() {
6467
}
6568

@@ -857,4 +860,19 @@ public String toString() {
857860
}
858861
}
859862

863+
public void addInitCommand(InitCommand command) {
864+
if ( initCommands == null ) {
865+
initCommands = new ArrayList<InitCommand>();
866+
}
867+
initCommands.add( command );
868+
}
869+
870+
public List<InitCommand> getInitCommands() {
871+
if ( initCommands == null ) {
872+
return Collections.emptyList();
873+
}
874+
else {
875+
return Collections.unmodifiableList( initCommands );
876+
}
877+
}
860878
}

hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
package org.hibernate.tool.schema.internal;
88

99
import java.util.ArrayList;
10+
import java.util.Collections;
1011
import java.util.Iterator;
1112
import java.util.List;
1213

1314
import org.hibernate.boot.Metadata;
1415
import org.hibernate.boot.model.naming.Identifier;
16+
import org.hibernate.boot.model.relational.InitCommand;
1517
import org.hibernate.boot.model.relational.QualifiedName;
1618
import org.hibernate.boot.model.relational.QualifiedNameParser;
1719
import org.hibernate.dialect.Dialect;
@@ -143,6 +145,8 @@ public String[] getSqlCreateStrings(Table table, Metadata metadata) {
143145

144146
applyComments( table, sqlStrings );
145147

148+
applyInitCommands( table, sqlStrings );
149+
146150
return sqlStrings.toArray( new String[ sqlStrings.size() ] );
147151
}
148152

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

159+
protected void applyInitCommands(Table table, List<String> sqlStrings) {
160+
for ( InitCommand initCommand : table.getInitCommands() ) {
161+
Collections.addAll( sqlStrings, initCommand.getInitCommands() );
162+
}
163+
}
164+
155165
protected void applyTableTypeString(StringBuilder buf) {
156166
buf.append( dialect.getTableTypeString() );
157167
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate;
8+
9+
import java.sql.SQLException;
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
13+
import org.hibernate.boot.Metadata;
14+
import org.hibernate.boot.MetadataSources;
15+
import org.hibernate.boot.model.naming.Identifier;
16+
import org.hibernate.boot.model.relational.Database;
17+
import org.hibernate.boot.model.relational.QualifiedTableName;
18+
import org.hibernate.boot.registry.StandardServiceRegistry;
19+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
20+
import org.hibernate.engine.jdbc.spi.JdbcServices;
21+
import org.hibernate.id.enhanced.TableStructure;
22+
import org.hibernate.mapping.Table;
23+
import org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl;
24+
import org.hibernate.tool.schema.extract.spi.DatabaseInformation;
25+
import org.hibernate.tool.schema.internal.TargetDatabaseImpl;
26+
import org.hibernate.tool.schema.internal.TargetStdoutImpl;
27+
import org.hibernate.tool.schema.spi.SchemaManagementTool;
28+
import org.hibernate.tool.schema.spi.Target;
29+
30+
import org.hibernate.testing.junit4.BaseUnitTestCase;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
35+
import static org.junit.Assert.assertEquals;
36+
import static org.junit.Assert.assertTrue;
37+
38+
/**
39+
* @author Steve Ebersole
40+
*/
41+
public class SchemaUpdateTableBackedSequenceTest extends BaseUnitTestCase {
42+
private StandardServiceRegistry ssr;
43+
44+
@Before
45+
public void before() {
46+
ssr = new StandardServiceRegistryBuilder().build();
47+
}
48+
49+
@After
50+
public void after() {
51+
StandardServiceRegistryBuilder.destroy( ssr );
52+
}
53+
54+
@Test
55+
public void testCreateTableOnUpdate() throws SQLException {
56+
Metadata metadata = new MetadataSources( ssr ).buildMetadata();
57+
58+
Database database = metadata.getDatabase();
59+
60+
TableStructure tableStructure = new TableStructure(
61+
database.getJdbcEnvironment(),
62+
new QualifiedTableName( null, null, Identifier.toIdentifier( "test_seq" ) ),
63+
Identifier.toIdentifier( "nextval" ),
64+
20,
65+
30,
66+
Long.class
67+
);
68+
tableStructure.registerExportables( database );
69+
70+
// lets make sure the InitCommand is there
71+
assertEquals( 1, database.getDefaultNamespace().getTables().size() );
72+
Table table = database.getDefaultNamespace().getTables().iterator().next();
73+
assertEquals( 1, table.getInitCommands().size() );
74+
75+
76+
class TargetImpl extends TargetStdoutImpl {
77+
boolean found = false;
78+
@Override
79+
public void accept(String action) {
80+
super.accept( action );
81+
if ( action.startsWith( "insert into test_seq" ) ) {
82+
found = true;
83+
}
84+
}
85+
}
86+
87+
TargetImpl target = new TargetImpl();
88+
89+
DatabaseInformation dbInfo = new DatabaseInformationImpl(
90+
ssr,
91+
database.getJdbcEnvironment(),
92+
ssr.getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess(),
93+
database.getDefaultNamespace().getPhysicalName().getCatalog(),
94+
database.getDefaultNamespace().getPhysicalName().getSchema()
95+
);
96+
97+
ssr.getService( SchemaManagementTool.class ).getSchemaMigrator( Collections.emptyMap() ).doMigration(
98+
metadata,
99+
dbInfo,
100+
true,
101+
Arrays.asList( target, new TargetDatabaseImpl( ssr.getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess() ) )
102+
);
103+
104+
assertTrue( target.found );
105+
}
106+
}

0 commit comments

Comments
 (0)