暂存提交
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
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_warehouse_overseas".equalsIgnoreCase(matcher.group(1))) {
|
||||
throw new IllegalStateException(
|
||||
"warehouse-overseas-service requires spring.datasource.url to target the baiyue_warehouse_overseas database");
|
||||
}
|
||||
return beanFactory -> { };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user