Search in sources :

Example 1 with VersionHandler

use of com.github.jingshouyan.jdbc.data.init.action.VersionHandler in project j-jdbc by jingshouyan.

the class DataInitAutoConfiguration method run.

@Override
public void run(ApplicationArguments args) {
    Map<String, VersionHandler> map = ctx.getBeansOfType(VersionHandler.class);
    List<VersionHandler> handlers = Lists.newArrayList(map.values());
    Collections.sort(handlers);
    String latest = versionDao.latestVersion().map(DataInitVersion::getVersion).orElse("");
    handlers.stream().filter(h -> isNew(h.version(), latest)).forEach(h -> {
        DataInitVersion version = new DataInitVersion();
        version.setVersion(h.version());
        version.setClazz(h.getClass().getName());
        versionDao.insert(version);
        try {
            h.action();
            version.setSuccess(true);
            versionDao.update(version);
        } catch (Throwable e) {
            version.setSuccess(false);
            version.setMessage(e.getMessage());
            version.forDelete();
            versionDao.update(version);
            throw e;
        }
    });
}
Also used : Ordered(org.springframework.core.Ordered) Order(org.springframework.core.annotation.Order) ApplicationArguments(org.springframework.boot.ApplicationArguments) VersionHandler(com.github.jingshouyan.jdbc.data.init.action.VersionHandler) ApplicationRunner(org.springframework.boot.ApplicationRunner) VersionUtil(com.github.jingshouyan.jdbc.comm.util.VersionUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) DataInitVersionDao(com.github.jingshouyan.jdbc.data.init.dao.DataInitVersionDao) ApplicationContext(org.springframework.context.ApplicationContext) ComponentScan(org.springframework.context.annotation.ComponentScan) Configuration(org.springframework.context.annotation.Configuration) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Lists(com.google.common.collect.Lists) DataInitVersion(com.github.jingshouyan.jdbc.data.init.entity.DataInitVersion) Map(java.util.Map) Collections(java.util.Collections) DataInitVersion(com.github.jingshouyan.jdbc.data.init.entity.DataInitVersion) VersionHandler(com.github.jingshouyan.jdbc.data.init.action.VersionHandler)

Aggregations

VersionUtil (com.github.jingshouyan.jdbc.comm.util.VersionUtil)1 VersionHandler (com.github.jingshouyan.jdbc.data.init.action.VersionHandler)1 DataInitVersionDao (com.github.jingshouyan.jdbc.data.init.dao.DataInitVersionDao)1 DataInitVersion (com.github.jingshouyan.jdbc.data.init.entity.DataInitVersion)1 Lists (com.google.common.collect.Lists)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ApplicationArguments (org.springframework.boot.ApplicationArguments)1 ApplicationRunner (org.springframework.boot.ApplicationRunner)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ComponentScan (org.springframework.context.annotation.ComponentScan)1 Configuration (org.springframework.context.annotation.Configuration)1 Ordered (org.springframework.core.Ordered)1 Order (org.springframework.core.annotation.Order)1