Search in sources :

Example 1 with ScriptTargetOutputToWriter

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();
}
Also used : StringWriter(java.io.StringWriter) EnumSet(java.util.EnumSet) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ScriptTargetOutput(org.hibernate.tool.schema.spi.ScriptTargetOutput) ScriptTargetOutputToWriter(org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) PrintWriter(java.io.PrintWriter)

Example 2 with ScriptTargetOutputToWriter

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);
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) SchemaValidator(org.hibernate.tool.schema.spi.SchemaValidator) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ScriptTargetOutputToWriter(org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter) SchemaMigrator(org.hibernate.tool.schema.spi.SchemaMigrator) StringWriter(java.io.StringWriter) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) TargetType(org.hibernate.tool.schema.TargetType) SessionFactoryServiceRegistry(org.hibernate.service.spi.SessionFactoryServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 3 with ScriptTargetOutputToWriter

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();
}
Also used : StringWriter(java.io.StringWriter) EnumSet(java.util.EnumSet) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) ScriptTargetOutput(org.hibernate.tool.schema.spi.ScriptTargetOutput) ScriptTargetOutputToWriter(org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport)

Aggregations

StringWriter (java.io.StringWriter)3 ScriptTargetOutputToWriter (org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToWriter)3 TargetDescriptor (org.hibernate.tool.schema.spi.TargetDescriptor)3 EnumSet (java.util.EnumSet)2 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)2 ScriptTargetOutput (org.hibernate.tool.schema.spi.ScriptTargetOutput)2 PrintWriter (java.io.PrintWriter)1 ServiceRegistry (org.hibernate.service.ServiceRegistry)1 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)1 SessionFactoryServiceRegistry (org.hibernate.service.spi.SessionFactoryServiceRegistry)1 TargetType (org.hibernate.tool.schema.TargetType)1 SchemaManagementException (org.hibernate.tool.schema.spi.SchemaManagementException)1 SchemaManagementTool (org.hibernate.tool.schema.spi.SchemaManagementTool)1 SchemaMigrator (org.hibernate.tool.schema.spi.SchemaMigrator)1 SchemaValidator (org.hibernate.tool.schema.spi.SchemaValidator)1