use of io.micronaut.data.r2dbc.operations.R2dbcOperations in project micronaut-data by micronaut-projects.
the class R2dbcSchemaGenerator method createSchema.
/**
* Creates the schema.
*
* @param beanLocator The bean locator
*/
@PostConstruct
protected void createSchema(BeanLocator beanLocator) {
RuntimeEntityRegistry runtimeEntityRegistry = beanLocator.getBean(RuntimeEntityRegistry.class);
for (DataR2dbcConfiguration configuration : configurations) {
SchemaGenerate schemaGenerate = configuration.getSchemaGenerate();
if (schemaGenerate != null && schemaGenerate != SchemaGenerate.NONE) {
List<String> packages = configuration.getPackages();
Collection<BeanIntrospection<Object>> introspections;
if (CollectionUtils.isNotEmpty(packages)) {
introspections = BeanIntrospector.SHARED.findIntrospections(MappedEntity.class, packages.toArray(new String[0]));
} else {
introspections = BeanIntrospector.SHARED.findIntrospections(MappedEntity.class);
}
PersistentEntity[] entities = introspections.stream().filter(i -> !i.getBeanType().getName().contains("$")).filter(i -> !java.lang.reflect.Modifier.isAbstract(i.getBeanType().getModifiers())).map(e -> runtimeEntityRegistry.getEntity(e.getBeanType())).toArray(PersistentEntity[]::new);
if (ArrayUtils.isNotEmpty(entities)) {
R2dbcOperations r2dbcOperations = configuration.getR2dbcOperations();
SqlQueryBuilder builder = new SqlQueryBuilder(configuration.getDialect());
Mono.from(r2dbcOperations.withConnection(connection -> {
List<String> createStatements = Arrays.stream(entities).flatMap(entity -> Arrays.stream(builder.buildCreateTableStatements(entity))).collect(Collectors.toList());
Flux<Void> createTablesFlow = Flux.fromIterable(createStatements).concatMap(sql -> {
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Creating Table: \n{}", sql);
}
return execute(connection, sql).onErrorResume((throwable -> {
if (LOG.isWarnEnabled()) {
LOG.warn("Unable to create table :{}", throwable.getMessage());
}
return Mono.empty();
}));
});
switch(schemaGenerate) {
case CREATE_DROP:
List<String> dropStatements = Arrays.stream(entities).flatMap(entity -> Arrays.stream(builder.buildDropTableStatements(entity))).collect(Collectors.toList());
return Flux.fromIterable(dropStatements).concatMap(sql -> {
if (DataSettings.QUERY_LOG.isDebugEnabled()) {
DataSettings.QUERY_LOG.debug("Dropping Table: \n{}", sql);
}
return execute(connection, sql).onErrorResume((throwable -> Mono.empty()));
}).thenMany(createTablesFlow).then();
case CREATE:
default:
return createTablesFlow.then();
}
})).block();
}
}
}
}
Aggregations