| 1 | //! 外层兜底**调得上去**,而且两处缺省说的是同一个数(迁移 0011)。 |
| 2 | //! |
| 3 | //! 为什么非要连库:这个 bug 的形态是**约束在 SQL、校验在 Rust,两边各自漂移**。 |
| 4 | //! `set_worker_concurrency` 放行 1..=256,而列上的 CHECK 曾是 `BETWEEN 1 AND 32`—— |
| 5 | //! 从设置页填 33 到 256 之间任何一个值,Rust 说行、数据库说不行,用户看到的是 |
| 6 | //! 一条 CHECK 约束报错。`cargo check` 和 clippy 对此一个字都不说,因为两边 |
| 7 | //! 根本不在同一种语言里。 |
| 8 | //! |
| 9 | //! 约束的上限当初等于列的缺省(都是 32),于是**兜底一格都调不上去**—— |
| 10 | //! 而 0001 的注释写着它"要明显大于各模型限额之和,否则被限流的任务会占满槽位 |
| 11 | //! 把别的饿死"。约束堵死了它自己的设计。 |
| 12 | //! |
| 13 | //! 没有 `UTOPIA_DATABASE_URL` 时跳过而不是失败。只读 + 改完还原,绝不留下痕迹。 |
| 14 | |
| 15 | use sqlx::PgPool; |
| 16 | |
| 17 | /// Rust 放行的值,数据库必须也放行。 |
| 18 | /// |
| 19 | /// 逐个试边界而不是只试一个:漂移可能出在任何一档上,而这几次查询很便宜。 |
| 20 | #[tokio::test] |
| 21 | async fn every_value_rust_accepts_the_database_accepts_too() -> anyhow::Result<()> { |
| 22 | let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else { |
| 23 | eprintln!("跳过:未设 UTOPIA_DATABASE_URL"); |
| 24 | return Ok(()); |
| 25 | }; |
| 26 | let pool = PgPool::connect(&url).await?; |
| 27 | |
| 28 | let before = utopia_store::access::worker_concurrency(&pool).await?; |
| 29 | |
| 30 | let run = async { |
| 31 | // 33 是当初被卡死的第一格;256 是 Rust 侧的上限 |
| 32 | for v in [1_i32, 33, 64, 255, 256] { |
| 33 | utopia_store::access::set_worker_concurrency(&pool, v) |
| 34 | .await |
| 35 | .map_err(|e| anyhow::anyhow!("Rust 放行了 {v},数据库却拒绝:{e}"))?; |
| 36 | let got = utopia_store::access::worker_concurrency(&pool).await?; |
| 37 | assert_eq!(got, v, "写进去 {v} 读回来却是 {got}"); |
| 38 | } |
| 39 | |
| 40 | // 反向:Rust 拒绝的,不该悄悄落库 |
| 41 | for v in [0_i32, 257, -1] { |
| 42 | assert!( |
| 43 | utopia_store::access::set_worker_concurrency(&pool, v) |
| 44 | .await |
| 45 | .is_err(), |
| 46 | "{v} 越界了却被接受" |
| 47 | ); |
| 48 | } |
| 49 | Ok::<(), anyhow::Error>(()) |
| 50 | } |
| 51 | .await; |
| 52 | |
| 53 | // 还原:这是共享的部署设置,测试不该改变别人的运行参数 |
| 54 | utopia_store::access::set_worker_concurrency(&pool, before).await?; |
| 55 | run |
| 56 | } |
| 57 | |
| 58 | /// 表里没有行时 Rust 的兜底值,必须与列的缺省是同一个数。 |
| 59 | /// |
| 60 | /// 两处分开写(一处 SQL、一处 Rust),改一处不会带上另一处。不一致的后果很隐蔽: |
| 61 | /// 有行的库跑一个数、空表的库跑另一个数,而两者都不报错。 |
| 62 | #[tokio::test] |
| 63 | async fn the_two_defaults_say_the_same_number() -> anyhow::Result<()> { |
| 64 | let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else { |
| 65 | eprintln!("跳过:未设 UTOPIA_DATABASE_URL"); |
| 66 | return Ok(()); |
| 67 | }; |
| 68 | let pool = PgPool::connect(&url).await?; |
| 69 | |
| 70 | // 列的缺省:直接问 information_schema,不猜 |
| 71 | let column_default: Option<String> = sqlx::query_scalar( |
| 72 | "SELECT column_default FROM information_schema.columns |
| 73 | WHERE table_name = 'deployment_settings' AND column_name = 'worker_concurrency'", |
| 74 | ) |
| 75 | .fetch_one(&pool) |
| 76 | .await?; |
| 77 | let column_default: i32 = column_default |
| 78 | .as_deref() |
| 79 | .and_then(|s| s.split("::").next()) |
| 80 | .and_then(|s| s.trim().parse().ok()) |
| 81 | .ok_or_else(|| anyhow::anyhow!("读不出列缺省:{column_default:?}"))?; |
| 82 | |
| 83 | // Rust 的兜底:把行藏起来再问一次 |
| 84 | let mut tx = pool.begin().await?; |
| 85 | sqlx::query("DELETE FROM deployment_settings") |
| 86 | .execute(&mut *tx) |
| 87 | .await?; |
| 88 | let fallback: Option<(i32,)> = |
| 89 | sqlx::query_as("SELECT worker_concurrency FROM deployment_settings LIMIT 1") |
| 90 | .fetch_optional(&mut *tx) |
| 91 | .await?; |
| 92 | assert!(fallback.is_none(), "行没删掉,下面这句就白测了"); |
| 93 | tx.rollback().await?; // **一定回滚**:deployment_settings 是单例,删了服务就没设置了 |
| 94 | |
| 95 | // access.rs 的 unwrap_or 里那个数 |
| 96 | let rust_fallback = 64; |
| 97 | assert_eq!( |
| 98 | column_default, rust_fallback, |
| 99 | "列缺省是 {column_default},Rust 兜底是 {rust_fallback}——两处漂开了" |
| 100 | ); |
| 101 | Ok(()) |
| 102 | } |