Search in sources :

Example 1 with GenerationTargetToDatabase

use of org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase in project hibernate-orm by hibernate.

the class IndividuallySchemaValidatorImplTest method testMissingColumn.

@Test
public void testMissingColumn() throws Exception {
    MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(NoNameColumn.class);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    metadata.validate();
    Map<String, Object> settings = new HashMap<>();
    ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
    DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl();
    connectionProvider.configure(properties());
    final GenerationTargetToDatabase schemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, new JdbcConnectionAccessImpl(connectionProvider)));
    try {
        new SchemaCreatorImpl(ssr).doCreation(metadata, serviceRegistry, settings, true, schemaGenerator);
        metadataSources = new MetadataSources(ssr);
        metadataSources.addAnnotatedClass(NameColumn.class);
        metadata = (MetadataImplementor) metadataSources.buildMetadata();
        metadata.validate();
        try {
            getSchemaValidator(metadata);
            Assert.fail("SchemaManagementException expected");
        } catch (SchemaManagementException e) {
            assertEquals("Schema-validation: missing column [name] in table [SomeSchema.ColumnEntity]", e.getMessage());
        }
    } finally {
        new SchemaDropperImpl(serviceRegistry).doDrop(metadata, false, schemaGenerator);
        serviceRegistry.destroy();
        connectionProvider.stop();
    }
}
Also used : JdbcConnectionAccessImpl(org.hibernate.testing.boot.JdbcConnectionAccessImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) DriverManagerConnectionProviderImpl(org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) Test(org.junit.Test)

Example 2 with GenerationTargetToDatabase

use of org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase in project hibernate-orm by hibernate.

the class HibernateSchemaManagementTool method buildGenerationTargets.

GenerationTarget[] buildGenerationTargets(TargetDescriptor targetDescriptor, DdlTransactionIsolator ddlTransactionIsolator, Map options) {
    final String scriptDelimiter = ConfigurationHelper.getString(HBM2DDL_DELIMITER, options);
    final GenerationTarget[] targets = new GenerationTarget[targetDescriptor.getTargetTypes().size()];
    int index = 0;
    if (targetDescriptor.getTargetTypes().contains(TargetType.STDOUT)) {
        targets[index] = new GenerationTargetToStdout(scriptDelimiter);
        index++;
    }
    if (targetDescriptor.getTargetTypes().contains(TargetType.SCRIPT)) {
        if (targetDescriptor.getScriptTargetOutput() == null) {
            throw new SchemaManagementException("Writing to script was requested, but no script file was specified");
        }
        targets[index] = new GenerationTargetToScript(targetDescriptor.getScriptTargetOutput(), scriptDelimiter);
        index++;
    }
    if (targetDescriptor.getTargetTypes().contains(TargetType.DATABASE)) {
        targets[index] = new GenerationTargetToDatabase(ddlTransactionIsolator, false);
    }
    return targets;
}
Also used : GenerationTargetToStdout(org.hibernate.tool.schema.internal.exec.GenerationTargetToStdout) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) GenerationTarget(org.hibernate.tool.schema.internal.exec.GenerationTarget) GenerationTargetToScript(org.hibernate.tool.schema.internal.exec.GenerationTargetToScript) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)

Example 3 with GenerationTargetToDatabase

use of org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase in project hibernate-orm by hibernate.

the class HibernateSchemaManagementTool method buildGenerationTargets.

GenerationTarget[] buildGenerationTargets(TargetDescriptor targetDescriptor, JdbcContext jdbcContext, Map options, boolean needsAutoCommit) {
    final String scriptDelimiter = ConfigurationHelper.getString(HBM2DDL_DELIMITER, options);
    final GenerationTarget[] targets = new GenerationTarget[targetDescriptor.getTargetTypes().size()];
    int index = 0;
    if (targetDescriptor.getTargetTypes().contains(TargetType.STDOUT)) {
        targets[index] = new GenerationTargetToStdout(scriptDelimiter);
        index++;
    }
    if (targetDescriptor.getTargetTypes().contains(TargetType.SCRIPT)) {
        if (targetDescriptor.getScriptTargetOutput() == null) {
            throw new SchemaManagementException("Writing to script was requested, but no script file was specified");
        }
        targets[index] = new GenerationTargetToScript(targetDescriptor.getScriptTargetOutput(), scriptDelimiter);
        index++;
    }
    if (targetDescriptor.getTargetTypes().contains(TargetType.DATABASE)) {
        targets[index] = new GenerationTargetToDatabase(getDdlTransactionIsolator(jdbcContext), true);
    }
    return targets;
}
Also used : GenerationTargetToStdout(org.hibernate.tool.schema.internal.exec.GenerationTargetToStdout) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) GenerationTarget(org.hibernate.tool.schema.internal.exec.GenerationTarget) GenerationTargetToScript(org.hibernate.tool.schema.internal.exec.GenerationTargetToScript) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)

Example 4 with GenerationTargetToDatabase

use of org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase 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 5 with GenerationTargetToDatabase

use of org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase in project hibernate-orm by hibernate.

the class SuppliedConnectionTest method cleanupTest.

@Override
protected void cleanupTest() throws Exception {
    try {
        Connection conn = cp.getConnection();
        try {
            final GenerationTargetToDatabase target = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry(), conn), true);
            new SchemaDropperImpl(serviceRegistry()).doDrop(metadata(), false, target);
        } finally {
            cp.closeConnection(conn);
        }
    } catch (Throwable ignore) {
    }
    super.cleanupTest();
}
Also used : DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) Connection(java.sql.Connection) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)

Aggregations

GenerationTargetToDatabase (org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)10 DdlTransactionIsolatorTestingImpl (org.hibernate.test.util.DdlTransactionIsolatorTestingImpl)7 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)6 SchemaDropperImpl (org.hibernate.tool.schema.internal.SchemaDropperImpl)6 MetadataSources (org.hibernate.boot.MetadataSources)5 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 SchemaManagementException (org.hibernate.tool.schema.spi.SchemaManagementException)4 HashMap (java.util.HashMap)3 Metadata (org.hibernate.boot.Metadata)3 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)3 GenerationTarget (org.hibernate.tool.schema.internal.exec.GenerationTarget)3 Test (org.junit.Test)3 Connection (java.sql.Connection)2 Map (java.util.Map)2 SessionFactoryBuilder (org.hibernate.boot.SessionFactoryBuilder)2 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)2 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)2 JdbcConnectionAccessImpl (org.hibernate.testing.boot.JdbcConnectionAccessImpl)2 HibernateSchemaManagementTool (org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)2 GenerationTargetToScript (org.hibernate.tool.schema.internal.exec.GenerationTargetToScript)2