Search in sources :

Example 1 with R2dbcOperations

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();
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) R2dbcOperations(io.micronaut.data.r2dbc.operations.R2dbcOperations) Logger(org.slf4j.Logger) Context(io.micronaut.context.annotation.Context) Collection(java.util.Collection) ArrayUtils(io.micronaut.core.util.ArrayUtils) LoggerFactory(org.slf4j.LoggerFactory) MappedEntity(io.micronaut.data.annotation.MappedEntity) Mono(reactor.core.publisher.Mono) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) Internal(io.micronaut.core.annotation.Internal) SchemaGenerate(io.micronaut.data.runtime.config.SchemaGenerate) Collectors(java.util.stream.Collectors) Connection(io.r2dbc.spi.Connection) Flux(reactor.core.publisher.Flux) List(java.util.List) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) PersistentEntity(io.micronaut.data.model.PersistentEntity) CollectionUtils(io.micronaut.core.util.CollectionUtils) PostConstruct(javax.annotation.PostConstruct) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) BeanLocator(io.micronaut.context.BeanLocator) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) DataSettings(io.micronaut.data.runtime.config.DataSettings) MappedEntity(io.micronaut.data.annotation.MappedEntity) PersistentEntity(io.micronaut.data.model.PersistentEntity) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) SchemaGenerate(io.micronaut.data.runtime.config.SchemaGenerate) R2dbcOperations(io.micronaut.data.r2dbc.operations.R2dbcOperations) PostConstruct(javax.annotation.PostConstruct)

Aggregations

BeanLocator (io.micronaut.context.BeanLocator)1 Context (io.micronaut.context.annotation.Context)1 Internal (io.micronaut.core.annotation.Internal)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)1 ArrayUtils (io.micronaut.core.util.ArrayUtils)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 MappedEntity (io.micronaut.data.annotation.MappedEntity)1 PersistentEntity (io.micronaut.data.model.PersistentEntity)1 SqlQueryBuilder (io.micronaut.data.model.query.builder.sql.SqlQueryBuilder)1 RuntimeEntityRegistry (io.micronaut.data.model.runtime.RuntimeEntityRegistry)1 R2dbcOperations (io.micronaut.data.r2dbc.operations.R2dbcOperations)1 DataSettings (io.micronaut.data.runtime.config.DataSettings)1 SchemaGenerate (io.micronaut.data.runtime.config.SchemaGenerate)1 Connection (io.r2dbc.spi.Connection)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1