28 lines
1.2 KiB
Java
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 -> { };
|
|
}
|
|
}
|