Add a server-side, atomic, declarative reconcile loop for a single app —
the foundation of the project-tool design. Developers describe an app's
scripts, routes, triggers, and secret-names in `picloud.toml`, then
`pic pull / plan / apply [--prune]` to converge live state to the manifest.
Server (manager-core):
- apply_service: a pure diff engine (compute_diff) shared by plan and
apply, plus an ApplyService that composes the existing per-repo writes
into ONE Postgres transaction. Identity keys mirror the DB UNIQUE
constraints (script=lower(name); route=(method,host_kind,host,
path_kind,path); trigger=per-kind semantic tuple; secret=name).
Apply takes a per-app advisory lock, recomputes the diff in-tx, applies
scripts -> routes -> triggers, prunes dependents-first, commits, then
refreshes the route table once post-commit.
- apply_api: POST /apps/{id}/plan (AppRead) and /apps/{id}/apply.
Apply requires the per-kind write caps the bundle exercises (all three
when --prune), plus AppSecretsRead when it binds an email trigger.
- tx-accepting repo siblings (insert/update/delete *_tx) so the existing
create/update/delete delegate to one SQL definition each.
- email triggers reference an inbound secret by NAME; the value is
resolved, decrypted (AAD-bound), and re-sealed server-side at apply —
it never travels in the manifest.
CLI (picloud-cli):
- manifest.rs (picloud.toml model), client plan/apply, and the pull/plan/
apply commands. pull rejects filesystem-unsafe script names up front.
Safety properties enforced and tested:
- idempotent: a freshly-pulled manifest re-applies as all-NoOp.
- atomic: a mid-bundle failure rolls back with nothing written.
- routes delete-before-insert so a freed binding is reusable in one apply.
- queue one-consumer invariant held inside the shared tx.
- email triggers are never pruned, and a script that still owns an
email/dead-letter trigger can't be pruned (the FK cascade would destroy
the sealed secret) — refused with a pointer to `pic triggers rm`.
- plan and apply agree on unset email-secret references.
No migration: the existing schema's UNIQUE constraints serve as identity
keys. Groups, env-scoping, and the `enabled` toggle are later milestones.
Tested: manager-core lib (360) + CLI bins (27) + 8 project-tool journeys
(pull/plan/apply/prune/email+queue), all green; clippy -D warnings clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
82 lines
2.4 KiB
Rust
82 lines
2.4 KiB
Rust
//! `pic plan` journey: a freshly-pulled manifest must diff to all-no-op
|
|
//! (pull→plan is idempotent), and editing a script source must surface
|
|
//! as an update.
|
|
|
|
use tempfile::TempDir;
|
|
|
|
use crate::common;
|
|
|
|
#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
|
|
#[test]
|
|
fn plan_roundtrips_then_detects_change() {
|
|
let Some(fx) = common::fixture_or_skip() else {
|
|
return;
|
|
};
|
|
let env = common::admin_env(fx);
|
|
let (script_id, guard) = common::deploy_fixture(&env, "plan", "hello.rhai");
|
|
let app = guard.slug().to_string();
|
|
|
|
common::pic_as(&env)
|
|
.args([
|
|
"routes", "create", "--script", &script_id, "--path", "/p", "--method", "GET",
|
|
])
|
|
.assert()
|
|
.success();
|
|
|
|
// Pull the live state, then plan it back — must be a clean no-op.
|
|
let dir = TempDir::new().expect("tempdir");
|
|
common::pic_as(&env)
|
|
.args(["pull", &app, "--dir"])
|
|
.arg(dir.path())
|
|
.assert()
|
|
.success();
|
|
let manifest = dir.path().join("picloud.toml");
|
|
|
|
let out = common::pic_as(&env)
|
|
.args(["plan", "--file"])
|
|
.arg(&manifest)
|
|
.output()
|
|
.expect("plan");
|
|
assert!(
|
|
out.status.success(),
|
|
"plan failed: {}",
|
|
String::from_utf8_lossy(&out.stderr)
|
|
);
|
|
let stdout = String::from_utf8(out.stdout).unwrap();
|
|
let hello = stdout
|
|
.lines()
|
|
.find(|l| l.contains("hello"))
|
|
.unwrap_or_else(|| panic!("no hello row in plan:\n{stdout}"));
|
|
assert!(
|
|
hello.contains("noop"),
|
|
"expected hello no-op, got:\n{stdout}"
|
|
);
|
|
assert!(
|
|
!stdout.contains("create") && !stdout.contains("delete"),
|
|
"fresh pull should diff clean, got:\n{stdout}"
|
|
);
|
|
|
|
// Edit the script source on disk → plan must report an update.
|
|
std::fs::write(
|
|
dir.path().join("scripts/hello.rhai"),
|
|
"let body = #{ ok: false }; body",
|
|
)
|
|
.expect("rewrite source");
|
|
let out = common::pic_as(&env)
|
|
.args(["plan", "--file"])
|
|
.arg(&manifest)
|
|
.output()
|
|
.expect("plan after edit");
|
|
let stdout = String::from_utf8(out.stdout).unwrap();
|
|
let hello = stdout
|
|
.lines()
|
|
.find(|l| l.contains("hello"))
|
|
.unwrap_or_else(|| panic!("no hello row in plan:\n{stdout}"));
|
|
assert!(
|
|
hello.contains("update"),
|
|
"expected hello update after source edit, got:\n{stdout}"
|
|
);
|
|
|
|
drop(guard);
|
|
}
|