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
15 package de.softwareforge.testing.postgres.embedded;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19 import jakarta.annotation.Nonnull;
20 import java.io.IOException;
21 import java.util.Set;
22 import java.util.function.Consumer;
23 import javax.sql.DataSource;
24
25 import com.google.common.collect.ImmutableList;
26 import org.flywaydb.core.Flyway;
27 import org.flywaydb.core.api.FlywayException;
28 import org.flywaydb.core.api.configuration.FluentConfiguration;
29
30 /**
31 * An {@link EmbeddedPostgresPreparer<DataSource>} that uses the <a href="https://flywaydb.org/">Flyway version control for your database</a> framework to
32 * migrate a data source to a known state.
33 */
34 public final class FlywayPreparer implements EmbeddedPostgresPreparer<DataSource> {
35
36 private final ImmutableList.Builder<Consumer<FluentConfiguration>> customizers = ImmutableList.builder();
37
38 /**
39 * Creates a new instance using one or more classpath locations.
40 *
41 * @param locations One or more locations on the classpath.
42 * @return A {@link FlywayPreparer} instance.
43 */
44 @Nonnull
45 public static FlywayPreparer forClasspathLocation(String... locations) {
46 FlywayPreparer preparer = new FlywayPreparer();
47 preparer.addCustomizer(c -> c.locations(locations));
48 return preparer;
49 }
50
51 /**
52 * Create a new, uninitialized preparer instance. Use {@link FlywayPreparer#addCustomizer(Consumer)} to modify the configuration for the
53 * {@link FluentConfiguration} object.
54 */
55 public FlywayPreparer() {
56 }
57
58 /**
59 * Add a customizer instance. Each customizer is called once with the {@link FluentConfiguration} instance before setting the datasource and calling
60 * {@link FluentConfiguration#load()} and {@link Flyway#migrate()}.
61 *
62 * @param customizer A {@link Consumer<FluentConfiguration>} instance. Must not be null.
63 * @return This object.
64 * @since 3.0
65 */
66 @Nonnull
67 public FlywayPreparer addCustomizer(@Nonnull Consumer<FluentConfiguration> customizer) {
68 checkNotNull(customizer, "customizer is null");
69 customizers.add(customizer);
70
71 return this;
72 }
73
74 /**
75 * Add customizer instances. Each customizer is called once with the {@link FluentConfiguration} instance before setting the datasource and calling
76 * {@link FluentConfiguration#load()} and {@link Flyway#migrate()}.
77 *
78 * @param customizers A set of {@link Consumer<FluentConfiguration>} instances. Must not be null.
79 * @return This object.
80 * @since 3.0
81 */
82 @Nonnull
83 public FlywayPreparer addCustomizers(@Nonnull Set<Consumer<FluentConfiguration>> customizers) {
84 checkNotNull(customizers, "customizers is null");
85 customizers.addAll(customizers);
86
87 return this;
88 }
89
90 @Override
91 public void prepare(@Nonnull DataSource dataSource) throws IOException {
92 checkNotNull(dataSource, "dataSource is null");
93
94 try {
95 final FluentConfiguration config = Flyway.configure();
96
97 customizers.build().forEach(c -> c.accept(config));
98
99 config.dataSource(dataSource);
100 Flyway flyway = config.load();
101 flyway.migrate();
102
103 } catch (FlywayException e) {
104 throw new IOException(e);
105 }
106 }
107 }