Skip to content

World's first open-source enterprise world model.

Run database-backed tests in the backend job so CI compiles the test binaries once (#872)

WaylandYang4137520796f9
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index d7d11f14..62cb653b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -15,8 +15,24 @@ env:
CARGO_TERM_COLOR: always
jobs:
+ # 连库测试也在这里跑。从前它们放在下面的 migrations job:那边有库、这边没有,
+ # 于是同一批测试二进制被编了两遍——backend 的 `cargo test --workspace` 编一遍
+ # 然后全部跳过,migrations 再编一遍才真的跑(store 约 150s、server 约 130s 全是重复编译)。
+ # 给这个 job 也起一个 Postgres,一次编译全部跑完,migrations job 只留迁移本身的检查。
+ # 附带的好处:分支保护只要求 backend 与 web 通过,连库测试挪进来才真正成为合并门槛
backend:
runs-on: ubuntu-latest
+ services:
+ postgres:
+ image: pgvector/pgvector:pg16
+ env:
+ POSTGRES_USER: utopia
+ POSTGRES_PASSWORD: utopia
+ POSTGRES_DB: utopia
+ ports: ["5432:5432"]
+ options: >-
+ --health-cmd pg_isready --health-interval 5s
+ --health-timeout 5s --health-retries 10
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
@@ -29,12 +45,23 @@ jobs:
run: cargo fmt --all --check
- name: Clippy
run: cargo clippy --workspace --all-targets -- -D warnings
+ # 库要先迁移好:store 的一百多个集成测试和 server 的多数 fixture 不自己跑迁移,
+ # 对着空库并发起几百个测试会成片撞 relation "organizations" does not exist(#869)。
+ # 不用 sqlx-cli——装它要一分多钟,而这个 job 在关键路径上;这个 example 复用
+ # 测试本来就要编的 utopia-store,几乎不花额外时间
+ - name: 迁移测试库
+ run: cargo run -p utopia-store --example migrate
+ env:
+ UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia
- name: Test
run: cargo test --workspace
# 装了 Poppler 就必须真的用它测:没有它时 PDF 回退的那个测试会跳过,
- # 而一个静静跳过的测试等于没写(#248)
+ # 而一个静静跳过的测试等于没写(#248)。库同理:有库就**必须**连
+ # (UTOPIA_TEST_REQUIRE_DB),连库测试没跑成要红,不能跳过显示绿色
env:
UTOPIA_TEST_REQUIRE_PDFTOTEXT: "1"
+ UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia
+ UTOPIA_TEST_REQUIRE_DB: "1"
- name: Build
run: cargo build --workspace
@@ -74,40 +101,9 @@ jobs:
# 第二遍必须也过:迁移改号后要能在已经跑过旧号的库上安全重放
- name: 再跑一遍
run: sqlx migrate run --database-url postgres://utopia:utopia@localhost:5432/utopia
- # 连库的测试只有这个 job 跑得动——rust job 没有数据库,那些测试在那边
- # 会跳过并显示绿色。不在这里跑一遍,等于写了测试却永远没执行过,
- # 比没写更糟:它会让人以为 SQL 有覆盖
- # 整套 store 集成测试都在这里跑,而且**没有库就失败**(UTOPIA_TEST_REQUIRE_DB):
- # 从前只跑 graph_changes,其余二十几个在没有库的 backend job 里静默跳过,
- # 绿色是假的(#248)
- - name: 连库测试
- run: |
- set -o pipefail
- cargo test -p utopia-store 2>&1 | tee store-tests.log
- env:
- UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia
- UTOPIA_TEST_REQUIRE_DB: "1"
- - name: 摘要
- if: always()
- run: |
- passed=$(grep -o '[0-9]* passed' store-tests.log | awk '{s+=$1} END {print s+0}')
- failed=$(grep -o '[0-9]* failed' store-tests.log | awk '{s+=$1} END {print s+0}')
- echo "utopia-store against Postgres: **${passed} passed**, ${failed} failed — a missing database fails this job instead of skipping" >> "$GITHUB_STEP_SUMMARY"
-
- # utopia-server 里连库的测试同样以 test_db::url() 为门槛,backend job 没有库就静默跳过。
- # 从前这里靠一张过滤器清单(mcp / chat / retrieval / documents_routes)逐个点名,
- # 清单外的模块——jobs_routes、rule_*、pipeline、ingest_sources、type_alignment、
- # extraction、query_engine::postgres——一个都没在 CI 上跑过(#248、#868)。
- # 改成整个 crate 一起跑,和上面的 utopia-store 一样,新模块不用再记得来登记。
- # 不连库的测试跟着再跑一遍,代价只有几秒:编译早就付过了(此前第一个过滤器步骤
- # 就要编一遍测试二进制),385 个测试本身跑完不到 10 秒。
- # 依赖前面「全新库上跑一遍」已把库迁移好:chat 等 fixture 不自己跑迁移,
- # 空库上并发起 385 个测试会撞 relation "organizations" does not exist。
- - name: utopia-server against Postgres
- run: cargo test -p utopia-server
- env:
- UTOPIA_DATABASE_URL: postgres://utopia:utopia@localhost:5432/utopia
- UTOPIA_TEST_REQUIRE_DB: "1"
+ # 连库测试不在这里:它们随 backend job 的 `cargo test --workspace` 一起跑,
+ # 那边同样有库、同样设了 UTOPIA_TEST_REQUIRE_DB。曾经放在这里是因为只有这个 job
+ # 有库,代价是同一批测试二进制编两遍;这个 job 现在只管迁移本身能不能跑、能不能重放
web:
runs-on: ubuntu-latest
diff --git a/CONTRIBUTING.zh-CN.md b/CONTRIBUTING.zh-CN.md
index f148b8bd..36ca8ac7 100644
--- a/CONTRIBUTING.zh-CN.md
+++ b/CONTRIBUTING.zh-CN.md
@@ -75,10 +75,11 @@ let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else {
它们守的是**编译器看不见的东西**:SQL 里的表别名、`NULL` 参与比较时的行为、`INNER JOIN` 悄悄滤掉的行、递归 CTE 在菱形继承下会不会把同一个祖先展开两次。`cargo check` 和 clippy 对这些一个字都不说。
-碰了 `crates/utopia-store/` 里的 SQL,请把它设上再跑一遍:
+碰了 `crates/utopia-store/` 里的 SQL,请把它设上再跑一遍。库要先迁移好——绝大多数连库测试不自己跑迁移,对着空库直接跑会成片报 relation does not exist:
```bash
export UTOPIA_DATABASE_URL=postgres://utopia:utopia@localhost:5432/utopia
+cargo run -p utopia-store --example migrate # 空库先迁移;CI 的 backend job 也是这么做的
cargo test --workspace
```
diff --git a/crates/utopia-store/examples/migrate.rs b/crates/utopia-store/examples/migrate.rs
new file mode 100644
index 00000000..3d835ae5
--- /dev/null
+++ b/crates/utopia-store/examples/migrate.rs
@@ -0,0 +1,27 @@
+//! 把 `UTOPIA_DATABASE_URL` 指向的库迁移到最新,然后退出。
+//!
+//! 连库测试假定库已经迁移好:store 的一百多个集成测试和 server 的多数 fixture 一上来
+//! 就建数据,不自己跑迁移,只有零星几个调 `db::migrate`。对着一个全新空库直接
+//! `cargo test --workspace`,先跑到的测试会撞 relation "organizations" does not exist
+//! (#869 验证时就是这样红了 32 个)。CI 从前靠 sqlx-cli 先 `migrate run` 一遍,可装它
+//! 要一分多钟,而连库测试如今在关键路径上的 backend job 里跑;这个 example 复用测试
+//! 本来就要编的 utopia-store,几乎不花额外时间。本地同理:`docker compose up -d db`
+//! 之后 `cargo run -p utopia-store --example migrate`,连库测试就能跑。
+//!
+//! 它只前滚,不做 sqlx-cli 的另一件事——在已迁移过的库上再放一遍看是否安全,
+//! 那仍是 migrations job 的活。
+
+#[tokio::main]
+async fn main() -> anyhow::Result<()> {
+ let url = std::env::var("UTOPIA_DATABASE_URL")
+ .ok()
+ .filter(|u| !u.trim().is_empty())
+ .ok_or_else(|| anyhow::anyhow!("UTOPIA_DATABASE_URL 未设置"))?;
+ // 建表建触发器要的权限比运行期高,和 utopia-server 启动时一样只开一个小池子、用完就关
+ let pool = utopia_store::db::connect(&url, Some(2)).await?;
+ utopia_store::db::migrate(&pool).await?;
+ pool.close().await;
+ // 不回显地址:它带密码,而这行会进 CI 日志
+ println!("数据库迁移完成");
+ Ok(())
+}
diff --git a/crates/utopia-store/src/test_db.rs b/crates/utopia-store/src/test_db.rs
index 4812a547..e5960552 100644
--- a/crates/utopia-store/src/test_db.rs
+++ b/crates/utopia-store/src/test_db.rs
@@ -2,10 +2,13 @@
//!
//! 每个连库测试都以同一句开头:没有 `UTOPIA_DATABASE_URL` 就跳过而不是失败,
//! 本地随手 `cargo test` 不必先起库。可 CI 上也这么跳,绿色就成了假的:backend job
-//! 没有库,24 个 store 集成测试全部静默返回,而有库的 migrations job 只跑了一个。
+//! 曾经没有库,24 个 store 集成测试全部静默返回,而有库的 migrations job 只跑了一个。
//!
-//! 所以跳过要分场合:设了 `UTOPIA_TEST_REQUIRE_DB` 的地方(CI 的连库 job),
-//! 没有库就是失败——「本该跑的没跑」得看得见。
+//! 所以跳过要分场合:设了 `UTOPIA_TEST_REQUIRE_DB` 的地方(CI 的 backend job,
+//! 它现在自带 Postgres),没有库就是失败——「本该跑的没跑」得看得见。
+//!
+//! 这里只给地址,不迁移:绝大多数调用方假定库已经迁好。空库先跑
+//! `cargo run -p utopia-store --example migrate`,CI 也是这么做的。
/// 连库测试用的数据库地址。`None` = 这次跳过。
///