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);
}
}
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);
}
}
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);
}
}
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());
}
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();
}
Aggregations