Search in sources :

Example 6 with SchemaManagementException

use of org.hibernate.tool.schema.spi.SchemaManagementException 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 7 with SchemaManagementException

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

the class ScriptTargetOutputToFile method toFileWriter.

@SuppressWarnings("ResultOfMethodCallIgnored")
static Writer toFileWriter(File file, String charsetName) {
    try {
        if (!file.exists()) {
            // best effort, since this is very likely not allowed in EE environments
            log.debug("Attempting to create non-existent script target file : " + file.getAbsolutePath());
            if (file.getParentFile() != null) {
                file.getParentFile().mkdirs();
            }
            file.createNewFile();
        }
    } catch (Exception e) {
        log.debug("Exception calling File#createNewFile : " + e.toString());
    }
    try {
        return charsetName != null ? new OutputStreamWriter(new FileOutputStream(file, true), charsetName) : new OutputStreamWriter(new FileOutputStream(file, true));
    } catch (IOException e) {
        throw new SchemaManagementException("Unable to open specified script target file for writing : " + file, e);
    }
}
Also used : SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) IOException(java.io.IOException)

Example 8 with SchemaManagementException

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

the class IndividuallySchemaValidatorImplTest method testMismatchColumnType.

@Test
public void testMismatchColumnType() throws Exception {
    MetadataSources metadataSources = new MetadataSources(ssr);
    metadataSources.addAnnotatedClass(NameColumn.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(IntegerNameColumn.class);
        metadata = (MetadataImplementor) metadataSources.buildMetadata();
        metadata.validate();
        try {
            getSchemaValidator(metadata);
            Assert.fail("SchemaManagementException expected");
        } catch (SchemaManagementException e) {
            assertEquals("Schema-validation: wrong column type encountered in column [name] in table [SomeSchema.ColumnEntity]; found [varchar (Types#VARCHAR)], but expecting [integer (Types#INTEGER)]", 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 9 with SchemaManagementException

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

the class AbstractSchemaValidator method validateTable.

protected void validateTable(Table table, TableInformation tableInformation, Metadata metadata, ExecutionOptions options, Dialect dialect) {
    if (tableInformation == null) {
        throw new SchemaManagementException(String.format("Schema-validation: missing table [%s]", table.getQualifiedTableName().toString()));
    }
    final Iterator selectableItr = table.getColumnIterator();
    while (selectableItr.hasNext()) {
        final Selectable selectable = (Selectable) selectableItr.next();
        if (Column.class.isInstance(selectable)) {
            final Column column = (Column) selectable;
            final ColumnInformation existingColumn = tableInformation.getColumn(Identifier.toIdentifier(column.getQuotedName()));
            if (existingColumn == null) {
                throw new SchemaManagementException(String.format("Schema-validation: missing column [%s] in table [%s]", column.getName(), table.getQualifiedTableName()));
            }
            validateColumnType(table, column, existingColumn, metadata, options, dialect);
        }
    }
}
Also used : Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) SchemaManagementException(org.hibernate.tool.schema.spi.SchemaManagementException) ColumnInformation(org.hibernate.tool.schema.extract.spi.ColumnInformation) Iterator(java.util.Iterator)

Aggregations

SchemaManagementException (org.hibernate.tool.schema.spi.SchemaManagementException)9 MetadataSources (org.hibernate.boot.MetadataSources)4 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)4 GenerationTargetToDatabase (org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 DriverManagerConnectionProviderImpl (org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl)2 ServiceRegistryImplementor (org.hibernate.service.spi.ServiceRegistryImplementor)2 DdlTransactionIsolatorTestingImpl (org.hibernate.test.util.DdlTransactionIsolatorTestingImpl)2 JdbcConnectionAccessImpl (org.hibernate.testing.boot.JdbcConnectionAccessImpl)2 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)2 SchemaDropperImpl (org.hibernate.tool.schema.internal.SchemaDropperImpl)2 GenerationTarget (org.hibernate.tool.schema.internal.exec.GenerationTarget)2 GenerationTargetToScript (org.hibernate.tool.schema.internal.exec.GenerationTargetToScript)2 GenerationTargetToStdout (org.hibernate.tool.schema.internal.exec.GenerationTargetToStdout)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 SQLException (java.sql.SQLException)1