Search in sources :

Example 81 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project bw-calendar-engine by Bedework.

the class SchemaBuilderImpl method execute.

@Override
public void execute(final Properties props, final String outputFile, final boolean export, final String delimiter) throws CalFacadeException {
    try {
        SchemaExport se = new SchemaExport();
        if (delimiter != null) {
            se.setDelimiter(delimiter);
        }
        se.setFormat(true);
        se.setHaltOnError(false);
        se.setOutputFile(outputFile);
        final EnumSet<TargetType> targets = EnumSet.noneOf(TargetType.class);
        if (export) {
            targets.add(TargetType.DATABASE);
        } else {
            targets.add(TargetType.SCRIPT);
        }
        Properties allProps = getConfiguration(props).getProperties();
        final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder(bsr);
        ssrBuilder.applySettings(allProps);
        se.execute(targets, SchemaExport.Action.BOTH, null, ssrBuilder.getBootstrapServiceRegistry());
    } catch (Throwable t) {
        throw new CalFacadeException(t);
    }
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) TargetType(org.hibernate.tool.schema.TargetType) Properties(java.util.Properties) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Example 82 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class MigrationTest method testIndexCreationViaSchemaUpdate.

// /**
// * 3_Version.hbm.xml contains a named unique constraint and an un-named
// * unique constraint (will receive a randomly-generated name).  Create
// * the original schema with 2_Version.hbm.xml.  Then, run SchemaUpdate
// * TWICE using 3_Version.hbm.xml.  Neither RECREATE_QUIETLY nor SKIP should
// * generate any exceptions.
// */
// @Test
// @TestForIssue( jiraKey = "HHH-8162" )
// public void testConstraintUpdate() {
// doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy.DROP_RECREATE_QUIETLY);
// doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy.RECREATE_QUIETLY);
// doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy.SKIP);
// }
// 
// private void doConstraintUpdate(UniqueConstraintSchemaUpdateStrategy strategy) {
// // original
// String resource1 = "org/hibernate/test/schemaupdate/2_Version.hbm.xml";
// // adds unique constraint
// String resource2 = "org/hibernate/test/schemaupdate/3_Version.hbm.xml";
// 
// MetadataImplementor v1metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
// .addResource( resource1 )
// .buildMetadata();
// MetadataImplementor v2metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
// .addResource( resource2 )
// .buildMetadata();
// 
// new SchemaExport( v1metadata ).execute( false, true, true, false );
// 
// // adds unique constraint
// Configuration v2cfg = new Configuration();
// v2cfg.getProperties().put( AvailableSettings.UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY, strategy );
// v2cfg.addResource( resource2 );
// SchemaUpdate v2schemaUpdate = new SchemaUpdate( serviceRegistry, v2cfg );
// v2schemaUpdate.execute( true, true );
// assertEquals( 0, v2schemaUpdate.getExceptions().size() );
// 
// Configuration v3cfg = new Configuration();
// v3cfg.getProperties().put( AvailableSettings.UNIQUE_CONSTRAINT_SCHEMA_UPDATE_STRATEGY, strategy );
// v3cfg.addResource( resource2 );
// SchemaUpdate v3schemaUpdate = new SchemaUpdate( serviceRegistry, v3cfg );
// v3schemaUpdate.execute( true, true );
// assertEquals( 0, v3schemaUpdate.getExceptions().size() );
// 
// new SchemaExport( serviceRegistry, v3cfg ).drop( false, true );
// }
@Test
@TestForIssue(jiraKey = "HHH-9713")
public void testIndexCreationViaSchemaUpdate() {
    MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(serviceRegistry).addAnnotatedClass(EntityWithIndex.class).buildMetadata();
    // drop and then create the schema
    new SchemaExport().execute(EnumSet.of(TargetType.DATABASE), SchemaExport.Action.BOTH, metadata);
    try {
        // update the schema
        new SchemaUpdate().execute(EnumSet.of(TargetType.DATABASE), metadata);
    } finally {
        // drop the schema
        new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) SchemaUpdate(org.hibernate.tool.hbm2ddl.SchemaUpdate) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 83 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class PostgreSQLMultipleSchemaSequenceTest method test.

@Test
@TestForIssue(jiraKey = "HHH-5538")
public void test() {
    StandardServiceRegistry ssr1 = new StandardServiceRegistryBuilder().build();
    final String extraSchemaName = "extra_schema_sequence_validation";
    try {
        final MetadataImplementor metadata1 = (MetadataImplementor) new MetadataSources(ssr1).addAnnotatedClass(Box.class).buildMetadata();
        try {
            new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata1);
            final ConnectionProvider connectionProvider1 = ssr1.getService(ConnectionProvider.class);
            DdlTransactionIsolatorTestingImpl ddlTransactionIsolator1 = new DdlTransactionIsolatorTestingImpl(ssr1, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider1));
            try (Statement statement = ddlTransactionIsolator1.getIsolatedConnection().createStatement()) {
                statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
                statement.execute(String.format("CREATE SCHEMA %s", extraSchemaName));
                try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
                    while (resultSet.next()) {
                        Long sequenceValue = resultSet.getLong(1);
                        assertEquals(Long.valueOf(1L), sequenceValue);
                    }
                }
            } catch (SQLException e) {
                fail(e.getMessage());
            }
            StandardServiceRegistry ssr2 = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.URL, Environment.getProperties().get(AvailableSettings.URL) + "?currentSchema=" + extraSchemaName).build();
            try {
                final MetadataImplementor metadata2 = (MetadataImplementor) new MetadataSources(ssr2).addAnnotatedClass(Box.class).buildMetadata();
                try {
                    new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata2);
                } finally {
                    final ConnectionProvider connectionProvider2 = ssr2.getService(ConnectionProvider.class);
                    DdlTransactionIsolatorTestingImpl ddlTransactionIsolator2 = new DdlTransactionIsolatorTestingImpl(ssr2, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider2));
                    try (Statement statement = ddlTransactionIsolator2.getIsolatedConnection().createStatement()) {
                        try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
                            while (resultSet.next()) {
                                Long sequenceValue = resultSet.getLong(1);
                                assertEquals(Long.valueOf(1L), sequenceValue);
                            }
                        }
                        statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
                    } catch (SQLException e) {
                        fail(e.getMessage());
                    }
                    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata2);
                }
            } finally {
                StandardServiceRegistryBuilder.destroy(ssr2);
            }
        } finally {
            // clean up
            new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata1);
        }
        final List<String> sqlLines = Files.readAllLines(output.toPath(), Charset.defaultCharset());
        assertEquals(2, sqlLines.stream().filter(s -> s.equalsIgnoreCase("create sequence SEQ_TEST start 1 increment 1")).count());
    } catch (IOException e) {
        fail(e.getMessage());
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr1);
    }
}
Also used : JdbcEnvironmentInitiator(org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SQLException(java.sql.SQLException) Statement(java.sql.Statement) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) IOException(java.io.IOException) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) ResultSet(java.sql.ResultSet) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 84 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class SchemaExportTest method testCreateAndDrop.

@Test
public void testCreateAndDrop() {
    final SchemaExport schemaExport = new SchemaExport();
    // should drop before creating, but tables don't exist yet
    schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
    if (doesDialectSupportDropTableIfExist()) {
        assertEquals(0, schemaExport.getExceptions().size());
    } else {
        assertEquals(1, schemaExport.getExceptions().size());
    }
    // call create again; it should drop tables before re-creating
    schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
    assertEquals(0, schemaExport.getExceptions().size());
    // drop the tables
    schemaExport.drop(EnumSet.of(TargetType.DATABASE), metadata);
    assertEquals(0, schemaExport.getExceptions().size());
}
Also used : SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Example 85 with SchemaExport

use of org.hibernate.tool.hbm2ddl.SchemaExport in project hibernate-orm by hibernate.

the class SchemaExportTest method testGenerateDdlToFile.

@Test
public void testGenerateDdlToFile() {
    final SchemaExport schemaExport = new SchemaExport();
    java.io.File outFile = new java.io.File("schema.ddl");
    schemaExport.setOutputFile(outFile.getPath());
    // do not script to console or export to database
    schemaExport.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.DROP, metadata);
    if (doesDialectSupportDropTableIfExist() && schemaExport.getExceptions().size() > 0) {
        assertEquals(2, schemaExport.getExceptions().size());
    }
    assertTrue(outFile.exists());
    // check file is not empty
    assertTrue(outFile.length() > 0);
    outFile.delete();
}
Also used : File(java.io.File) File(java.io.File) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Aggregations

SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)90 Test (org.junit.Test)45 MetadataSources (org.hibernate.boot.MetadataSources)42 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)28 ISchemaExport (org.jboss.tools.hibernate.runtime.spi.ISchemaExport)27 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)22 TestForIssue (org.hibernate.testing.TestForIssue)13 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)12 Configuration (org.hibernate.cfg.Configuration)12 Connection (java.sql.Connection)10 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)10 After (org.junit.After)10 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)9 File (java.io.File)8 Before (org.junit.Before)8 HashMap (java.util.HashMap)5 Metadata (org.hibernate.boot.Metadata)5 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)5 SQLException (java.sql.SQLException)4 Entity (javax.persistence.Entity)4