View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package de.softwareforge.testing.postgres.embedded;
15  
16  import static com.google.common.base.Preconditions.checkNotNull;
17  
18  import java.io.IOException;
19  import java.util.Set;
20  import java.util.function.Consumer;
21  import javax.sql.DataSource;
22  
23  import com.google.common.collect.ImmutableList;
24  import edu.umd.cs.findbugs.annotations.NonNull;
25  import org.flywaydb.core.Flyway;
26  import org.flywaydb.core.api.FlywayException;
27  import org.flywaydb.core.api.configuration.FluentConfiguration;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * An {@link EmbeddedPostgresPreparer<DataSource>} that uses the <a href="https://flywaydb.org/">Flyway version control for your database</a> framework to
33   * migrate a data source to a known state.
34   */
35  public final class FlywayPreparer implements EmbeddedPostgresPreparer<DataSource> {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(FlywayPreparer.class);
38  
39      private final ImmutableList.Builder<Consumer<FluentConfiguration>> customizers = ImmutableList.builder();
40  
41      /**
42       * Creates a new instance using one or more classpath locations.
43       *
44       * @param locations One or more locations on the classpath.
45       * @return A {@link FlywayPreparer} instance.
46       */
47      @NonNull
48      public static FlywayPreparer forClasspathLocation(String... locations) {
49          FlywayPreparermbedded/FlywayPreparer.html#FlywayPreparer">FlywayPreparer preparer = new FlywayPreparer();
50          preparer.addCustomizer(c -> c.locations(locations));
51          return preparer;
52      }
53  
54      /**
55       * Create a new, uninitialized preparer instance. Use {@link #addCustomizer(Consumer)} to modify the configuration for the {@link FluentConfiguration}
56       * object.
57       */
58      public FlywayPreparer() {
59      }
60  
61      /**
62       * Add a customizer instance. Each customizer is called once with the {@link FluentConfiguration} instance before setting the datasource and calling {@link
63       * FluentConfiguration#load()} and {@link Flyway#migrate()}.
64       *
65       * @param customizer A {@link Consumer<FluentConfiguration>} instance. Must not be null.
66       * @return This object.
67       */
68      @NonNull
69      public FlywayPreparer addCustomizer(@NonNull Consumer<FluentConfiguration> customizer) {
70          checkNotNull(customizer, "customizer is null");
71          customizers.add(customizer);
72  
73          return this;
74      }
75  
76      /**
77       * Add customizer instances. Each customizer is called once with the {@link FluentConfiguration} instance before setting the datasource and calling {@link
78       * FluentConfiguration#load()} and {@link Flyway#migrate()}.
79       *
80       * @param customizers A set of {@link Consumer<FluentConfiguration>} instances. Must not be null.
81       * @return This object.
82       */
83      @NonNull
84      public FlywayPreparer addCustomizers(@NonNull Set<Consumer<FluentConfiguration>> customizers) {
85          checkNotNull(customizers, "customizers is null");
86          customizers.addAll(customizers);
87  
88          return this;
89      }
90  
91      /**
92       * @deprecated Use {@link #addCustomizer(Consumer)}.
93       */
94      @Deprecated
95      @NonNull
96      public FlywayPreparer customize(@NonNull Consumer<FluentConfiguration> customizer) {
97          return addCustomizer(customizer);
98      }
99  
100     @Override
101     public void prepare(@NonNull DataSource dataSource) throws IOException {
102         checkNotNull(dataSource, "dataSource is null");
103 
104         try {
105             final FluentConfiguration config = Flyway.configure();
106 
107             customizers.build().forEach(c -> c.accept(config));
108 
109             config.dataSource(dataSource);
110             Flyway flyway = config.load();
111             flyway.migrate();
112 
113         } catch (FlywayException e) {
114             throw new IOException(e);
115         }
116     }
117 }