feat(A10 W1): Flyway 平台表迁移单一真相(本地 PG 真验)
platform-app 加 flyway-core+flyway-database-postgresql(10.10.0,Boot BOM 管版本);db/migration/V1__sys_page_schema/V2__sys_datastatus/V3__sys_ai_agent(DDL 从既有幂等 bootstrap 迁入,V2 去测试种子行);application.yml 启 Flyway(baseline-on-migrate + baseline-version=0,只管 EIP4 新增 SYS_* 平台表,不碰客户业务表);dev/local 的 spring.sql.init 收敛移除→app 运行态 provisioning 单一走 Flyway。FlywayMigrationLocalIT 对真实 PostgreSQL 15.18 验:①全新 schema→V1-V3 建全表+幂等;②既有非空 schema(已有 sys_page_schema+数据)→baseline V0→V1-V3 幂等应用、既有行不破坏(=客户库接管)。docker build 两镜像真验过(daemon 已起)。全 reactor mvn clean install 绿(无 surefire @SpringBootTest 受 Flyway 影响)。残留 follow-up:bootstrap-*.sql(ci.sh/AiAgentDdlTest/parity fixture 用)与 db/migration 文档级重复,全收敛(删 bootstrap/ci.sh repoint)后续。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,9 @@
|
||||
> 目标读者:拍板人 + 执行者。本文是**待拍板设计**,不是已实现记录。对应进度总览 §3 A10(🔴 未起步)、§5 P2#9、§10.2「生产交付能力缺口」。
|
||||
> 定位:把「dev 能跑」推进到「客户环境可交付、可回滚、可观测」。**纯设计产出,不依赖运行环境**;落地需用户环境(真 DB / 容器宿主 / Gitea runner)。
|
||||
|
||||
> **🛠️ 实施进度(2026-06-25)**:**W2+W3+W4 MVP 骨架已落地**(按推荐默认 D1 nginx 单源 / D3 compose+systemd 双轨 / D4 env / D6 客户 PG)——`platform-app/Dockerfile`、`flowable-service/Dockerfile`、`deploy/{nginx.conf,docker-compose.yml,.env.example,README.md,systemd/*.service}`。验证:`docker compose config` 通过(4 服务 + 变量插值);**`docker build` 待有 daemon 的 dev/CI 真验**(本机 daemon 未起)。验证过程揪出并修:flowable-service 双数据源(FLOWABLE_JDBC_*/BIZ_JDBC_*)env 漏传已补。**W1 Flyway 未做(下一批次)→ 当前 compose 假定 DB 已置备,尚不能从零自举平台表**。W5/W6 需准生产。
|
||||
> **🛠️ 实施进度(2026-06-25,本地 PG+Docker 真验)**:**W1 Flyway 已实现 + 真验**——`platform-app` 加 flyway-core + flyway-database-postgresql(10.10.0),`db/migration/V1-V3`(sys_page_schema/sys_datastatus/sys_ai_*,DDL 从既有幂等 bootstrap 迁入),`application.yml` 启 Flyway(baseline-on-migrate + baseline-version=0),dev/local 的 spring.sql.init 收敛移除(app 运行态 provisioning 单一走 Flyway)。`FlywayMigrationLocalIT` 对真实 PostgreSQL 15.18 验两场景全绿:① 全新 schema→V1-V3 建全表+幂等;② 既有非空 schema(已有 sys_page_schema+数据)→baseline V0→V1-V3 幂等应用、既有行不破坏(=客户库接管场景)。**docker build 两镜像真验通过**(daemon 已起:platform-app 727MB/flowable 641MB)。**残留(收敛 follow-up)**:platform-ai/metadata/flow-gateway 的 bootstrap-*.sql 仍在(ci.sh/AiAgentDdlTest/parity fixture 用),与 db/migration 形成文档级重复——全收敛(删 bootstrap、ci.sh/测试改指 db/migration)待后续;app 运行态已单源。
|
||||
>
|
||||
> **W2+W3+W4 MVP 骨架已落地**(按推荐默认 D1 nginx 单源 / D3 compose+systemd 双轨 / D4 env / D6 客户 PG)——`platform-app/Dockerfile`、`flowable-service/Dockerfile`、`deploy/{nginx.conf,docker-compose.yml,.env.example,README.md,systemd/*.service}`。验证:`docker compose config` 通过(4 服务 + 变量插值);**`docker build` 待有 daemon 的 dev/CI 真验**(本机 daemon 未起)。验证过程揪出并修:flowable-service 双数据源(FLOWABLE_JDBC_*/BIZ_JDBC_*)env 漏传已补。**W1 Flyway 未做(下一批次)→ 当前 compose 假定 DB 已置备,尚不能从零自举平台表**。W5/W6 需准生产。
|
||||
|
||||
## 0. 一句话
|
||||
|
||||
@@ -61,7 +63,7 @@ EIP4 当前只有 **dev 形态**(手工 `run-dev.sh` 起两个 fat jar + 前
|
||||
|
||||
## 3.1 W1 Flyway 详细执行规格(ready-to-run,2026-06-25 勘探后细化)
|
||||
|
||||
> **状态**:未实现(需 PG 验 migrate 才能安全落地)。但已做前置去重 + 钉死精确步骤,接 PG 即可执行。
|
||||
> **状态(2026-06-25 更新)**:✅ **已实现 + 对本地真实 PG 验证通过**(见上「实施进度」+ `FlywayMigrationLocalIT`)。下方为落地时遵循的规格,现已按此落地;残留全收敛(删 bootstrap/repoint ci.sh)为 follow-up。
|
||||
> **已做前置(2026-06-25)**:`bootstrap-ai-agent.sql` 两份去重——删 platform-app 副本,单一真相归 `platform-ai/src/main/resources/bootstrap/bootstrap-ai-agent.sql`(platform-app 经依赖传递消费;原同 classpath 路径双副本=解析歧义,已消除)。
|
||||
|
||||
**为何需 PG 才能安全做**:迁移 SQL 是 PG 专有(`::jsonb`/`::xml`/`ON CONFLICT`),H2 跑不了;本机 Docker daemon 未起→testcontainers 也用不了。盲发自动 migrate 进客户库启动路径=高爆炸半径,必须先在 dev PG 验过 `flyway migrate` + 回退。
|
||||
|
||||
@@ -23,6 +23,9 @@
|
||||
<!-- 可观测性(总览 §10.2):仅 /actuator/health(含 liveness/readiness 探针),不上 Prometheus 全家桶 -->
|
||||
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
|
||||
<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><scope>runtime</scope></dependency>
|
||||
<!-- A10 W1:平台表迁移单一真相(db/migration);Flyway 10.x 拆库支持,PG 需 flyway-database-postgresql。版本由 Spring Boot BOM 管 -->
|
||||
<dependency><groupId>org.flywaydb</groupId><artifactId>flyway-core</artifactId></dependency>
|
||||
<dependency><groupId>org.flywaydb</groupId><artifactId>flyway-database-postgresql</artifactId></dependency>
|
||||
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
|
||||
<!-- 模块边界纪律:ArchUnit 规则跑在能看到全部类的 app 模块测试里 -->
|
||||
<dependency><groupId>com.tngtech.archunit</groupId><artifactId>archunit-junit5</artifactId><scope>test</scope></dependency>
|
||||
|
||||
@@ -22,10 +22,7 @@ spring:
|
||||
hikari:
|
||||
maximum-pool-size: 10
|
||||
read-only: false
|
||||
sql:
|
||||
init:
|
||||
mode: always
|
||||
schema-locations: classpath:bootstrap/bootstrap-ai-agent.sql
|
||||
# A10 W1:平台表(含 AI Agent)provisioning 已收敛到 Flyway(db/migration);原 spring.sql.init 自举移除。
|
||||
|
||||
eip4:
|
||||
auth:
|
||||
|
||||
@@ -13,10 +13,7 @@ spring:
|
||||
hikari:
|
||||
maximum-pool-size: 5
|
||||
read-only: false
|
||||
sql:
|
||||
init:
|
||||
mode: always
|
||||
schema-locations: classpath:bootstrap/bootstrap-ai-agent.sql
|
||||
# A10 W1:平台表(含 AI Agent)provisioning 已收敛到 Flyway(db/migration);原 spring.sql.init 自举移除。
|
||||
|
||||
server:
|
||||
port: 8084
|
||||
|
||||
@@ -5,6 +5,15 @@ spring:
|
||||
# 认证走 JWT(非表单/Basic),排除默认内存用户管理:免生成误导性 dev 口令、免无用 AuthManager
|
||||
exclude:
|
||||
- org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
|
||||
# A10 W1:EIP4 平台表迁移单一真相(db/migration/V*.sql)。启动时自动 migrate。
|
||||
# baseline-on-migrate + baseline-version=0:既有客户库(非空,eip3 schema)首次接管时 baseline 于 V0
|
||||
# (不动既有对象),再跑 V1+;迁移 SQL 幂等(CREATE IF NOT EXISTS),既有平台表也安全。
|
||||
# 仅管 EIP4 新增 SYS_* 平台表,不碰客户业务表 schema(保真层原样消费)。
|
||||
flyway:
|
||||
enabled: true
|
||||
baseline-on-migrate: true
|
||||
baseline-version: 0
|
||||
locations: classpath:db/migration
|
||||
datasource:
|
||||
url: jdbc:postgresql://192.168.20.68:5435/postgres?currentSchema=eip3
|
||||
username: ceshi
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
-- A10 W1 Flyway 迁移 V1:平台表 sys_page_schema(页面 schema 服务化 P4-4)。
|
||||
-- 源:platform-metadata/.../bootstrap/bootstrap-page-schema.sql 的 DDL(逐字,仅建表+索引)。
|
||||
-- 幂等(CREATE IF NOT EXISTS):配合 baseline-on-migrate,既有客户库已有此表也安全。
|
||||
CREATE TABLE IF NOT EXISTS sys_page_schema (
|
||||
template_id character varying(64) NOT NULL,
|
||||
name character varying(255),
|
||||
schema_json jsonb NOT NULL,
|
||||
schema_etag character varying(64) NOT NULL,
|
||||
version integer NOT NULL DEFAULT 1,
|
||||
update_time bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_page_schema PRIMARY KEY (template_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_page_schema_name ON sys_page_schema (name);
|
||||
@@ -0,0 +1,11 @@
|
||||
-- A10 W1 Flyway 迁移 V2:平台表 sys_datastatus(流程网关业务状态回写)。
|
||||
-- 源:platform-flow-gateway/.../bootstrap/bootstrap-datastatus.sql 的 DDL(仅建表+索引)。
|
||||
-- 注:原 bootstrap 含 P34-WB-TEST-* 测试种子行,属【测试 fixture】,生产迁移不含。
|
||||
-- 口径:三列无主键(对齐客户库已知漂移 迁移手册 §7.2);DATASTATUS 值见 ProcessCoreService 常量。
|
||||
CREATE TABLE IF NOT EXISTS sys_datastatus (
|
||||
businesskey character varying(200),
|
||||
tablename character varying(200),
|
||||
datastatus integer
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_datastatus_bk ON sys_datastatus (businesskey);
|
||||
@@ -0,0 +1,86 @@
|
||||
-- A10 W1 Flyway 迁移 V3:P5 AI Agent 运行态平台表(任务/草稿/变更包/执行/特性包/修订/审计)。
|
||||
-- 源:platform-ai/.../bootstrap/bootstrap-ai-agent.sql 的 DDL(逐字,仅建表+索引)。
|
||||
-- 幂等:配合 baseline-on-migrate,既有库已有这些表也安全。
|
||||
-- 纪律:只存 AI 运行态/草稿,不直接写 SYS_PAGE_SCHEMA / SYS_ENTITY / 服务流等生产元数据。
|
||||
CREATE TABLE IF NOT EXISTS sys_ai_task (
|
||||
task_id character varying(64) NOT NULL,
|
||||
objective text NOT NULL,
|
||||
status character varying(32) NOT NULL,
|
||||
plan_json jsonb NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
updated_at bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_ai_task PRIMARY KEY (task_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sys_ai_draft (
|
||||
draft_id character varying(64) NOT NULL,
|
||||
task_id character varying(64) NOT NULL,
|
||||
artifact_type character varying(32) NOT NULL,
|
||||
target_id character varying(128) NOT NULL,
|
||||
payload_json jsonb NOT NULL,
|
||||
gates_json jsonb NOT NULL,
|
||||
metadata_json jsonb NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_ai_draft PRIMARY KEY (draft_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sys_ai_change_set (
|
||||
change_set_id character varying(64) NOT NULL,
|
||||
task_id character varying(64) NOT NULL,
|
||||
status character varying(32) NOT NULL,
|
||||
draft_ids_json jsonb NOT NULL,
|
||||
summary_json jsonb NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_ai_change_set PRIMARY KEY (change_set_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sys_ai_run (
|
||||
run_id character varying(64) NOT NULL,
|
||||
task_id character varying(64) NOT NULL,
|
||||
change_set_id character varying(64) NOT NULL,
|
||||
run_type character varying(32) NOT NULL,
|
||||
status character varying(32) NOT NULL,
|
||||
payload_json jsonb NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_ai_run PRIMARY KEY (run_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sys_ai_feature_package (
|
||||
package_id character varying(64) NOT NULL,
|
||||
task_id character varying(64) NOT NULL,
|
||||
change_set_id character varying(64) NOT NULL,
|
||||
status character varying(32) NOT NULL,
|
||||
manifest_json jsonb NOT NULL,
|
||||
deployment_package_json jsonb NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_ai_feature_package PRIMARY KEY (package_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sys_ai_revision (
|
||||
revision_id character varying(64) NOT NULL,
|
||||
task_id character varying(64) NOT NULL,
|
||||
package_id character varying(64) NOT NULL,
|
||||
status character varying(32) NOT NULL,
|
||||
note text NOT NULL,
|
||||
snapshot_json jsonb NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_ai_revision PRIMARY KEY (revision_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sys_ai_audit_log (
|
||||
audit_id character varying(64) NOT NULL,
|
||||
task_id character varying(64) NOT NULL,
|
||||
action character varying(96) NOT NULL,
|
||||
actor character varying(64) NOT NULL,
|
||||
details_json jsonb NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
CONSTRAINT pk_sys_ai_audit_log PRIMARY KEY (audit_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_ai_draft_task ON sys_ai_draft (task_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_ai_change_set_task ON sys_ai_change_set (task_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_ai_run_task ON sys_ai_run (task_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_ai_run_change_set ON sys_ai_run (change_set_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_ai_feature_package_task ON sys_ai_feature_package (task_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_ai_revision_task ON sys_ai_revision (task_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_sys_ai_audit_log_task ON sys_ai_audit_log (task_id, created_at);
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.eip4.platform.app;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.flywaydb.core.api.output.MigrateResult;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* A10 W1 Flyway 平台表迁移真验(本地 PG,localhost:5433)。
|
||||
*
|
||||
* <p>验证 {@code classpath:db/migration} 的 V1-V3 在真实 PostgreSQL 上:
|
||||
* ① 全新 schema 上 migrate 创建全部 EIP4 平台表(sys_page_schema/sys_datastatus/sys_ai_*);
|
||||
* ② 二次 migrate 幂等(0 个新迁移);
|
||||
* ③ baseline-on-migrate + baseline-version=0 在【已有同名表的非空 schema】上 baseline 后照常应用、零报错
|
||||
* (模拟客户既有库接管),既有表不被破坏。
|
||||
*
|
||||
* <p>用隔离 schema {@code flyway_it_*},用例后 DROP CASCADE 清理,不污染业务数据。
|
||||
* 命名 *IT:不入默认 surefire(无 DB 的 CI 不强依赖);本地经 {@code -Dtest=FlywayMigrationLocalIT} 真跑。
|
||||
* DB 不可达时 {@code assumeTrue} 跳过(非伪绿:跳过有明确前置原因)。
|
||||
*/
|
||||
class FlywayMigrationLocalIT {
|
||||
|
||||
private static final String URL = "jdbc:postgresql://localhost:5433/eip4dev";
|
||||
private static final String USER = "zhengwr";
|
||||
private static final String PASS = "";
|
||||
|
||||
private static final List<String> EXPECTED_TABLES = List.of(
|
||||
"sys_page_schema", "sys_datastatus",
|
||||
"sys_ai_task", "sys_ai_draft", "sys_ai_change_set", "sys_ai_run",
|
||||
"sys_ai_feature_package", "sys_ai_revision", "sys_ai_audit_log");
|
||||
|
||||
private boolean reachable() {
|
||||
try (Connection c = DriverManager.getConnection(URL, USER, PASS)) {
|
||||
return c.isValid(3);
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Flyway flyway(String schema) {
|
||||
return Flyway.configure()
|
||||
.dataSource(URL, USER, PASS)
|
||||
.schemas(schema)
|
||||
.locations("classpath:db/migration")
|
||||
.baselineOnMigrate(true)
|
||||
.baselineVersion("0")
|
||||
.load();
|
||||
}
|
||||
|
||||
private void exec(String sql) throws SQLException {
|
||||
try (Connection c = DriverManager.getConnection(URL, USER, PASS); Statement s = c.createStatement()) {
|
||||
s.execute(sql);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> tablesIn(String schema) throws SQLException {
|
||||
List<String> out = new ArrayList<>();
|
||||
try (Connection c = DriverManager.getConnection(URL, USER, PASS); Statement s = c.createStatement();
|
||||
ResultSet rs = s.executeQuery(
|
||||
"SELECT tablename FROM pg_tables WHERE schemaname='" + schema + "' ORDER BY 1")) {
|
||||
while (rs.next()) {
|
||||
out.add(rs.getString(1));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrate_creates_all_platform_tables_and_is_idempotent() throws SQLException {
|
||||
assumeTrue(reachable(), "本地 PG localhost:5433 不可达,跳过(前置:需本地 dev 库)");
|
||||
String schema = "flyway_it_fresh";
|
||||
try {
|
||||
exec("DROP SCHEMA IF EXISTS " + schema + " CASCADE; CREATE SCHEMA " + schema);
|
||||
|
||||
MigrateResult r = flyway(schema).migrate();
|
||||
assertThat(r.migrationsExecuted).as("V1+V2+V3 应全部应用").isEqualTo(3);
|
||||
assertThat(tablesIn(schema)).as("全部 EIP4 平台表已建")
|
||||
.containsAll(EXPECTED_TABLES);
|
||||
|
||||
// 幂等:二次 migrate 无新迁移
|
||||
assertThat(flyway(schema).migrate().migrationsExecuted)
|
||||
.as("二次 migrate 幂等").isZero();
|
||||
} finally {
|
||||
exec("DROP SCHEMA IF EXISTS " + schema + " CASCADE");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void baseline_on_existing_nonempty_schema_does_not_break() throws SQLException {
|
||||
assumeTrue(reachable(), "本地 PG localhost:5433 不可达,跳过");
|
||||
String schema = "flyway_it_existing";
|
||||
try {
|
||||
// 模拟客户既有库:schema 非空,且已有一张目标表(过去 bootstrap 建过)
|
||||
exec("DROP SCHEMA IF EXISTS " + schema + " CASCADE; CREATE SCHEMA " + schema);
|
||||
exec("CREATE TABLE " + schema + ".sys_page_schema ("
|
||||
+ "template_id varchar(64) PRIMARY KEY, name varchar(255), schema_json jsonb NOT NULL,"
|
||||
+ " schema_etag varchar(64) NOT NULL, version integer NOT NULL DEFAULT 1, update_time bigint NOT NULL)");
|
||||
exec("INSERT INTO " + schema + ".sys_page_schema(template_id,schema_json,schema_etag,update_time)"
|
||||
+ " VALUES('keep-me','{}'::jsonb,'etag',1)");
|
||||
|
||||
// baseline-on-migrate:非空 schema 无 history → baseline V0 → 跑 V1-V3(幂等,既有表 IF NOT EXISTS 跳过)
|
||||
MigrateResult r = flyway(schema).migrate();
|
||||
assertThat(r.migrationsExecuted).as("baseline 后 V1-V3 应用").isEqualTo(3);
|
||||
assertThat(tablesIn(schema)).containsAll(EXPECTED_TABLES);
|
||||
|
||||
// 既有数据不被破坏
|
||||
try (Connection c = DriverManager.getConnection(URL, USER, PASS); Statement s = c.createStatement();
|
||||
ResultSet rs = s.executeQuery("SELECT count(*) FROM " + schema + ".sys_page_schema WHERE template_id='keep-me'")) {
|
||||
rs.next();
|
||||
assertThat(rs.getInt(1)).as("既有行未被迁移破坏").isEqualTo(1);
|
||||
}
|
||||
} finally {
|
||||
exec("DROP SCHEMA IF EXISTS " + schema + " CASCADE");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user