暂存提交
This commit is contained in:
+1
-1
@@ -18,7 +18,7 @@ public class WarehouseDatabaseGuardConfiguration {
|
||||
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))) {
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -29,9 +29,9 @@ spring:
|
||||
config.namespace: ${NACOS_NAMESPACE:public}
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: ${WAREHOUSE_DB_URL:jdbc:mysql://127.0.0.1:3306/baiyue_warehouse_overseas?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true}
|
||||
url: ${WAREHOUSE_DB_URL:jdbc:mysql://127.0.0.1:3306/baiyue_lastlag_warehouse?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true}
|
||||
username: ${WAREHOUSE_DB_USERNAME:root}
|
||||
password: ${WAREHOUSE_DB_PASSWORD:}
|
||||
password: ${WAREHOUSE_DB_PASSWORD:123456}
|
||||
management.endpoints.web.exposure.include: health,info,prometheus
|
||||
|
||||
baiyue:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
-- This script is run through warehouse-overseas-service's datasource only.
|
||||
-- The datasource guard requires the selected schema to be baiyue_warehouse_overseas;
|
||||
-- The datasource guard requires the selected schema to be baiyue_lastlag_warehouse;
|
||||
-- database creation and USE are intentionally forbidden here so a miswired
|
||||
-- service account cannot create or switch schemas during startup.
|
||||
|
||||
|
||||
+17
-2
@@ -1,17 +1,32 @@
|
||||
package com.baiyue.warehouse.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.env.YamlPropertySourceLoader;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
class WarehouseDatabaseGuardConfigurationTest {
|
||||
@Test
|
||||
void applicationDefaultsTargetDedicatedOverseasWarehouseDatabase() throws Exception {
|
||||
var properties = new YamlPropertySourceLoader()
|
||||
.load("application", new ClassPathResource("application.yml"))
|
||||
.get(0);
|
||||
|
||||
assertThat(properties.getProperty("spring.datasource.url"))
|
||||
.asString()
|
||||
.contains("baiyue_lastlag_warehouse")
|
||||
.doesNotContain("baiyue_lastlag_warehouse");
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsDedicatedOverseasWarehouseDatabase() {
|
||||
MockEnvironment environment = new MockEnvironment()
|
||||
.withProperty("spring.datasource.url",
|
||||
"jdbc:mysql://127.0.0.1:3306/baiyue_warehouse_overseas?serverTimezone=Asia/Shanghai");
|
||||
"jdbc:mysql://127.0.0.1:3306/baiyue_lastlag_warehouse?serverTimezone=Asia/Shanghai");
|
||||
|
||||
assertThatCode(() -> WarehouseDatabaseGuardConfiguration.warehouseDatabaseGuard(environment))
|
||||
.doesNotThrowAnyException();
|
||||
@@ -24,6 +39,6 @@ class WarehouseDatabaseGuardConfigurationTest {
|
||||
|
||||
assertThatThrownBy(() -> WarehouseDatabaseGuardConfiguration.warehouseDatabaseGuard(environment))
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("baiyue_warehouse_overseas");
|
||||
.hasMessageContaining("baiyue_lastlag_warehouse");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user