Skip to content

Commit ea8e0cf

Browse files
committed
Support for simple numeric Flyway version
While Flyway's `MigrationVersion` is a String value, a simple "0" is also a valid number. When such value is not wrapped in YAML, an integer is injected rather than a String which leads to a failure as we can't convert it. This commit updates the `StringToMigrationVersionConverter` to also supports conversion from a Number. This is a convenience for users using YAML for configuration Closes gh-4981
1 parent c23af8f commit ea8e0cf

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,15 @@
1616

1717
package org.springframework.boot.autoconfigure.flyway;
1818

19+
import java.util.Collections;
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
1923
import javax.annotation.PostConstruct;
2024
import javax.persistence.EntityManagerFactory;
2125
import javax.sql.DataSource;
2226

27+
import org.apache.commons.lang.ObjectUtils;
2328
import org.flywaydb.core.Flyway;
2429
import org.flywaydb.core.api.MigrationVersion;
2530

@@ -38,7 +43,8 @@
3843
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3944
import org.springframework.context.annotation.Bean;
4045
import org.springframework.context.annotation.Configuration;
41-
import org.springframework.core.convert.converter.Converter;
46+
import org.springframework.core.convert.TypeDescriptor;
47+
import org.springframework.core.convert.converter.GenericConverter;
4248
import org.springframework.core.io.DefaultResourceLoader;
4349
import org.springframework.core.io.ResourceLoader;
4450
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
@@ -63,8 +69,8 @@ public class FlywayAutoConfiguration {
6369

6470
@Bean
6571
@ConfigurationPropertiesBinding
66-
public StringToMigrationVersionConverter stringToMigrationVersionConverter() {
67-
return new StringToMigrationVersionConverter();
72+
public StringOrNumberToMigrationVersionConverter stringOrNumberMigrationVersionConverter() {
73+
return new StringOrNumberToMigrationVersionConverter();
6874
}
6975

7076
@Configuration
@@ -169,14 +175,29 @@ public FlywayJpaDependencyConfiguration() {
169175
}
170176

171177
/**
172-
* Convert a String to a {@link MigrationVersion}.
178+
* Convert a String or Number to a {@link MigrationVersion}.
173179
*/
174-
private static class StringToMigrationVersionConverter
175-
implements Converter<String, MigrationVersion> {
180+
private static class StringOrNumberToMigrationVersionConverter
181+
implements GenericConverter {
182+
183+
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS = createConvertiblePairs();
176184

177185
@Override
178-
public MigrationVersion convert(String source) {
179-
return MigrationVersion.fromVersion(source);
186+
public Set<ConvertiblePair> getConvertibleTypes() {
187+
return CONVERTIBLE_PAIRS;
188+
}
189+
190+
@Override
191+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
192+
String value = ObjectUtils.toString(source);
193+
return MigrationVersion.fromVersion(value);
194+
}
195+
196+
private static Set<ConvertiblePair> createConvertiblePairs() {
197+
Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>(2);
198+
convertiblePairs.add(new ConvertiblePair(String.class, MigrationVersion.class));
199+
convertiblePairs.add(new ConvertiblePair(Number.class, MigrationVersion.class));
200+
return Collections.unmodifiableSet(convertiblePairs);
180201
}
181202

182203
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.flyway;
1818

1919
import java.util.Arrays;
20+
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.Map;
2223

@@ -42,6 +43,7 @@
4243
import org.springframework.context.annotation.Bean;
4344
import org.springframework.context.annotation.Configuration;
4445
import org.springframework.core.Ordered;
46+
import org.springframework.core.env.MapPropertySource;
4547
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
4648
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
4749
import org.springframework.stereotype.Component;
@@ -198,7 +200,7 @@ public void customFlywayWithJpa() throws Exception {
198200
}
199201

200202
@Test
201-
public void overrideBaselineVersion() throws Exception {
203+
public void overrideBaselineVersionString() throws Exception {
202204
EnvironmentTestUtils.addEnvironment(this.context, "flyway.baseline-version=0");
203205
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
204206
FlywayAutoConfiguration.class,
@@ -208,6 +210,18 @@ public void overrideBaselineVersion() throws Exception {
208210
equalTo(MigrationVersion.fromVersion("0")));
209211
}
210212

213+
@Test
214+
public void overrideBaselineVersionNumber() throws Exception {
215+
Map<String, Object> source = Collections.<String, Object>singletonMap("flyway.baseline-version", 1);
216+
this.context.getEnvironment().getPropertySources().addLast(new MapPropertySource("flyway", source));
217+
registerAndRefresh(EmbeddedDataSourceConfiguration.class,
218+
FlywayAutoConfiguration.class,
219+
PropertyPlaceholderAutoConfiguration.class);
220+
Flyway flyway = this.context.getBean(Flyway.class);
221+
assertThat(flyway.getBaselineVersion(),
222+
equalTo(MigrationVersion.fromVersion("1")));
223+
}
224+
211225
private void registerAndRefresh(Class<?>... annotatedClasses) {
212226
this.context.register(annotatedClasses);
213227
this.context.refresh();

0 commit comments

Comments
 (0)