Search in sources :

Example 1 with ScriptSourceInput

use of org.hibernate.tool.schema.spi.ScriptSourceInput in project hibernate-orm by hibernate.

the class CrossSchemaForeignKeyGenerationTest method testImprovedSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated.

@Test
@TestForIssue(jiraKey = "HHH-10420")
public void testImprovedSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated() throws Exception {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(SchemaOneEntity.class);
    metadataSources.addAnnotatedClass(SchemaTwoEntity.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    final HibernateSchemaManagementTool tool = (HibernateSchemaManagementTool) ssr.getService(SchemaManagementTool.class);
    final Map configurationValues = ssr.getService(ConfigurationService.class).getSettings();
    final ExecutionOptions options = new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return true;
        }

        @Override
        public Map getConfigurationValues() {
            return configurationValues;
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }
    };
    new GroupedSchemaMigratorImpl(tool, DefaultSchemaFilter.INSTANCE).doMigration(metadata, options, TargetDescriptorImpl.INSTANCE);
    new SchemaDropperImpl(tool).doDrop(metadata, options, ssr.getService(JdbcEnvironment.class).getDialect(), new SourceDescriptor() {

        @Override
        public SourceType getSourceType() {
            return SourceType.METADATA;
        }

        @Override
        public ScriptSourceInput getScriptSourceInput() {
            return null;
        }
    }, buildTargets());
}
Also used : HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) SourceDescriptor(org.hibernate.tool.schema.spi.SourceDescriptor) ScriptSourceInput(org.hibernate.tool.schema.spi.ScriptSourceInput) SourceType(org.hibernate.tool.schema.SourceType) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) GroupedSchemaMigratorImpl(org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Map(java.util.Map) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with ScriptSourceInput

use of org.hibernate.tool.schema.spi.ScriptSourceInput in project hibernate-orm by hibernate.

the class SchemaDropperImpl method doDrop.

/**
 * For tests
 */
public void doDrop(Metadata metadata, final ServiceRegistry serviceRegistry, final Map settings, final boolean manageNamespaces, GenerationTarget... targets) {
    if (targets == null || targets.length == 0) {
        final JdbcContext jdbcContext = tool.resolveJdbcContext(settings);
        targets = new GenerationTarget[] { new GenerationTargetToDatabase(serviceRegistry.getService(TransactionCoordinatorBuilder.class).buildDdlTransactionIsolator(jdbcContext), true) };
    }
    doDrop(metadata, new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return manageNamespaces;
        }

        @Override
        public Map getConfigurationValues() {
            return settings;
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }
    }, serviceRegistry.getService(JdbcEnvironment.class).getDialect(), new SourceDescriptor() {

        @Override
        public SourceType getSourceType() {
            return SourceType.METADATA;
        }

        @Override
        public ScriptSourceInput getScriptSourceInput() {
            return null;
        }
    }, targets);
}
Also used : ExceptionHandler(org.hibernate.tool.schema.spi.ExceptionHandler) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) SourceDescriptor(org.hibernate.tool.schema.spi.SourceDescriptor) ScriptSourceInput(org.hibernate.tool.schema.spi.ScriptSourceInput) JdbcContext(org.hibernate.tool.schema.internal.exec.JdbcContext) SourceType(org.hibernate.tool.schema.SourceType) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) Map(java.util.Map)

Example 3 with ScriptSourceInput

use of org.hibernate.tool.schema.spi.ScriptSourceInput in project hibernate-orm by hibernate.

the class SchemaCreatorImpl method applyImportSources.

private void applyImportSources(ExecutionOptions options, ImportSqlCommandExtractor commandExtractor, boolean format, GenerationTarget... targets) {
    final ServiceRegistry serviceRegistry = tool.getServiceRegistry();
    final ClassLoaderService classLoaderService = serviceRegistry.getService(ClassLoaderService.class);
    // I have had problems applying the formatter to these imported statements.
    // and legacy SchemaExport did not format them, so doing same here
    // final Formatter formatter = format ? DDLFormatterImpl.INSTANCE : FormatStyle.NONE.getFormatter();
    final Formatter formatter = FormatStyle.NONE.getFormatter();
    final Object importScriptSetting = options.getConfigurationValues().get(HBM2DDL_LOAD_SCRIPT_SOURCE);
    String charsetName = (String) options.getConfigurationValues().get(HBM2DDL_CHARSET_NAME);
    if (importScriptSetting != null) {
        final ScriptSourceInput importScriptInput = interpretScriptSourceSetting(importScriptSetting, classLoaderService, charsetName);
        log.executingImportScript(importScriptInput.toString());
        importScriptInput.prepare();
        try {
            for (String command : importScriptInput.read(commandExtractor)) {
                applySqlString(command, formatter, options, targets);
            }
        } finally {
            importScriptInput.release();
        }
    }
    final String importFiles = ConfigurationHelper.getString(AvailableSettings.HBM2DDL_IMPORT_FILES, options.getConfigurationValues(), DEFAULT_IMPORT_FILE);
    for (String currentFile : importFiles.split(",")) {
        final String resourceName = currentFile.trim();
        final ScriptSourceInput importScriptInput = interpretLegacyImportScriptSetting(resourceName, classLoaderService, charsetName);
        importScriptInput.prepare();
        try {
            log.executingImportScript(importScriptInput.toString());
            for (String command : importScriptInput.read(commandExtractor)) {
                applySqlString(command, formatter, options, targets);
            }
        } finally {
            importScriptInput.release();
        }
    }
}
Also used : ScriptSourceInput(org.hibernate.tool.schema.spi.ScriptSourceInput) Formatter(org.hibernate.engine.jdbc.internal.Formatter) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) ServiceRegistry(org.hibernate.service.ServiceRegistry) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 4 with ScriptSourceInput

use of org.hibernate.tool.schema.spi.ScriptSourceInput in project hibernate-orm by hibernate.

the class CrossSchemaForeignKeyGenerationTest method testSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated.

@Test
@TestForIssue(jiraKey = "HHH-10420")
public void testSchemaMigrationForeignKeysAreGeneratedAfterAllTheTablesAreCreated() throws Exception {
    final MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(SchemaOneEntity.class);
    metadataSources.addAnnotatedClass(SchemaTwoEntity.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    final Database database = metadata.getDatabase();
    final HibernateSchemaManagementTool tool = (HibernateSchemaManagementTool) ssr.getService(SchemaManagementTool.class);
    final Map configurationValues = ssr.getService(ConfigurationService.class).getSettings();
    final ExecutionOptions options = new ExecutionOptions() {

        @Override
        public boolean shouldManageNamespaces() {
            return true;
        }

        @Override
        public Map getConfigurationValues() {
            return configurationValues;
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }
    };
    new IndividuallySchemaMigratorImpl(tool, DefaultSchemaFilter.INSTANCE).doMigration(metadata, options, TargetDescriptorImpl.INSTANCE);
    new IndividuallySchemaMigratorImpl(tool, DefaultSchemaFilter.INSTANCE).doMigration(metadata, options, TargetDescriptorImpl.INSTANCE);
    new SchemaDropperImpl(tool).doDrop(metadata, options, ssr.getService(JdbcEnvironment.class).getDialect(), new SourceDescriptor() {

        @Override
        public SourceType getSourceType() {
            return SourceType.METADATA;
        }

        @Override
        public ScriptSourceInput getScriptSourceInput() {
            return null;
        }
    }, buildTargets());
}
Also used : HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) SourceDescriptor(org.hibernate.tool.schema.spi.SourceDescriptor) ScriptSourceInput(org.hibernate.tool.schema.spi.ScriptSourceInput) SourceType(org.hibernate.tool.schema.SourceType) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) Database(org.hibernate.boot.model.relational.Database) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Map(java.util.Map) IndividuallySchemaMigratorImpl(org.hibernate.tool.schema.internal.IndividuallySchemaMigratorImpl) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

ScriptSourceInput (org.hibernate.tool.schema.spi.ScriptSourceInput)4 Map (java.util.Map)3 SourceType (org.hibernate.tool.schema.SourceType)3 ExecutionOptions (org.hibernate.tool.schema.spi.ExecutionOptions)3 SourceDescriptor (org.hibernate.tool.schema.spi.SourceDescriptor)3 MetadataSources (org.hibernate.boot.MetadataSources)2 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)2 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)2 TestForIssue (org.hibernate.testing.TestForIssue)2 HibernateSchemaManagementTool (org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)2 SchemaDropperImpl (org.hibernate.tool.schema.internal.SchemaDropperImpl)2 SchemaManagementTool (org.hibernate.tool.schema.spi.SchemaManagementTool)2 Test (org.junit.Test)2 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)1 Database (org.hibernate.boot.model.relational.Database)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 Formatter (org.hibernate.engine.jdbc.internal.Formatter)1 TransactionCoordinatorBuilder (org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder)1 ServiceRegistry (org.hibernate.service.ServiceRegistry)1 GroupedSchemaMigratorImpl (org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl)1