Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.jpa.flyway

File FlywayConfiguration.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
25% of files have more coverage

Code metrics

6
20
2
1
58
52
5
0.25
10
2
2.5

Classes

Class Line # Actions
FlywayConfiguration 20 20 5
0.678571467.9%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.jpa.flyway;
2   
3    import static io.sunshower.persist.core.DataSourceConfigurations.isBaselineVersion;
4   
5    import io.sunshower.persist.core.DatabaseConfigurationSource;
6    import io.sunshower.persistence.Dialect;
7    import io.sunshower.persistence.MigrationResult;
8    import io.sunshower.persistence.PersistenceConfiguration;
9    import io.sunshower.persistence.PersistenceUnit;
10    import javax.inject.Singleton;
11    import javax.sql.DataSource;
12    import org.flywaydb.core.Flyway;
13    import org.slf4j.Logger;
14    import org.slf4j.LoggerFactory;
15    import org.springframework.context.ApplicationContext;
16    import org.springframework.context.annotation.Bean;
17    import org.springframework.context.annotation.Configuration;
18   
19    @Configuration
 
20    public class FlywayConfiguration {
21    static final Logger log = LoggerFactory.getLogger(FlywayConfiguration.class);
22   
 
23  2 toggle @Bean
24    public static PersistenceUnit persistenceConfigurationProcessor(
25    ApplicationContext context, Dialect dialect) {
26  2 log.info("Configured with dialect: {}", dialect.getKey());
27  2 return new PersistenceUnit(dialect, context);
28    }
29   
 
30  2 toggle @Bean
31    @Singleton
32    public MigrationResult createMigrations(
33    DataSource dataSource, PersistenceUnit context, DatabaseConfigurationSource source) {
34  2 for (PersistenceConfiguration ctx : context.configurations()) {
35  2 final Flyway flyway = new Flyway();
36   
37  2 if (isBaselineVersion(source)) {
38  0 log.info("Setting baseline version to {}", source.version());
39  0 flyway.setBaselineVersionAsString(source.version());
40    }
41  2 final String table = ctx.getId() + "_migrations";
42  2 flyway.setTable(table);
43  2 if (!ctx.getSchema().trim().isEmpty()) {
44  0 flyway.setSchemas(ctx.getSchema());
45    }
46  2 flyway.setDataSource(dataSource);
47  2 String[] migrationPaths = ctx.getMigrationPaths();
48  2 flyway.setLocations(migrationPaths);
49  2 if (isBaselineVersion(source)) {
50  0 log.info("baselining database...");
51  0 flyway.baseline();
52  0 log.info("database baselined");
53    }
54  2 flyway.migrate();
55    }
56  2 return new MigrationResult(context);
57    }
58    }