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;
}
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);
}
}
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();
}
}
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);
}
}
}
Aggregations