refactor: enforce mysql identity store

This commit is contained in:
lingniu
2026-07-01 10:18:00 +08:00
parent 74e01acc7a
commit 7b0b69295f
2 changed files with 15 additions and 21 deletions

View File

@@ -6,12 +6,12 @@ import com.lingniu.ingest.identity.VehicleIdentityRegistry;
import com.lingniu.ingest.identity.VehicleIdentityResolver;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.util.Locale;
@AutoConfiguration
@EnableConfigurationProperties(VehicleIdentityProperties.class)
@@ -23,27 +23,21 @@ public class VehicleIdentityAutoConfiguration {
return new ObjectMapper();
}
@Bean
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "file")
public Object rejectedLegacyFileIdentityStore() {
throw new IllegalStateException("identity.store=file has been removed; configure identity.store=mysql");
}
@Bean
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "sqlite")
public Object rejectedLegacySqliteIdentityStore() {
throw new IllegalStateException("identity.store=sqlite has been removed; configure identity.store=mysql");
}
@Bean
@ConditionalOnMissingBean({VehicleIdentityResolver.class, VehicleIdentityRegistry.class})
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "mysql",
matchIfMissing = true)
public MySqlVehicleIdentityService mysqlVehicleIdentityService(VehicleIdentityProperties properties,
ObjectMapper objectMapper) {
requireMysqlStore(properties.getStore());
VehicleIdentityProperties.Mysql mysql = properties.getMysql();
DataSource dataSource = new DriverManagerDataSource(
mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword());
return new MySqlVehicleIdentityService(dataSource, mysql, objectMapper);
}
private static void requireMysqlStore(String value) {
String store = value == null || value.isBlank() ? "mysql" : value.trim().toLowerCase(Locale.ROOT);
if (!"mysql".equals(store)) {
throw new IllegalStateException("identity.store supports only mysql; configured value: " + value);
}
}
}

View File

@@ -51,22 +51,22 @@ class VehicleIdentityAutoConfigurationTest {
}
@Test
void rejectsLegacyFileStoreMode() {
void rejectsUnsupportedStoreMode() {
runner
.withPropertyValues("lingniu.ingest.identity.store=file")
.run(context -> assertThat(context.getStartupFailure())
.hasRootCauseInstanceOf(IllegalStateException.class)
.hasMessageContaining("identity.store=file has been removed")
.hasMessageContaining("identity.store=mysql"));
.hasMessageContaining("identity.store supports only mysql")
.hasMessageContaining("file"));
}
@Test
void rejectsLegacySqliteStoreMode() {
void rejectsSqliteStoreModeWithSameMysqlOnlyRule() {
runner
.withPropertyValues("lingniu.ingest.identity.store=sqlite")
.run(context -> assertThat(context.getStartupFailure())
.hasRootCauseInstanceOf(IllegalStateException.class)
.hasMessageContaining("identity.store=sqlite has been removed")
.hasMessageContaining("identity.store=mysql"));
.hasMessageContaining("identity.store supports only mysql")
.hasMessageContaining("sqlite"));
}
}