|
| 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