Files
baiyue-firstleg/warehouse-overseas-service/src/main/java/com/baiyue/warehouse/config/WarehouseDatabaseGuardConfiguration.java
T
2026-08-19 16:49:41 +08:00

28 lines
1.2 KiB
Java

package com.baiyue.warehouse.config;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Prevents the warehouse schema migration from ever running against another database. */
@Configuration(proxyBeanMethods = false)
public class WarehouseDatabaseGuardConfiguration {
private static final Pattern MYSQL_SCHEMA = Pattern.compile(
"^jdbc:mysql://[^/]+/([^?;]+)(?:[?;].*)?$", Pattern.CASE_INSENSITIVE);
@Bean
static BeanFactoryPostProcessor warehouseDatabaseGuard(Environment environment) {
String url = environment.getProperty("spring.datasource.url", "").trim();
Matcher matcher = MYSQL_SCHEMA.matcher(url);
if (!matcher.matches() || !"baiyue_lastlag_warehouse".equalsIgnoreCase(matcher.group(1))) {
throw new IllegalStateException(
"warehouse-overseas-service requires spring.datasource.url to target the baiyue_warehouse_overseas database");
}
return beanFactory -> { };
}
}