1
2
3
4
5
6
7
8
9
10
11
12
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
33
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
43
44
45
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
56
57
58 public FlywayPreparer() {
59 }
60
61
62
63
64
65
66
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
78
79
80
81
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
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 }