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 com.lingniu.ingest.identity.VehicleIdentityResolver;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.Locale;
@AutoConfiguration @AutoConfiguration
@EnableConfigurationProperties(VehicleIdentityProperties.class) @EnableConfigurationProperties(VehicleIdentityProperties.class)
@@ -23,27 +23,21 @@ public class VehicleIdentityAutoConfiguration {
return new ObjectMapper(); 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 @Bean
@ConditionalOnMissingBean({VehicleIdentityResolver.class, VehicleIdentityRegistry.class}) @ConditionalOnMissingBean({VehicleIdentityResolver.class, VehicleIdentityRegistry.class})
@ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "mysql",
matchIfMissing = true)
public MySqlVehicleIdentityService mysqlVehicleIdentityService(VehicleIdentityProperties properties, public MySqlVehicleIdentityService mysqlVehicleIdentityService(VehicleIdentityProperties properties,
ObjectMapper objectMapper) { ObjectMapper objectMapper) {
requireMysqlStore(properties.getStore());
VehicleIdentityProperties.Mysql mysql = properties.getMysql(); VehicleIdentityProperties.Mysql mysql = properties.getMysql();
DataSource dataSource = new DriverManagerDataSource( DataSource dataSource = new DriverManagerDataSource(
mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword()); mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword());
return new MySqlVehicleIdentityService(dataSource, mysql, objectMapper); 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 @Test
void rejectsLegacyFileStoreMode() { void rejectsUnsupportedStoreMode() {
runner runner
.withPropertyValues("lingniu.ingest.identity.store=file") .withPropertyValues("lingniu.ingest.identity.store=file")
.run(context -> assertThat(context.getStartupFailure()) .run(context -> assertThat(context.getStartupFailure())
.hasRootCauseInstanceOf(IllegalStateException.class) .hasRootCauseInstanceOf(IllegalStateException.class)
.hasMessageContaining("identity.store=file has been removed") .hasMessageContaining("identity.store supports only mysql")
.hasMessageContaining("identity.store=mysql")); .hasMessageContaining("file"));
} }
@Test @Test
void rejectsLegacySqliteStoreMode() { void rejectsSqliteStoreModeWithSameMysqlOnlyRule() {
runner runner
.withPropertyValues("lingniu.ingest.identity.store=sqlite") .withPropertyValues("lingniu.ingest.identity.store=sqlite")
.run(context -> assertThat(context.getStartupFailure()) .run(context -> assertThat(context.getStartupFailure())
.hasRootCauseInstanceOf(IllegalStateException.class) .hasRootCauseInstanceOf(IllegalStateException.class)
.hasMessageContaining("identity.store=sqlite has been removed") .hasMessageContaining("identity.store supports only mysql")
.hasMessageContaining("identity.store=mysql")); .hasMessageContaining("sqlite"));
} }
} }