use of org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter in project quarkus by quarkusio.
the class HibernateOrmDevConsoleInfoSupplier method generateDDL.
private static String generateDDL(SchemaExport.Action action, Metadata metadata, ServiceRegistry serviceRegistry, String importFiles) {
SchemaExport schemaExport = new SchemaExport();
schemaExport.setFormat(true);
schemaExport.setDelimiter(";");
schemaExport.setImportFiles(importFiles);
StringWriter writer = new StringWriter();
try {
schemaExport.doExecution(action, false, metadata, serviceRegistry, new TargetDescriptor() {
@Override
public EnumSet<TargetType> getTargetTypes() {
return EnumSet.of(TargetType.SCRIPT);
}
@Override
public ScriptTargetOutput getScriptTargetOutput() {
return new ScriptTargetOutputToWriter(writer) {
@Override
public void accept(String command) {
super.accept(command);
}
};
}
});
} catch (RuntimeException e) {
StringWriter stackTraceWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stackTraceWriter));
return "Could not generate DDL: \n" + stackTraceWriter.toString();
}
return writer.toString();
}
use of org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter in project quarkus by quarkusio.
the class SchemaManagementIntegrator method runPostBootValidation.
public static void runPostBootValidation(String name) {
if (!LaunchMode.current().isDevOrTest()) {
throw new IllegalStateException("Can only be used in dev or test mode");
}
try {
Holder val = metadataMap.get(name);
if (val == null) {
return;
}
// if this is none we assume another framework is doing this (e.g. flyway)
ServiceRegistry serviceRegistry = val.sessionFactory.getServiceRegistry();
SchemaManagementTool schemaManagementTool = serviceRegistry.getService(SchemaManagementTool.class);
SimpleExecutionOptions executionOptions = new SimpleExecutionOptions(serviceRegistry);
SchemaValidator validator = schemaManagementTool.getSchemaValidator(executionOptions.getConfigurationValues());
try {
validator.doValidation(val.metadata, executionOptions);
} catch (SchemaManagementException e) {
log.error("Failed to validate Schema: " + e.getMessage());
SchemaMigrator migrator = schemaManagementTool.getSchemaMigrator(executionOptions.getConfigurationValues());
StringWriter writer = new StringWriter();
migrator.doMigration(val.metadata, executionOptions, new TargetDescriptor() {
@Override
public EnumSet<TargetType> getTargetTypes() {
return EnumSet.of(TargetType.SCRIPT);
}
@Override
public ScriptTargetOutput getScriptTargetOutput() {
return new ScriptTargetOutputToWriter(writer) {
@Override
public void accept(String command) {
super.accept(command);
}
};
}
});
log.error("The following SQL may resolve the database issues, as generated by the Hibernate schema migration tool. WARNING: You must manually verify this SQL is correct, this is a best effort guess, do not copy/paste it without verifying that it does what you expect.\n\n" + writer.toString());
}
} catch (Throwable t) {
log.error("Failed to run post-boot validation", t);
}
}
use of org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter in project hibernate-orm by hibernate.
the class DefaultCatalogAndSchemaTest method generateScriptFromMetadata.
// This is precisely how scripts are generated for the Quarkus DevUI
// Don't change this code except to match changes in
// https://github.com/quarkusio/quarkus/blob/d07ecb23bfba38ee48868635e155c4b513ce6af9/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/devconsole/HibernateOrmDevConsoleInfoSupplier.java#L61-L92
private String generateScriptFromMetadata(SchemaExport.Action action) {
ServiceRegistryImplementor sessionFactoryServiceRegistry = sessionFactory.getServiceRegistry();
SchemaExport schemaExport = new SchemaExport();
schemaExport.setFormat(true);
schemaExport.setDelimiter(";");
StringWriter writer = new StringWriter();
schemaExport.doExecution(action, false, metadata, sessionFactoryServiceRegistry, new TargetDescriptor() {
@Override
public EnumSet<TargetType> getTargetTypes() {
return EnumSet.of(TargetType.SCRIPT);
}
@Override
public ScriptTargetOutput getScriptTargetOutput() {
return new ScriptTargetOutputToWriter(writer) {
@Override
public void accept(String command) {
super.accept(command);
}
};
}
});
return writer.toString();
}
Aggregations