use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.
the class SchemaExport method doExecution.
public void doExecution(Action action, boolean needsJdbc, Metadata metadata, ServiceRegistry serviceRegistry, TargetDescriptor targetDescriptor) {
Map config = new HashMap();
config.putAll(serviceRegistry.getService(ConfigurationService.class).getSettings());
config.put(AvailableSettings.HBM2DDL_DELIMITER, delimiter);
config.put(AvailableSettings.FORMAT_SQL, format);
config.put(AvailableSettings.HBM2DDL_IMPORT_FILES, importFiles);
final SchemaManagementTool tool = serviceRegistry.getService(SchemaManagementTool.class);
final ExceptionHandler exceptionHandler = haltOnError ? ExceptionHandlerHaltImpl.INSTANCE : new ExceptionHandlerCollectingImpl();
final ExecutionOptions executionOptions = SchemaManagementToolCoordinator.buildExecutionOptions(config, exceptionHandler);
final SourceDescriptor sourceDescriptor = new SourceDescriptor() {
@Override
public SourceType getSourceType() {
return SourceType.METADATA;
}
@Override
public ScriptSourceInput getScriptSourceInput() {
return null;
}
};
try {
if (action.doDrop()) {
tool.getSchemaDropper(config).doDrop(metadata, executionOptions, sourceDescriptor, targetDescriptor);
}
if (action.doCreate()) {
tool.getSchemaCreator(config).doCreation(metadata, executionOptions, sourceDescriptor, targetDescriptor);
}
} finally {
if (exceptionHandler instanceof ExceptionHandlerCollectingImpl) {
exceptions.addAll(((ExceptionHandlerCollectingImpl) exceptionHandler).getExceptions());
}
}
}
use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.
the class SchemaManagementToolInitiator method initiateService.
public SchemaManagementTool initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object setting = configurationValues.get(AvailableSettings.SCHEMA_MANAGEMENT_TOOL);
SchemaManagementTool tool = registry.getService(StrategySelector.class).resolveStrategy(SchemaManagementTool.class, setting);
if (tool == null) {
tool = new HibernateSchemaManagementTool();
}
return tool;
}
use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.
the class SchemaToolTransactionHandlingTest method testDropCreateDropInExistingJtaTransaction.
// for each case we want to run these tool delegates in a matrix of:
// 1) JTA versus JDBC transaction handling
// 2) existing transaction versus not
//
// cases:
// 1) create-drop
// 2) update
// 3) validate
//
// so:
// 1) create-drop
// 1.1) JTA transaction handling
// 1.1.1) inside an existing transaction
// 1.1.2) outside any transaction
// 1.1) JDBC transaction handling
// - there really cannot be an "existing transaction" case...
@Test
public void testDropCreateDropInExistingJtaTransaction() {
// start a JTA transaction...
try {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
} catch (Exception e) {
throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
}
// hold reference to Transaction object
final Transaction jtaTransaction;
try {
jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
} catch (SystemException e) {
throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
}
final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
// perform the test...
try {
final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
final SchemaDropper schemaDropper = smt.getSchemaDropper(Collections.emptyMap());
final SchemaCreator schemaCreator = smt.getSchemaCreator(Collections.emptyMap());
final Metadata mappings = buildMappings(registry);
try {
try {
schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (CommandAcceptanceException e) {
//ignore may happen if sql drop does not support if exist
}
schemaCreator.doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} finally {
try {
schemaDropper.doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (Exception ignore) {
// ignore
}
}
} finally {
try {
jtaTransaction.commit();
((StandardServiceRegistryImpl) registry).destroy();
} catch (Exception e) {
// not much we can do...
}
}
}
use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.
the class SchemaToolTransactionHandlingTest method testValidateInExistingJtaTransaction.
@Test
public void testValidateInExistingJtaTransaction() {
// start a JTA transaction...
try {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
} catch (Exception e) {
throw new RuntimeException("Unable to being JTA transaction prior to starting test", e);
}
// hold reference to Transaction object
final Transaction jtaTransaction;
try {
jtaTransaction = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().getTransaction();
} catch (SystemException e) {
throw new RuntimeException("Unable to access JTA Transaction prior to starting test", e);
}
final StandardServiceRegistry registry = buildJtaStandardServiceRegistry();
// perform the test...
try {
final SchemaManagementTool smt = registry.getService(SchemaManagementTool.class);
final Metadata mappings = buildMappings(registry);
// first make the schema exist...
try {
smt.getSchemaCreator(Collections.emptyMap()).doCreation(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (Exception e) {
throw new RuntimeException("Unable to create schema to validation tests", e);
}
try {
smt.getSchemaValidator(Collections.emptyMap()).doValidation(mappings, ExecutionOptionsTestImpl.INSTANCE);
} finally {
try {
smt.getSchemaDropper(Collections.emptyMap()).doDrop(mappings, ExecutionOptionsTestImpl.INSTANCE, SourceDescriptorImpl.INSTANCE, TargetDescriptorImpl.INSTANCE);
} catch (Exception ignore) {
// ignore
}
}
} finally {
try {
jtaTransaction.commit();
((StandardServiceRegistryImpl) registry).destroy();
} catch (Exception e) {
// not much we can do...
}
}
}
use of org.hibernate.tool.schema.spi.SchemaManagementTool in project hibernate-orm by hibernate.
the class SchemaUpdate method execute.
@SuppressWarnings("unchecked")
public void execute(EnumSet<TargetType> targetTypes, Metadata metadata, ServiceRegistry serviceRegistry) {
if (targetTypes.isEmpty()) {
LOG.debug("Skipping SchemaExport as no targets were specified");
return;
}
exceptions.clear();
LOG.runningHbm2ddlSchemaUpdate();
Map config = new HashMap();
config.putAll(serviceRegistry.getService(ConfigurationService.class).getSettings());
config.put(AvailableSettings.HBM2DDL_DELIMITER, delimiter);
config.put(AvailableSettings.FORMAT_SQL, format);
final SchemaManagementTool tool = serviceRegistry.getService(SchemaManagementTool.class);
final ExceptionHandlerCollectingImpl exceptionHandler = new ExceptionHandlerCollectingImpl();
final ExecutionOptions executionOptions = SchemaManagementToolCoordinator.buildExecutionOptions(config, exceptionHandler);
final TargetDescriptor targetDescriptor = SchemaExport.buildTargetDescriptor(targetTypes, outputFile, serviceRegistry);
try {
tool.getSchemaMigrator(config).doMigration(metadata, executionOptions, targetDescriptor);
} finally {
exceptions.addAll(exceptionHandler.getExceptions());
}
}
Aggregations