Search in sources :

Example 1 with TargetDescriptor

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

the class SchemaUpdateTableBackedSequenceTest method testCreateTableOnUpdate.

@Test
public void testCreateTableOnUpdate() throws SQLException {
    Metadata metadata = new MetadataSources(ssr).buildMetadata();
    Database database = metadata.getDatabase();
    TableStructure tableStructure = new TableStructure(database.getJdbcEnvironment(), new QualifiedTableName(null, null, Identifier.toIdentifier("test_seq")), Identifier.toIdentifier("nextval"), 20, 30, Long.class);
    tableStructure.registerExportables(database);
    // lets make sure the InitCommand is there
    assertEquals(1, database.getDefaultNamespace().getTables().size());
    Table table = database.getDefaultNamespace().getTables().iterator().next();
    assertEquals(1, table.getInitCommands().size());
    final TargetImpl target = new TargetImpl();
    ssr.getService(SchemaManagementTool.class).getSchemaMigrator(Collections.emptyMap()).doMigration(metadata, new ExecutionOptions() {

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

        @Override
        public Map getConfigurationValues() {
            return ssr.getService(ConfigurationService.class).getSettings();
        }

        @Override
        public ExceptionHandler getExceptionHandler() {
            return ExceptionHandlerLoggedImpl.INSTANCE;
        }
    }, new TargetDescriptor() {

        @Override
        public EnumSet<TargetType> getTargetTypes() {
            return EnumSet.of(TargetType.SCRIPT, TargetType.DATABASE);
        }

        @Override
        public ScriptTargetOutput getScriptTargetOutput() {
            return target;
        }
    });
    assertTrue(target.found);
    new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) EnumSet(java.util.EnumSet) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ExceptionHandler(org.hibernate.tool.schema.spi.ExceptionHandler) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) TableStructure(org.hibernate.id.enhanced.TableStructure) Database(org.hibernate.boot.model.relational.Database) ScriptTargetOutput(org.hibernate.tool.schema.spi.ScriptTargetOutput) Map(java.util.Map) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) Test(org.junit.Test)

Example 2 with TargetDescriptor

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

the class SchemaExport method execute.

@SuppressWarnings("unchecked")
public void execute(EnumSet<TargetType> targetTypes, Action action, Metadata metadata, ServiceRegistry serviceRegistry) {
    if (action == Action.NONE) {
        LOG.debug("Skipping SchemaExport as Action.NONE was passed");
        return;
    }
    if (targetTypes.isEmpty()) {
        LOG.debug("Skipping SchemaExport as no targets were specified");
        return;
    }
    exceptions.clear();
    LOG.runningHbm2ddlSchemaExport();
    final TargetDescriptor targetDescriptor = buildTargetDescriptor(targetTypes, outputFile, serviceRegistry);
    doExecution(action, needsJdbcConnection(targetTypes), metadata, serviceRegistry, targetDescriptor);
}
Also used : TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor)

Example 3 with TargetDescriptor

use of org.hibernate.tool.schema.spi.TargetDescriptor 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 ExceptionHandler exceptionHandler = haltOnError ? ExceptionHandlerHaltImpl.INSTANCE : 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 {
        if (exceptionHandler instanceof ExceptionHandlerCollectingImpl) {
            exceptions.addAll(((ExceptionHandlerCollectingImpl) exceptionHandler).getExceptions());
        }
    }
}
Also used : SchemaManagementTool(org.hibernate.tool.schema.spi.SchemaManagementTool) ExceptionHandler(org.hibernate.tool.schema.spi.ExceptionHandler) ExecutionOptions(org.hibernate.tool.schema.spi.ExecutionOptions) HashMap(java.util.HashMap) TargetDescriptor(org.hibernate.tool.schema.spi.TargetDescriptor) ExceptionHandlerCollectingImpl(org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TargetDescriptor (org.hibernate.tool.schema.spi.TargetDescriptor)3 Map (java.util.Map)2 ExceptionHandler (org.hibernate.tool.schema.spi.ExceptionHandler)2 ExecutionOptions (org.hibernate.tool.schema.spi.ExecutionOptions)2 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 Database (org.hibernate.boot.model.relational.Database)1 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)1 TableStructure (org.hibernate.id.enhanced.TableStructure)1 Table (org.hibernate.mapping.Table)1 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)1 ExceptionHandlerCollectingImpl (org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl)1 SchemaManagementTool (org.hibernate.tool.schema.spi.SchemaManagementTool)1 ScriptTargetOutput (org.hibernate.tool.schema.spi.ScriptTargetOutput)1 Test (org.junit.Test)1