Search in sources :

Example 1 with LiquibaseFactory

use of io.quarkus.liquibase.LiquibaseFactory in project quarkus by quarkusio.

the class LiquibaseRecorder method doStartActions.

public void doStartActions() {
    try {
        InjectableInstance<LiquibaseFactory> liquibaseFactoryInstance = Arc.container().select(LiquibaseFactory.class, Any.Literal.INSTANCE);
        if (liquibaseFactoryInstance.isUnsatisfied()) {
            return;
        }
        for (InstanceHandle<LiquibaseFactory> liquibaseFactoryHandle : liquibaseFactoryInstance.handles()) {
            try {
                LiquibaseFactory liquibaseFactory = liquibaseFactoryHandle.get();
                var config = liquibaseFactory.getConfiguration();
                if (!config.cleanAtStart && !config.migrateAtStart) {
                    continue;
                }
                try (Liquibase liquibase = liquibaseFactory.createLiquibase()) {
                    if (config.cleanAtStart) {
                        liquibase.dropAll();
                    }
                    if (config.migrateAtStart) {
                        if (config.validateOnMigrate) {
                            liquibase.validate();
                        }
                        liquibase.update(liquibaseFactory.createContexts(), liquibaseFactory.createLabels());
                    }
                }
            } catch (UnsatisfiedResolutionException e) {
            // ignore, the DS is not configured
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException("Error starting Liquibase", e);
    }
}
Also used : Liquibase(liquibase.Liquibase) LiquibaseFactory(io.quarkus.liquibase.LiquibaseFactory) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException)

Example 2 with LiquibaseFactory

use of io.quarkus.liquibase.LiquibaseFactory in project quarkus by quarkusio.

the class LiquibaseSchemaProvider method resetAllDatabases.

@Override
public void resetAllDatabases() {
    try {
        InjectableInstance<LiquibaseFactory> liquibaseFactoryInstance = Arc.container().select(LiquibaseFactory.class, Any.Literal.INSTANCE);
        if (liquibaseFactoryInstance.isUnsatisfied()) {
            return;
        }
        for (InstanceHandle<LiquibaseFactory> liquibaseFactoryHandle : liquibaseFactoryInstance.handles()) {
            try {
                LiquibaseFactory liquibaseFactory = liquibaseFactoryHandle.get();
                doReset(liquibaseFactory);
            } catch (UnsatisfiedResolutionException e) {
            // ignore, the DS is not configured
            }
        }
    } catch (Exception e) {
        throw new IllegalStateException("Error starting Liquibase", e);
    }
}
Also used : LiquibaseFactory(io.quarkus.liquibase.LiquibaseFactory) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 3 with LiquibaseFactory

use of io.quarkus.liquibase.LiquibaseFactory in project quarkus by quarkusio.

the class LiquibaseDevConsoleRecorder method handler.

public Handler<RoutingContext> handler() {
    return new DevConsolePostHandler() {

        @Override
        protected void handlePost(RoutingContext event, MultiMap form) throws Exception {
            String datasource = form.get("datasource");
            String operation = form.get("operation");
            InjectableInstance<LiquibaseFactory> liquibaseFactoryInstance = Arc.container().select(LiquibaseFactory.class);
            if (liquibaseFactoryInstance.isUnsatisfied()) {
                return;
            }
            Annotation qualifier;
            if (DataSourceUtil.isDefault(datasource)) {
                qualifier = Default.Literal.INSTANCE;
            } else {
                qualifier = LiquibaseDataSourceLiteral.of(datasource);
            }
            InstanceHandle<LiquibaseFactory> liquibaseFactoryHandle = Arc.container().instance(LiquibaseFactory.class, qualifier);
            if (!liquibaseFactoryHandle.isAvailable()) {
                flashMessage(event, "Unknown datasource: " + datasource, FlashMessageStatus.ERROR);
            }
            LiquibaseFactory liquibaseFactory = liquibaseFactoryHandle.get();
            if ("clean".equals(operation)) {
                try (Liquibase liquibase = liquibaseFactory.createLiquibase()) {
                    liquibase.dropAll();
                }
                flashMessage(event, "Datasource " + datasource + " cleaned");
                return;
            } else if ("migrate".equals(operation)) {
                try (Liquibase liquibase = liquibaseFactory.createLiquibase()) {
                    liquibase.update(liquibaseFactory.createContexts(), liquibaseFactory.createLabels());
                }
                flashMessage(event, "Datasource " + datasource + " migrated");
                return;
            } else {
                flashMessage(event, "Invalid operation: " + operation, FlashMessageStatus.ERROR);
                return;
            }
        }
    };
}
Also used : DevConsolePostHandler(io.quarkus.devconsole.runtime.spi.DevConsolePostHandler) Liquibase(liquibase.Liquibase) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) LiquibaseFactory(io.quarkus.liquibase.LiquibaseFactory) Annotation(java.lang.annotation.Annotation)

Example 4 with LiquibaseFactory

use of io.quarkus.liquibase.LiquibaseFactory in project quarkus by quarkusio.

the class LiquibaseRecorder method liquibaseSupplier.

public Supplier<LiquibaseFactory> liquibaseSupplier(String dataSourceName) {
    DataSource dataSource = DataSources.fromName(dataSourceName);
    if (dataSource instanceof UnconfiguredDataSource) {
        return new Supplier<LiquibaseFactory>() {

            @Override
            public LiquibaseFactory get() {
                throw new UnsatisfiedResolutionException("No datasource has been configured");
            }
        };
    }
    LiquibaseFactoryProducer liquibaseProducer = Arc.container().instance(LiquibaseFactoryProducer.class).get();
    LiquibaseFactory liquibaseFactory = liquibaseProducer.createLiquibaseFactory(dataSource, dataSourceName);
    return new Supplier<LiquibaseFactory>() {

        @Override
        public LiquibaseFactory get() {
            return liquibaseFactory;
        }
    };
}
Also used : UnconfiguredDataSource(io.quarkus.agroal.runtime.UnconfiguredDataSource) LiquibaseFactory(io.quarkus.liquibase.LiquibaseFactory) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) Supplier(java.util.function.Supplier) UnconfiguredDataSource(io.quarkus.agroal.runtime.UnconfiguredDataSource) DataSource(javax.sql.DataSource)

Aggregations

LiquibaseFactory (io.quarkus.liquibase.LiquibaseFactory)4 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)3 Liquibase (liquibase.Liquibase)2 UnconfiguredDataSource (io.quarkus.agroal.runtime.UnconfiguredDataSource)1 DevConsolePostHandler (io.quarkus.devconsole.runtime.spi.DevConsolePostHandler)1 MultiMap (io.vertx.core.MultiMap)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 Annotation (java.lang.annotation.Annotation)1 Supplier (java.util.function.Supplier)1 DataSource (javax.sql.DataSource)1 LiquibaseException (liquibase.exception.LiquibaseException)1