Files
baiyue-firstleg/lastleg-service/src/test/java/com/baiyue/lastleg/schema/Phase5OverseasUpgradeSqlContractTest.java
T

145 lines
7.3 KiB
Java
Raw Normal View History

package com.baiyue.lastleg.schema;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
class Phase5OverseasUpgradeSqlContractTest {
private static final List<String> PHASE5_TABLES = List.of(
"ll_dispatch_pda_task", "ll_dispatch_pda_pick", "ll_dispatch_pda_operation",
"ll_dispatch_pda_pick_order", "ll_outbound_order", "ll_outbound_pallet_item",
2026-09-09 16:37:28 +08:00
"ll_outbound_pda_task", "ll_outbound_pda_pick", "ll_delivery_order",
"ll_delivery_operation", "ll_dropship_order_profile", "ll_dropship_carton",
"ll_dropship_sku_inventory", "ll_dropship_carton_ledger", "ll_exception_order",
"ll_exception_return_item", "ll_exception_order_command", "ll_customer_visit");
@Test
void overseasFixedEntryCreatesEveryPhase5TableBeforeAnyPhase5AlterOrAssertion() throws Exception {
String overseas = repositoryFile("deploy/sql/海外区域统一增量-MySQL8.sql");
int phase5Gate = overseas.indexOf("-- 五期排车、出库与海外PDA正式业务号/幂等约束");
assertThat(phase5Gate).isPositive();
for (String table : PHASE5_TABLES) {
String create = "CREATE TABLE IF NOT EXISTS " + table + " (";
assertThat(overseas.indexOf(create)).as(table).isBetween(0, phase5Gate - 1);
assertThat(count(overseas, create)).as(table).isEqualTo(1);
assertThat(overseas.indexOf(table)).as(table + " first reference")
.isEqualTo(overseas.indexOf(create) + "CREATE TABLE IF NOT EXISTS ".length());
}
assertThat(overseas).contains("<> 23", "五期完整业务表集合未部署");
}
@Test
void overseasFixedEntryConvergesKnownIntermediatePhase5ShapesWithoutBackfill() throws Exception {
String overseas = repositoryFile("deploy/sql/海外区域统一增量-MySQL8.sql");
assertThat(overseas).contains(
"ALTER TABLE ll_outbound_order RENAME COLUMN tenant_id TO customer_id",
"ALTER TABLE ll_outbound_pallet_item RENAME COLUMN tenant_id TO customer_id",
"ALTER TABLE ll_delivery_order RENAME COLUMN tenant_id TO customer_id",
"ALTER TABLE ll_outbound_order ADD COLUMN outbound_type",
2026-09-07 11:48:49 +08:00
"ALTER TABLE ll_outbound_order ADD COLUMN lx_codes",
"ALTER TABLE ll_outbound_pallet_item ADD COLUMN picked_package_count",
"ALTER TABLE ll_delivery_order ADD COLUMN overseas_warehouse_name",
"ALTER TABLE ll_delivery_order ADD COLUMN waybill_no",
"ALTER TABLE ll_dropship_order_profile ADD COLUMN source_pallet_no",
2026-09-10 23:48:48 +08:00
"ALTER TABLE ll_dropship_order_profile ADD COLUMN manifest_version",
"ALTER TABLE ll_dropship_order_profile ADD COLUMN manifest_package_count",
"ALTER TABLE ll_dropship_order_profile ADD COLUMN manifest_fragment_count",
"ALTER TABLE ll_dropship_order_profile ADD COLUMN manifest_hash",
2026-09-11 00:06:57 +08:00
"ALTER TABLE ll_inbound_plan_pallet ADD COLUMN version",
2026-09-10 23:48:48 +08:00
"ADD KEY idx_ll_dropship_profile_manifest(source_pallet_no,manifest_hash)",
"ADD UNIQUE KEY uk_ll_dropship_sku_inventory(profile_id,sku_code)",
"MODIFY COLUMN source_pallet_no VARCHAR(128) NOT NULL");
int relabelCreateEnd = overseas.indexOf(") ENGINE=InnoDB COMMENT='换标货物冻结明细';");
int relabelModify = overseas.indexOf("MODIFY COLUMN destination_fba_warehouse_code", relabelCreateEnd);
for (String column : List.of("destination_fba_warehouse_code", "source_inventory_balance_id",
"source_inventory_position_id", "source_inbound_order_id")) {
assertThat(overseas.indexOf("ALTER TABLE ll_relabel_item ADD COLUMN " + column, relabelCreateEnd))
.as(column + " converges before grouped MODIFY")
.isBetween(relabelCreateEnd, relabelModify - 1);
}
int start = overseas.indexOf("-- 五期正式结构必须由海外固定入口自行补齐");
int end = overseas.indexOf("USE baiyue_lastleg;", start);
String phase5 = overseas.substring(start, end);
assertThat(phase5).doesNotContain("\nUPDATE ", "\nDELETE ", "\nINSERT ");
}
@Test
void overseasFixedEntryUsesTheCurrentBaselineDefinitions() throws Exception {
String baseline = repositoryFile("lastleg-service/src/main/resources/db/lastleg.sql");
String overseas = repositoryFile("deploy/sql/海外区域统一增量-MySQL8.sql");
for (String table : PHASE5_TABLES) {
assertThat(normalize(createStatement(overseas, table)))
.as(table)
.isEqualTo(normalize(createStatement(baseline, table)));
}
}
@Test
void overseasFixedAdminProjectionAssertionMatchesItsAuthorityArray() throws Exception {
String overseas = repositoryFile("deploy/sql/海外区域统一增量-MySQL8.sql");
int start = overseas.indexOf("SET @platform_admin_overseas_authorities = JSON_ARRAY(");
int end = overseas.indexOf(");", start);
String authorityArray = overseas.substring(start, end);
int authorityCount = count(authorityArray, "'platform:");
assertThat(authorityCount).isPositive();
assertThat(overseas).contains("JSON_LENGTH(authorities_json) <> " + authorityCount);
}
@Test
void overseasReturnOrderBackfillNormalizesLegacyColumnCollations() throws Exception {
String overseas = repositoryFile("deploy/sql/海外区域统一增量-MySQL8.sql");
assertThat(overseas).contains(
"o.source_order_id COLLATE utf8mb4_unicode_ci = CAST(r.id AS CHAR) COLLATE utf8mb4_unicode_ci",
"o.source_order_no COLLATE utf8mb4_unicode_ci = r.return_no COLLATE utf8mb4_unicode_ci");
}
@Test
void domesticFixedEntryKeepsIamPermissionsButOwnsNoLastlegBusinessSchema() throws Exception {
String domestic = repositoryFile("deploy/sql/头程数据库统一增量-MySQL8.sql");
assertThat(domestic).contains(
"platform:overseas-warehouse:dispatch:list",
"platform:overseas-warehouse:exception-order:list",
"platform:overseas-warehouse:customer-visit:list");
assertThat(domestic).doesNotContain(
"USE baiyue_lastleg",
"CREATE DATABASE IF NOT EXISTS baiyue_lastleg",
"CREATE TABLE IF NOT EXISTS ll_",
"ALTER TABLE ll_");
}
private static String createStatement(String sql, String table) {
String token = "CREATE TABLE IF NOT EXISTS " + table + " (";
int start = sql.indexOf(token);
int end = sql.indexOf(';', start);
assertThat(start).as(table).isGreaterThanOrEqualTo(0);
assertThat(end).as(table).isGreaterThan(start);
return sql.substring(start, end + 1);
}
private static String normalize(String sql) {
return sql.toLowerCase(Locale.ROOT).replaceAll("\\s+", "");
}
private static int count(String source, String token) {
return source.split(java.util.regex.Pattern.quote(token), -1).length - 1;
}
private static String repositoryFile(String relative) throws Exception {
Path path = Path.of(relative);
if (!Files.exists(path)) path = Path.of("..").resolve(relative);
return Files.readString(path, StandardCharsets.UTF_8);
}
}